Jump to content

Count by id, help.


Kristian_C
 Share

Recommended Posts

PHP/script

<script language=JavaScript>function sell_all (amount, total){ 				var del = confirm("Are you sure you wish to sell " + amount + " cars, totaling $ " + total); 				if (del == true){ var loc="garage.php?sell_all=true&sell_s=go"; window.location=loc; } 			}			</script><?$mysql=mysql_query("SELECT * FROM users WHERE username='$username'");$fetch=mysql_fetch_object($mysql);if (strip_tags($_GET['sell_s'])){$vv=$_POST['vv'];$i=0; $d=0;foreach($_POST['vv'] as $val){$nice=mysql_fetch_object(mysql_query("SELECT * FROM garage WHERE id='$val'"));$i+=$nice->worth;$d++;}if (strip_tags($_GET['sell_all']) != "true" ){ echo "<script> sell_all('$d', '$i') </script>"; } if (strip_tags($_GET['sell_all']) == "true"){$n_money = $fetch->money + $nice->worth;mysql_query("UPDATE users SET money='$n_money' WHERE username='$username'");mysql_query("DELETE FROM garage WHERE id='$nice'"); echo "Cars Sold"; }

HTML

<form name="form1" method="post" action="?sell_s=go">  <table width="52%" border="1" align="center" cellpadding="2" cellspacing="0" class=thinline bordercolor=black>	<tr bordercolor="#000000" background="includes/grad.jpg">	  <td height="25" background="includes/grad.jpg" colspan="9"><center class="style2">		  <strong>Garage </strong>	  </center></td>	</tr>	<tr bgcolor=#666666>	  <td height="201" colspan="6" class=tip><div align="center"></div>		  <div align="center"><img src="includes/garagepic.jpg" width="642" height="201"></div></td>	</tr>	<tr>	  <td height="24" colspan="6" background="includes/grad.jpg" class=tip><div align="center"></div>		  <div align="center">			<div align="center"></div>			<div align="center"></div>			<div align="center"></div>			<div align="center"><span class="style1 style2"><strong>Your cars: </strong></span></div>		  </div>		  <div align="center"></div>		  <div align="center"></div>		  <div align="center"></div></td>	</tr>	<tr bgcolor=#666666>	  <td width="36%" height="24" class=tip><div align="center"><span class="style1">Type(id)</span></div></td>	  <td width="12%" class=tip><div align="center"><span class="style1">Damage</span></div></td>	  <td width="21%" class=tip><div align="center"><span class="style1">Origion</span></div></td>	  <td width="21%" class=tip><div align="center"><span class="style1">Location</span></div></td>	  <td width="10%" class=tip><div align="center"><span class="style1 style2">Sell</span></div></td>	</tr>	<?   $gather = mysql_query("SELECT * FROM garage WHERE owner='$username'");  $count=mysql_num_rows($gather);  if ($count=="0"){  echo "<tr><td colspan=6 bgcolor='666666' cellspace=2><center>No Cars In Garage</center></td></tr>";  } $e=0;  while($object=mysql_fetch_object($gather)){if ($e == "0"){ $color="#666666"; $e=1; }else{ $color="#434343"; $e=0; }  echo   "  <tr bgcolor=$color> 	<td><input type=checkbox name='vv[]' value='$object->id'><a href=?type=$object->id&prc=$for>$object->car</a>  <font color='black'><b>$object->id</b></font></td>	<td width=13%>$object->damage%</td>  	<td width=16%>$object->origion</td>";	if($object->status==1){		$ships=$object->shiptime-time();		if($ships<1){			mysql_query("UPDATE garage SET status='0' WHERE id='$object->id'");		}	  echo"	<td width=19%>$ships seconds before $object->car will arive in $object->location</td>	  ";	}else{	  echo"		  <td width=19%>$object->location (<a onclick=ship($object->id); href=#>Ship!</a>)</td>";	}	 echo "<td><a href='?sell=$object->id&prc=$type->car'</a>Sell</td>";  }  ?>  </table> <table width="52%" border="1" align="center" cellpadding="2" cellspacing="0" class=thinline bordercolor=black>	<tr> 	  <td height="20" colspan="2" background="includes/grad.jpg"><center class="style2">		<strong>		  Options 		</strong>	  </center></td>	</tr>	<tr bgcolor="#666666"> 	  <td width="49%" height="26"><div align="center" class="style2"><strong> 		  <input name="sell_s" type="submit" id="sell_s" value="Sell selected">		</strong></div></td>	  <td width="51%"><div align="center" class="style2"><strong> 		  <input name="delete_s" type="submit" id="delete_s" value="Delete selected">		</strong></div></td>	<tr bgcolor="#666666">	  <td height="26" colspan="2"><div align="center" class="style2"><strong>Function in progress. </strong></div></td>  </table></form>

Anyone knows what i can do? it comes up in the alert box functionally and stuff, but it says : Invalid argument supplied for foreach()so i really dont know whats worng :S

Link to comment
Share on other sites

If it says you have an invalid argument to foreach, then you are not giving foreach an array. Foreach only works on arrays. This will happen if no checkboxes were selected. You can do a check on the variable before you run the loop:

$vv=$_POST['vv'];$i=0; $d=0;if (is_array($_POST['vv'])){  foreach($_POST['vv'] as $val){	$nice=mysql_fetch_object(mysql_query("SELECT * FROM garage WHERE id='$val'"));	$i+=$nice->worth;	$d++;  }}

If $d is 0 then you know the loop didn't run.

Link to comment
Share on other sites

Thanks mate, now i works without errors... But dont delete the id/update the users money tho...i really cant do so much with count functions so dont know what do make it count several id's to delete from the db... can you help with that to?and thanks again :)

Link to comment
Share on other sites

I'm not sure what you're asking, what are you trying to do?
well i have a game (online mafia game), but still working on some functions that i need to comlete before i can start makeing new ones... and it is like a "garage" lets say it is a inbox then and in most inboxes you have check boxes to delete more then one message.. its just like the same, the player checks the boxes that counts id from the table garage and when he pushes sell it comes up a alert box with : are you shure you want to sell # of cars for a total of xxx money.If yes it is a code that should be counting all id's to delete and update the users table with the money the user should get by selling his cars... understand? ...
Link to comment
Share on other sites

Right, so what are you having problems with? For the checkboxes, you will want to name them all the same thing (an array) and have the value of each one be the database ID.<input type="checkbox" name="cars[]" value="123">You can get the list of IDs as an array from post:$cars = $_POST['cars'];and loop through it to total up the sum, or do whatever. If you are showing a confirmation screen between the form and the database work, then you will want to have a hidden form on the confirmation screen containing all the same stuff but inside hidden elements instead of checkboxes:<input type="hidden" name="cars[]" value="123">

Link to comment
Share on other sites

Right, so what are you having problems with? For the checkboxes, you will want to name them all the same thing (an array) and have the value of each one be the database ID.<input type="checkbox" name="cars[]" value="123">You can get the list of IDs as an array from post:$cars = $_POST['cars'];and loop through it to total up the sum, or do whatever. If you are showing a confirmation screen between the form and the database work, then you will want to have a hidden form on the confirmation screen containing all the same stuff but inside hidden elements instead of checkboxes:<input type="hidden" name="cars[]" value="123">
The problem is just to get the counted sum and id's to be updated/deleted from the db :the count code that u fixed for we works so its counting and showing the sum and total cars.. but need to delete all ids from sql as ive said and update the users money thats my only problem :hers the count/delete/update codes :
<script language=JavaScript>function sell_all (amount, total){ 				var del = confirm("Are you sure you wish to sell " + amount + " cars, totaling $ " + total); 				if (del == true){ var loc="garage.php?sell_all=true&sell_s=go"; window.location=loc; } 			}			</script><?$mysql=mysql_query("SELECT * FROM users WHERE username='$username'");$fetch=mysql_fetch_object($mysql);if (strip_tags($_GET['sell_s'])){$vv=$_POST['vv'];$i=0; $d=0;if (is_array($_POST['vv'])){  foreach($_POST['vv'] as $val){	$nice=mysql_fetch_object(mysql_query("SELECT * FROM garage WHERE id='$val'"));	$i+=$nice->worth;	$d++;  }}if (strip_tags($_GET['sell_all']) != "true" ){ echo "<script> sell_all('$d', '$i') </script>"; } if (strip_tags($_GET['sell_all']) == "true"){$n_money = $fetch->money + $nice->worth;mysql_query("UPDATE users SET money='$n_money' WHERE username='$username'");mysql_query("DELETE FROM garage WHERE id='$nice'"); echo "Cars Sold"; } }

and i have a form like this :

<form name="form1" method="post" action="?sell_s=go">  <table width="52%" border="1" align="center" cellpadding="2" cellspacing="0" class=thinline bordercolor=black>	<tr bordercolor="#000000" background="includes/grad.jpg">	  <td height="25" background="includes/grad.jpg" colspan="9"><center class="style2">		  <strong>Garage </strong>	  </center></td>	</tr>	<tr bgcolor=#666666>	  <td height="201" colspan="6" class=tip><div align="center"></div>		  <div align="center"><img src="includes/garagepic.jpg" width="642" height="201"></div></td>	</tr>	<tr>	  <td height="24" colspan="6" background="includes/grad.jpg" class=tip><div align="center"></div>		  <div align="center">			<div align="center"></div>			<div align="center"></div>			<div align="center"></div>			<div align="center"><span class="style1 style2"><strong>Your cars: </strong></span></div>		  </div>		  <div align="center"></div>		  <div align="center"></div>		  <div align="center"></div></td>	</tr>	<tr bgcolor=#666666>	  <td width="36%" height="24" class=tip><div align="center"><span class="style1">Type(id)</span></div></td>	  <td width="12%" class=tip><div align="center"><span class="style1">Damage</span></div></td>	  <td width="21%" class=tip><div align="center"><span class="style1">Origion</span></div></td>	  <td width="21%" class=tip><div align="center"><span class="style1">Location</span></div></td>	  <td width="10%" class=tip><div align="center"><span class="style1 style2">Sell</span></div></td>	</tr>	<?   $gather = mysql_query("SELECT * FROM garage WHERE owner='$username'");  $count=mysql_num_rows($gather);  if ($count=="0"){  echo "<tr><td colspan=6 bgcolor='666666' cellspace=2><center>No Cars In Garage</center></td></tr>";  } $e=0;  while($object=mysql_fetch_object($gather)){if ($e == "0"){ $color="#666666"; $e=1; }else{ $color="#434343"; $e=0; }  echo   "  <tr bgcolor=$color> 	<td><input type=checkbox name='vv[]' value='$object->id'><a href=?type=$object->id&prc=$for>$object->car</a>  <font color='black'><b>$object->id</b></font></td>	<td width=13%>$object->damage%</td>  	<td width=16%>$object->origion</td>";	if($object->status==1){		$ships=$object->shiptime-time();		if($ships<1){			mysql_query("UPDATE garage SET status='0' WHERE id='$object->id'");		}	  echo"	<td width=19%>$ships seconds before $object->car will arive in $object->location</td>	  ";	}else{	  echo"		  <td width=19%>$object->location (<a onclick=ship($object->id); href=#>Ship!</a>)</td>";	}	 echo "<td><a href='?sell=$object->id&prc=$type->car'</a>Sell</td>";  }  ?>  </table></form>

Link to comment
Share on other sites

There are several problems with this:

if (is_array($_POST['vv'])){  foreach($_POST['vv'] as $val){	$nice=mysql_fetch_object(mysql_query("SELECT * FROM garage WHERE id='$val'"));	$i+=$nice->worth;	$d++;  }}if (strip_tags($_GET['sell_all']) != "true" ){ echo "<script> sell_all('$d', '$i') </script>"; }if (strip_tags($_GET['sell_all']) == "true"){$n_money = $fetch->money + $nice->worth;mysql_query("UPDATE users SET money='$n_money' WHERE username='$username'");mysql_query("DELETE FROM garage WHERE id='$nice'");echo "Cars Sold";}

First, the top, you have the loop that gets an object called $nice and adds the worth property to $i.In the bottom part, you set the money to $fetch->money + $nice->worth. At this point, $nice is set to the last record you got from the database above. So it's set to whatever the last car was, not the total. Apparently you have $i storing the total from all vehicles, so you should be using that. Then in your SQL statement, you have this:mysql_query("DELETE FROM garage WHERE id='$nice'");$nice is an object, not a database ID, which I assume is probably a number. And again, it is only set to the last object you got from the database. It's not set to every object, only the last one because it gets re-set each time through the loop. If you want to delete all of the IDs you are getting from the database above, then create an array of all of the IDs you want to delete in the loop.

$delete_ids = array();if (is_array($_POST['vv'])){  foreach($_POST['vv'] as $val){	$nice=mysql_fetch_object(mysql_query("SELECT * FROM garage WHERE id='$val'"));	$delete_ids[] = $nice->id;	$i+=$nice->worth;	$d++;  }}

And then you can check if there are any IDs to delete, and if so delete them by creating a list to send to the SQL server.So, instead of this:mysql_query("DELETE FROM garage WHERE id='$nice'");You would have something like this:

if (count($delete_ids) > 0){  $id_list = implode(",", $delete_ids);  mysql_query("DELETE FROM garage WHERE id IN ($id_list)");}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...