AdRock Posted July 19, 2011 Share Posted July 19, 2011 I have a list of products in my database (i.e T-Shirts, etc). Some of them are the same product but different sizes and prices but still the same thing and also any other random product in there that doesn't come in different sizes. I have a shopping cart which normally connects to the database and creates a form for each product. What i want to do is if it's a product without a size, just create the usual form, but if it's a T-Shirt for example with different sizes, put them in a select drop list. Here is my code that loops through the database and outputs the form just for a standard form (no sizes) while (list($prodid,$item,$description,$price) = $result->fetchrow()) { if ($count % NUMCOLS == 0) echo "<tr>"; # new row echo '<td>'; echo '<form method="post" action="" class="jcart"> <fieldset> <input type="hidden" name="jcartToken" value="'.$_SESSION['jcartToken'].'" /> <input type="hidden" name="my-item-id" value="'.$prodid.'" /> <input type="hidden" name="my-item-name" value="'.$item.'" /> <input type="hidden" name="my-item-price" value="'.$price.'" /> <input type="hidden" name="my-item-url" value="product.php?prodid='.$prodid.'" /> <ul> <li><img src="images/shop/products/test.gif" /></li> <li><strong><a href="product.php?prodid='.$prodid.'">'.$item.'</a></strong></li> <li>Price: £'.number_format($price, 2).'</li> <li> <label>Qty: <input type="text" name="my-item-qty" value="1" size="3" /></label> </li> </ul> <div class="buttonadd"> <input type="submit" name="my-add-button" value=" " class="addcart" /> </div> </fieldset> </form> '; echo '</td>'; $count++; $counter++; if ($count % NUMCOLS == 0) echo "</tr>\n"; # end row } here is the table structure CREATE TABLE `shop` ( `prodid` int(2) NOT NULL auto_increment, `itemid` int(2) NOT NULL default '0', `item` varchar(50) NOT NULL default '', `size` char(2) NOT NULL default '', `description` text NOT NULL, `image` varchar(50) NOT NULL default '', `price` float NOT NULL default '0', PRIMARY KEY (`prodid`) ) I have added a field called itemid which links all different sizes of one product to it. WHat i need to know now is how i code so it sees how many of an an itemid there is and if it's one, stick it in a normal form and if it's greater than one , put it into a select list Link to comment https://forums.phpfreaks.com/topic/242357-looping-through-database-and-output-to-select-list-or-not/ Share on other sites More sharing options...
djlee Posted July 19, 2011 Share Posted July 19, 2011 well you need to know BEFORE any output can begin, you cant get a count in the same query so your left with two options. Get a count via query by grouping on item id, or store everything into an array such as $products = array(); $query = xxx while($row = mysql_fetch_assoc($query)) { $products[$row['product_id']][] = $row } That will give you an array where the key is the item id (which is not unique in this context) andthe value is an array of products, so then you can do foreach($products as $productid => $arr) { if(count($arr) > 1) { // more than 1 } else { // only 1 } } However it may be better to seperate it out, so you have products in a table, then another table with the product_id, size and price. That way your not duplicating data and it just makes life easier (i.e. the array would be a value of products with a nested array of sizes => prices, which would be much better) Link to comment https://forums.phpfreaks.com/topic/242357-looping-through-database-and-output-to-select-list-or-not/#findComment-1244771 Share on other sites More sharing options...
AdRock Posted July 19, 2011 Author Share Posted July 19, 2011 I'll have to give that a try. I see the logic in that and how it works. Link to comment https://forums.phpfreaks.com/topic/242357-looping-through-database-and-output-to-select-list-or-not/#findComment-1244782 Share on other sites More sharing options...
AdRock Posted July 19, 2011 Author Share Posted July 19, 2011 I've been thinking about this and the logic is fine but how to put the records with the count array higher than 1 into a select list? Link to comment https://forums.phpfreaks.com/topic/242357-looping-through-database-and-output-to-select-list-or-not/#findComment-1244853 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.