2017-04-17 00:55
浏览 51

PHP - 通过mysqli_fetch_array结果循环向行添加额外的产品

I'm having a problem with some code I've written to display products by category and allow customers to choose the size they want. I'm sure it's a simple solution, and almost as certain the problem stems from my lack of understanding how the msqli_fetch_assoc while loop actually works.

The problem is that the first row shows the first product correctly, but the second not just the secone product, but the first and second products. The third row shows the first three products, and so on.

Here is my complete code (please ignore the JQuery/Javascript, I'm brand new to it and will likely be back for help with that in another post.) I've been staring at it for hours. Can someone point out where I've gone wrong here?


$dbserver = 'localhost';
    $dbname = '########';
    $dbuser = '########';
    $dbpassword = '########';

    $category = $_GET['category'];
    $db = mysqli_connect($dbserver, $dbuser, $dbpassword, $dbname);

    if (mysqli_connect_errno()) {
        die("Connection failed: " . mysqli_connect_error());
    mysqli_select_db($db, $dbname);
    $sql = "SELECT * FROM products WHERE category = '".$category."'";
    $result = mysqli_query($db, $sql);

    if (!$result) {
        die ('Error: Could not select products'.mysqli_error($db));

   function make_size_select($product) {
    echo '<select name="unit_menu">';
    $price_set = array(

    $handles = array(
        'halfpint'=>'Half Pint (8 oz)',
        'pint'=>'Pint (16 oz)',
        'dk'=>'Dutch Kettle (16 oz)',
        'quart'=>'Quart (32 oz)');

    foreach ($price_set as $key=>$value) {
        if ( $value > 0.00) {
            echo '<option value="'.$value.'">'.$handles[$key].'</option>';
    echo '</select>';

  <title>Smoky Mountain Honey House</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link href="../css/style.css" rel="stylesheet" type="text/css"  media="all" />
  <link href="../css/custom.css" rel="stylesheet" type="text/css"  media="all" />
  <link href='' rel='stylesheet' type='text/css'>
  <script type="text/javascript" src="../js/jquery-1.9.0.min.js"></script>
  <script type="text/javascript" src="../js/menu.js"></script> 
  $(window).load(function() {
    var product = $.getJSON(<?php echo json_encode($result); ?>);

   // show the price for a single unit based on current values for unit_size and unit_type
  function setUnitPrice(product) {
    // get the value for unit_type
    var item_type = $.closest('.unit_type');
    // get the value for unit size
    var item_size = $.closest('.unit_size');

    // Get the base price for the item
    var unit_price = Object.keys(product[item_size]);

    // If the unit type is 'ind', divide the price by 12
    if (item_type.value == "ind") {
        unit_price = unit_price / 12;

    return unit_price;

 function showUnitPrice(product) {
    var unit_price = setUnitPrice(product);

 function showRowTotal(product) {
    var unit_price = setUnitPrice(product);
    var row_price = unit_price * $(this).closest('.qty').value;


<div class="header_img">
     <div class="himage_half"><img src="../images/honeyhouse-1.jpg" alt="" /></div>
    <div class="himage_half">
        $(document).load() {
<div class="wrap">
    <div class="header">                
        <div class="logo">
            <a href="../index.php"><img src="../images/logo.png" alt=""></a>
        <div class="menu">
          <nav class="clearfix">
            <ul class="clearfix">
                <li><a href="../index.html">HOME</a></li>
                <li><a href="../about.html">ABOUT</a></li>
                <li><a href="../locations.html">LOCATIONS</a></li>
                <li><a href="../shop.html">SHOP</a></li>
                <li><a href="../contact.html">CONTACT</a></li>
            <a href="#" id="pull">Menu</a>


<div class="wrap">
    <div class="content">
    while ($row = mysqli_fetch_assoc($result)) {
    $products[] = $row;
    foreach ($products as $product) { ?>
        <div class="product_row">
        <form name="order_row_<?php echo $product['product_id']; ?>" action="#" method="post">
            <div class="product_cell product_id">
                <input type="hidden" name="product_id" value="<?php echo $product['product_id']; ?>">
                <?php echo ucfirst($product['name']); ?>
            <div class="product_cell">
                <select name="unit_type" onChange="showUnitPrice(product);">
                    <option value="ind" selected>By the Jar</option>
                    <option value="case">By the Case</option>
            <div class="product_cell unit_menu"">
                <?php echo make_size_select($product); ?>
            <div class="product_cell qty">
                <input type="number" name="qty" max="11" label="How many?" />
            <div class="product_cell unit-price"></div>
            <div class="product_cell row-price"></div>
            <div class="product_cell">
                <button class="add-to-cart" onclick="addToCart();">Add To Cart</button>
    <?php }}; ?>


<div class="copy-right">
        <p>&copy; 2016 Smoky Mountain Honey House</p>
        <p>Template by <a href=""> W3layouts</a></p>

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doujie3888 2017-04-19 20:29

    On the suggestion of Bagus Tesa, I took out the for each loop, edited the rest to account for that, and the problem is solved. Thanks, Bagus!

    解决 无用
    打赏 举报
  • 查看更多回答(1条)

相关推荐 更多相似问题