Having a stupid moment...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
riderdan
Forum Newbie
Posts: 1
Joined: Sat Oct 06, 2007 1:30 am

Having a stupid moment...

Post by riderdan »

I can't debug this, for some reason... Almost certainly PEBKAC :-(

I'm have PHP code to query a MySQL database and display a list of items (groceries actually) and am now working on the update to the database (so when the list is edited, the changes are saved). As part of this (a debugging step) I'm trying to echo the variables that I'll be writing in a loop. Unfortunately, the first time through the loop it works, subsequent passes are returning blanks.

Here's the code.

Code: Select all

$asiles_query="SELECT * FROM asiles ORDER BY 'number'";
$asiles_result=mysql_query($asiles_query);
$asiles_num=mysql_numrows($asiles_result);

$item_query="SELECT * FROM items";
$item_result=mysql_query($item_query);
$item_num=mysql_numrows($item_result);


// This is the outer display loop

	echo "<table>\n";
	echo "<tr>\n<td>&nbsp;</td>\n<td>Item</td>\n<td>Amount</td>\n<td>Asile</td>\n<td>&nbsp;</td>\n</tr>";
	$i=0;
	while ($i < $item_num) 
	{
		$name=mysql_result($item_result,$i,"name");
		$amount=mysql_result($item_result,$i,"amount");
		$asile=(mysql_result($item_result,$i,"asile")-1);
		$selected=mysql_result($item_result,$i,"selected");		
		echo "<tr>\n";
		echo "<form  method=\"POST\" action=\"make_list.php\" >\n<td>";
		echo "<INPUT TYPE=CHECKBOX ";
		if ($selected=="yes")
			{
			echo "CHECKED";
			}
		echo " name=\"checkbox_item[$i]\">\n</td>\n";
		echo "<td>\n<INPUT TYPE=text VALUE=\"".$name."\" name=\"name_item[$i]\">\n</td>\n";
		echo "<td>\n<INPUT TYPE=text VALUE=\"".$amount."\" name=\"amount_item[$i]\">\n</input>\n</td>\n";
		echo "<td>\n<SELECT NAME=\"asile_item[$i]\">\n";

		// This is the loop to set the asile dropdown
		$a=0;
		while ($a < $asiles_num)
		{
			if ($a==mysql_result($item_result,$i,"asile"))
			{
				echo "<OPTION VALUE=\"".$a. "\" selected> ".mysql_result($asiles_result,$a,"name")."</option>\n";
	  		}
			else
			{
				echo "<OPTION VALUE=\"".$a."\"> ".mysql_result($asiles_result,$a,"name")."</option>\n";
			}
		$a++;
		}
	
	echo "</SELECT>\n";
	echo "<input type=\"submit\" name=\"Submit\" value=\"Submit\">\n</form>\n</td>\n</tr>\n";
	$i++;
	}
	echo "</table>";
	echo "\n\n";

//here's the broken part //
	
	for($i=0;$i<$item_num;$i++){
	echo "name=$name_item[$i], amount=$amount_item[$i], selected=$checkbox_item[$i], asile=$asile_item[$i] <br>";
	}
?>
The first loop (where $i=0) it returns the name of the item, amount, selected status, and asile number. The rest of the iterations, it returns blanks.

Could one of the gurus on this forum look it over and help me have one of those "head-slap" moments? I've looked at this so many times my eyes are getting blurry, but I'm just not seeing whatever stupid mistake I made :-)

TIA
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

A much simpler way to do this, and at the same time avoiding repeated calls to mysql_result(), it do simply loop your dataset with mysql_fetch_assoc()

Code: Select all

if (mysql_num_rows($item_result) > 0) { 
   while ($row = mysql_fetch_assoc($result)) {
      echo $row['name'] .'<br>'; //etc etc
   }
}
Post Reply