开发者

php for each output from database doesn't display correctly

I have a database with a load of products and I am trying to output them. Some of the products have the same itemid but different sizes.

The problem i'm having is getting the products with the same itemid into a select list. I can output the products normally but i want tro group any of the same product in a select list.

Here is a link to my site with a hard coded product list http://operationbraveheart.org.uk/jcart/.

and here is a link to the output of my code http://operationbraveheart.org.uk/jcart/testshop.php

What is happening is that instead of a load of different forms for each product being displayed, i get one form with a massive empty select list.

I want to do something similar but instead of hard coding, i want to loop thought the database, if there is only one item with that itemid, it's a normal form, if there's more than one with the same itemid, it goes in a select list.

Here is my code that shoudl output the products

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>&nbsp;</td>";
       echo "</tr>";
    }
    echo "</table>";

Here is my table structure and example data

  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`)
) 

INSERT INTO `shop` VALUES (1, 1, 'Key Ring', '', 'Key Ring: This can be personalised', '', 2);
INSERT INTO `shop` VALUES (2, 2, 'Tableware', '', 'Tableware: Coasters and Table Mats', '', 3);
INSERT INTO `shop` VALUES (3, 3, 'Braveheart Bear', '', '7" Braveheart Bear (choice of T-Shrt)', '', 9.99);
INSERT INTO `shop` VALUES (4, 4, 'Bravehart Bear', '', '9" Braveheart Bear (choice of T-Shirt)', '', 11.99);
INSERT INTO `shop` VALUES (5, 5, 'Wristband', '', 'Operation Braveheart wristband', '', 2);
INSERT INTO `shop` VALUES (6, 6, 'Mug', 开发者_如何学C'', 'Standard mug', '', 7.99);
INSERT INTO `shop` VALUES (7, 7, 'A5 jotter', '', 'Various designs', '', 0.75);
INSERT INTO `shop` VALUES (8, 8, 'Operation Braveheart T-Shirt', 'S', 'Operation Braveheart T-Shirt.  All size in army green.', '', 6.99);
INSERT INTO `shop` VALUES (9, 8, 'Operation Braveheart T-Shirt', 'M', 'Operation Braveheart T-Shirt. All size in army green.', '', 7.99);
INSERT INTO `shop` VALUES (10, 8, 'Operation Braveheart T-Shirt', 'L', 'Operation Braveheart T-Shirt. All size in army green.', '', 8.99);
INSERT INTO `shop` VALUES (11, 8, 'Operation Braveheart T-Shirt', 'XL', 'Operation Braveheart T-Shirt. All size in army green.', '', 9.99);

I've now added print_r($superitem); and database structre/data


There's no 'named' elements in your product array. You'll note that $row['itemid'] for your array key as produce [] in the print_r output, and there's no ['size'] subscript in the child arrays produced by the rows:

        [0] => Array
            (
                [0] => 1
                [1] => Key Ring
                [2] => Key Ring: This can be personalised
                [3] => 2
            )

.... there's no 'item_id', and no 'size' element there. Presumably the 0=>1 is the item_id, and 3=>2 is the size.

So your form generation code is working properly, but your database fetch is failing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜