PDA

View Full Version : Loop Error - Please Help!



corpseslayer
06-20-2009, 05:09 PM
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.

CrazyT
06-20-2009, 07:58 PM
Is this your's looks like that paid mod O.O

Haunted Dawg
06-21-2009, 10:48 AM
To be honest, if your new to PHP & MySQL you are good.

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



$variable = mysql_fetch_assoc(mysql_query)) or die(mysql_error());


.. as it seem's to not unset and that's why it will not run again. You can sort this with unset() (http://php.net/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();
?>

codestryke
06-21-2009, 07:32 PM
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'].'
';
}