Announcement

Collapse
No announcement yet.

Loop Error - Please Help!

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Loop Error - Please Help!

    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.

    Code:
    <?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.

  • #2
    Re: Loop Error - Please Help!

    Is this your's looks like that paid mod O.O

    Comment


    • #3
      Re: Loop Error - Please Help!

      Code:
      To be honest, if your new to PHP & MySQL you are good.
      
      As far as i can tell. Never place your..
      
      
      	
      	
      	
      
      
      PHP Code:
      $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() 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(); ?>

      Comment


      • #4
        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:
        PHP Code:
        $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'].'
        '
        ;

        Creating online addictions, one game at a time:
        www.extremecast.com || www.bbgamezone.net

        Comment

        Working...
        X