AdRock Posted July 21, 2011 Share Posted July 21, 2011 What i am trying to do is output my products from my database into a form. Each product (itemid) has it's own form. My code is supposed to loop through the list of products, look at the itemid and if there is more than one of the same itemid, put it into a select list. If there is only one itemid, take the other branch and ignore the select list. The problem is I get one form with a massive select list but it has no values. What I am expecting is a load of different forms which a different product in each form and one of the forms has a select box with 4 options for S, M, L and XL Here is my code that is causing the problem. while ($row = $result->fetchrow()) { $superitem[$row['itemid']][] = $row; } foreach($superitem AS $subitem) { list($prodid,$item,$description,$price) = $subitem[0]; if ($count % NUMCOLS == 0) echo "<tr>"; # new row echo '<td>'; //Your normal code up until the select box... echo '<form method="post" action="" class="jcart"> <fieldset> <input type="hidden" name="jcartToken" value="'.$_SESSION['jcartToken'].'" /> <input type="hidden" name="my-item-id" value="2" /> <input type="hidden" name="my-item-price" value="19.50" /> <input type="hidden" name="my-item-url" value="http://yahoo.com" />'; if(count($subitem) > 1) { echo '<li><select name="my-item-name" id="foo">'; foreach($subitem AS $subsubitem) { echo "<option value='".$subsubitem['size']."'>".$subsubitem['size']."</option>"; } echo "</select></li>"; } else { echo '<input type="hidden" name="my-item-name" value="'.$item.'" />'; } echo'<li>Price: $<span class="price">10.00</span></li> <li> <label>Qty: <input type="text" name="my-item-qty" value="1" size="3" /></label> </li> </ul> <input type="submit" name="my-add-button" value="add to cart" class="button" /> </fieldset> </form>'; echo '</td>'; $count++; $counter++; if ($count % NUMCOLS == 0) echo "</tr>\n"; # end row } Quote Link to comment https://forums.phpfreaks.com/topic/242569-for-each-outputting-once-from-database/ Share on other sites More sharing options...
wildteen88 Posted July 21, 2011 Share Posted July 21, 2011 How is $subitem constructed? What is the output of echo '<pre>' . print_r($subitem, true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/242569-for-each-outputting-once-from-database/#findComment-1245796 Share on other sites More sharing options...
Psycho Posted July 21, 2011 Share Posted July 21, 2011 A couple other points/questions 1. No need to dump the DB results into an array if you are only then going to loop through the array one time. Just process the data as you extract the results from the database and save time and memory 2. Where is the constant "NUMCOLS" defined and have you validated that it does have a value? 3. The logic for starting/closing a table row will cause invalid HTML 4. I also see a problem with this logic while ($row = $result->fetchrow()) { $superitem[$row['itemid']][] = $row; } foreach($superitem AS $subitem) { list($prodid, $item, $description, $price) = $subitem[0]; Based upon what I see the foreach loop will iterate over each record from the DB results and $subitem will be an array of the fields in the record. You are then trying to use list() on "$subitem[0]". but, that will only be the first value of a record - not an array. Please show the query you are using and the fields you are using from the database if you use "*" in your SELECT statement. EDIT: Scratch that last statement, I now see that you are creating a multi-dimensional array. Quote Link to comment https://forums.phpfreaks.com/topic/242569-for-each-outputting-once-from-database/#findComment-1245805 Share on other sites More sharing options...
AdRock Posted July 21, 2011 Author Share Posted July 21, 2011 Here is the whole page code <?php include_once('jcart/jcart.php'); require_once('php/database/MySQL.php'); require_once('php/database/connection.php'); require_once('php/init.php'); /** * The home page of the website */ // The title of the page $title = "Operation Braveheart Shop"; // Include the header html require_once("header.inc.php"); echo '<div class="mainbox"><h1>Operation Braveheart Shop</h1>'; $dir = "gallery"; $p = $_GET['pagenum']; define ("NUMCOLS",3); $webpage = basename($cat); $db = & new MySQL($host,$dbUser,$dbPass,$dbName); $count="SELECT COUNT(*) FROM shop"; $sql="SELECT * FROM shop"; // Perform a query getting back a MySQLResult object $res = $db->query($count); $result = $db->query($sql); //get the number of rows in datatbase $getresult = $result->size(); $numrows = $res->fetchrow(); if(isset($_GET['pagenum'])?$page = $_GET['pagenum']:$page = 1); $entries_per_page = 32; $total_pages = ceil($numrows[0]/$entries_per_page); $offset = (($page * $entries_per_page) - $entries_per_page); $sql="SELECT prodid, item, description, price FROM shop ORDER BY prodid ASC LIMIT $offset,$entries_per_page"; // Perform a query getting back a MySQLResult object $result = $db->query($sql); $err = $result->size(); if($err == 0) { echo ("<p>No matches met your criteria.</p>"); } else { $count = 0; if($page == 1) { $counter= 1; } else { $counter= ((($entries_per_page) * ($page-1)) + 1); } echo "<table border='0' id='gallery'>"; while ($row = $result->fetchrow()) { $superitem[$row['itemid']][] = $row; } foreach($superitem AS $subitem) { list($prodid,$item,$description,$price) = $subitem[0]; if ($count % NUMCOLS == 0) echo "<tr>"; # new row echo '<td>'; //Your normal code up until the select box... echo '<form method="post" action="" class="jcart"> <fieldset> <input type="hidden" name="jcartToken" value="'.$_SESSION['jcartToken'].'" /> <input type="hidden" name="my-item-id" value="2" /> <input type="hidden" name="my-item-price" value="19.50" /> <input type="hidden" name="my-item-url" value="http://yahoo.com" />'; if(count($subitem) > 1) { echo '<li><select name="my-item-name" id="foo">'; foreach($subitem AS $subsubitem) { echo "<option value='".$subsubitem['size']."'>".$subsubitem['size']."</option>"; } echo "</select></li>"; } else { echo '<input type="hidden" name="my-item-name" value="'.$item.'" />'; } echo'<li>Price: $<span class="price">10.00</span></li> <li> <label>Qty: <input type="text" name="my-item-qty" value="1" size="3" /></label> </li> </ul> <input type="submit" name="my-add-button" value="add to cart" class="button" /> </fieldset> </form>'; echo '</td>'; $count++; $counter++; if ($count % NUMCOLS == 0) echo "</tr>\n"; # end row } if ($count % NUMCOLS != 0) { while ($count++ % NUMCOLS) echo "<td> </td>"; echo "</tr>"; } echo "</table>"; } if($getresult > 1) pagination_two($dir,$total_pages,$page); ?> <div id="popupContact"> <a id="popupContactClose">Close X</a> <p id="popbuttons"> <a href="checkout.php" id="checkout"><img src="images/shop/checkout-pop.gif" /></a> <a id="continue"><img src="images/shop/continue-pop.gif" /></a> </p> </div> <div id="backgroundPopup"></div> </div> <?php // Include the footer html require_once("footer.inc.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/242569-for-each-outputting-once-from-database/#findComment-1245818 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.