Jump to content
MakeWebGames

Loop Error - Please Help!


corpseslayer

Recommended Posts

I have a database that has a list of items in it that when combined make another item. (or thats the theory) but im having issues getting it to display the information. The loop runs once, then bails and does not run the rest of the code (to complete the table).

The items required part shows with the item names, but the item given and make link do not show. If i take the loop out the table processes fine.

 

<?php
include_once('globals.php');
echo '<h2>Workshop</h2>';

echo '<table border="0" width="95%" cellspacing="1" cellpadding="0" class="table">
  <tr style="background: gray; ">
     <th>Recipe ID</th>
     <th>Items Required</th>
     <th>Item Given</th>
     <th>Make</th>
  </tr>
  <tr>';
     $fetch = mysql_query("SELECT * FROM workshop") or die(mysql_error());
     while($recipe = mysql_fetch_assoc($fetch))   
          {
            $sele = mysql_fetch_assoc($db->query("SELECT inv_id FROM inventory WHERE inv_userid = '{$ir['userid']}'")) or die(mysql_error());
            $itemr = mysql_fetch_assoc($db->query("SELECT itmid, itmname FROM items WHERE itmid = '{$recipe['reciperecieved']}'")) or die(mysql_error());
           //  }
           echo "<td> {$recipe['recipeid']} </td>";
           echo "<td> ";
           $items=explode(",","{$recipe['reciperequired']}");
           $ingredients=count($items);
           for ($i = 0; $i <= $ingredients; $i++) 
               { 
                $itemname = mysql_fetch_assoc($db->query("SELECT itmid, itmname FROM items WHERE itmid = '{$items[$i]}'")) or die(mysql_error());
                echo "{$itemname['itmname']}
";
               }
           echo "</td>";
           echo "<td> {$itemr['itmname']} </td>";
           echo "<td>Make</td></tr>";
          }
     echo     "</table>";
$h->endpage();
?>

 

Above is my code, im new to both PHP and mySQL (I learned in old skool pascal!) so sorry if it looks bad!. I intend ot make the full mode free on here once its finished.

Ill give + to anyone that can help!

Thanks in advance.

Link to comment
Share on other sites

Re: Loop Error - Please Help!

 

To be honest, if your new to PHP & MySQL you are good.

As far as i can tell. Never place your..

[php]
$variable = mysql_fetch_assoc(mysql_query)) or die(mysql_error());
[/php]

 
.. as it seem's to not unset and that's why it will not run again. You can sort this with unset() but here is a fix (hopefully)
 
[php]
<?php
include_once('globals.php');
echo '<h2>Workshop</h2>
<table border="0" width="95%" cellspacing="1" cellpadding="0" class="table">
<tr style="background: gray; ">
	<th>Recipe ID</th>
	<th>Items Required</th>
	<th>Item Given</th>
	<th>Make</th>
</tr>';
$fetch = mysql_query("SELECT * FROM workshop") or die(mysql_error());
while($recipe = mysql_fetch_assoc($fetch))	{
	$sale_fetch = mysql_query("SELECT `inv_id` FROM `inventory` WHERE `inv_userid` = ".$ir['userid']) or die(mysql_error());
	$sele = mysql_fetch_assoc($sale_fetch);
	$itemr_fetch = mysql_query("SELECT `itmid`,`itmname` FROM `items` WHERE `itmid` = ".$recipe['reciperecieved']) or die(mysql_error());
	$itemr = mysql_fetch_assoc($itemr_fetch);
	echo '
<tr>
	<td>'.$recipe['recipeid'].'</td>
	<td>';
	$items = explode(',', $recipe['reciperequired']);
	$ingredients = count($items);
	for ($i = 0; $i <= $ingredients; ++$i)	{
		$itemname_fetch = mysql_query("SELECT `itmname` FROM `items` WHERE `itmid` = ".$items[$i]) or die(mysql_error());
		$itemname = mysql_fetch_assoc($itemname_fetch);
		echo $itemname['itmname'].'
';
	}	echo '
	</td>
	<td>'.$itemr['itmname'].'</td>
	<td>Make</td>';
}	echo '
</table>';
$h->endpage();
?>

 

Link to comment
Share on other sites

Re: Loop Error - Please Help!

You don't need to keep querying the db to get all the ingredients. You already have the ingredients as a comma separated list in the receipt so instead use the IN clause.

Something like:

$items = explode(',', $recipe['reciperequired']);
$ing_count = count($items);
$ing_fetch = mysql_query("SELECT `itmname` FROM `items` WHERE `itmid` IN (".$recipe['reciperequired'].")" or die(mysql_error());
while( $ing = mysql_fetch_assoc($ing_fetch) ) {
   echo $ing['itmname'].'
';
}   
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...