PHP - 显示表中的每个产品

时间:2017-08-09 11:18:49

标签: php arrays foreach

我有一个products表格,我在其中列出了产品。我想创建一个代码,它将显示相同的样式,但每个产品的价格,名称,描述,图片不同。我已经创建了一些代码来实现这一点。我的代码只显示行中的第一个。我需要列出所有这些。我的代码是:

function grabProducts($con) {
    ?>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <div class="col-md-12" id="please_wait">
        <div class="panel panel-warning">
            <div class="panel-heading">
                <h3 class="panel-title"><i>Loading, Please Wait...</i></h3>
            </div>
            <div class="panel-body">

            </div>
        </div>
    </div>
    <script>
    $(document).ready(function() {
        $('#please_wait').fadeOut(7000);
        $('#content_purchase').hide(0).delay(5000).fadeIn("slow");
    });
    </script>
    <?php

    $users = $this->grabUserInfos($con);
    foreach ($users as $user) {
        $username = $user[1];
        $email = $user[3];
    }

    $site_config = new site_config();
    $member_config = new member_config();

    $result = mysqli_query($con, "SELECT * FROM products");
    $count = mysqli_num_rows($result);

    // See if there are any products in the database
    if ($count > 0) {

        // While loop for each product element
        while ($row = mysqli_fetch_array($result)) {
            $id = $row['id'];
            $name = $row['name'];
            $description = $row['description'];
            $picture = $row['picture_location'];
            $price = $row['price'];
            $stock_monitering = $row['stock_monitering'];
            $stock = $row['stock'];
            $new_stock = $stock - 1;

            $url_path = 'http' . (empty($_SERVER['HTTPS']) ? '' : 's') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
            $url_path_naked = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH).'/pizza/products.php';

            $getValidatedCheck_ = mysqli_query($con, "SELECT txn_id, hasValidated, item_name, amount, currency, payment_date FROM payment_logs WHERE userId = '$username' ORDER BY id DESC LIMIT 1");
            $count_success_payment = mysqli_num_rows($getValidatedCheck_);

            // While loop for our successful payment_logs for each user
            while ($row = mysqli_fetch_array($getValidatedCheck_)) {
                $txn_id = $row['txn_id'];
                $validated_check = $row['hasValidated'];    
                $item_name = $row['item_name']; 
                $item_price = $row['amount'];   
                $item_currency = $row['currency'];
                $payment_date = $row['payment_date'];

                $now = strtotime("-10 minutes");

                // If there is not enough stock, show out of stock
                if($stock <= 0) {
                    $display = 'out_of_stock';
                } else if($stock > 0 && $now > strtotime($payment_date)) {
                    $display = 'show_products';
                }

                // If there are no transactions for that user show the products like normal
                if($count_success_payment == 0) {

                    // If they've not already seen the success message, show them it now and update their hasValidated from 0 to 1 so they don't see it again
                    if ($validated_check == '0') {
                        // If stock monitering is on, update our stock to 1 less than we had before the purchase
                        if($stock_monitering == '1') {
                            mysqli_query($con, "UPDATE products SET stock = '$new_stock' WHERE name = '$name'");
                        }
                        $display = 'show_success';
                    }
                }
            }

    switch($display) {
        case "show_products":
        ?>
            <div class="col-md-3" id="content_purchase" style="display: none;">
                <div class="panel panel-danger">
                    <div class="panel-heading">
                        <h3 class="panel-title">Price: <i>$<?php echo $price; ?></i>
                            <div class="fRight" style="float: right;"><i><?php echo $name; ?></i> Stock
                                (<?php echo $stock; ?>)
                            </div>
                        </h3>
                    </div>
                    <center>
                        <div class="panel-body">
                            <img src="<?php echo $picture; ?>"
                                 style="width: 85%; height: 100px; margin: 15px 0px 15px 0px; border-radius: 5px; border: 2px solid #ED4949;">
                            <form name="paypal_form" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
                                <input type="hidden" name="cmd" value="_xclick">
                                <input type="hidden" name="business"
                                       value="<?php $site_config->grabSiteSettings($con, 'paypal_address'); ?>">
                                <input type="hidden" name="item_name" value="<?php echo $name; ?>">
                                <input type="hidden" name="item_number" value="<?php echo $id; ?>">
                                <input type="hidden" name="amount" value="<?php echo $price; ?>">
                                <input type="hidden" name="quantity" value="1">
                                <input type="hidden" name="currency_code" value="USD">
                                <input type="hidden" name="custom" value="username=<? echo $username; ?>&status=<? echo $checkout_status; ?>&product=<? echo $name; ?>">
                                <input type="hidden" name="notify_url" value="https://benzaofficial.com/pizza/includes/checkout.php">
                                <input type="hidden" name="return"
                                       value="<?php $site_config->grabSiteSettings($con, 'site_url'); ?>/pizza/products.php?status=complete">
                                <input type="hidden" name="cancel_return"
                                       value="<?php $site_config->grabSiteSettings($con, 'site_url'); ?>/pizza/products.php?status=canceled">
                                <button type="submit" class="btn btn-danger"
                                        style="vertical-align : bottom; margin-bottom: 15px; display: block; width: 85%;">
                                    <i class="fa fa-paypal"></i>aypal
                                </button>
                                <form>
                        </div>
                    </center>
                </div>
            </div>
        <?php
        break;
        case "show_success":
        ?>
        <div class="col-md-12" id="content_purchase" style="display: none;">
            <div class="panel panel-success">
                <div class="panel-heading">
                    <h3 class="panel-title"><i>PAYMENT SUCCESS!</i></h3>
                </div>
                <div class="panel-body">
                    <p>You've successfully purchased <i><?php echo $item_name; ?></i> for <i><?php echo $item_price; ?> <i><?php echo $item_currency; ?></i></i> We have emailed you your receipt to <?php echo $email; ?>. You can click <a href="myFiles.php" style="color: white;">here</a> to download your purchased files.<br>Click <a href="<?php echo $url_path; ?>" style="color: white;">here</a> to purchase again.</p>
                </div>
            </div>
        </div>
        <?php
        mysqli_query($con, "UPDATE payment_logs SET hasValidated = '1' WHERE userId = '$username' ORDER BY id DESC LIMIT 1");
        break;
        case "no_products":
        ?>
        <div class="col-md-12" id="content_purchase" style="display: none;">
            <div class="panel panel-danger">
                <div class="panel-heading">
                    <h3 class="panel-title"><i>Currently No Products For Sale</i></h3>
                </div>
                <div class="panel-body">
                    <p>There are currently no products up for sale.</p>
                </div>
            </div>
        </div>
        <?php
        break;
        case "out_of_stock":
        ?>
            <div class="col-md-3" id="content_purchase" style="display: none;">
                <div class="panel panel-danger">
                    <div class="panel-heading">
                        <h3 class="panel-title">Price: <i>$<?php echo $price; ?></i>
                            <div class="fRight" style="float: right;"><i><?php echo $name; ?></i> Stock
                                (<?php echo $stock; ?>)
                            </div>
                        </h3>
                    </div>
                    <center>
                        <div class="panel-body">
                            <img src="<?php echo $picture; ?>"
                                 style="width: 85%; height: 100px; margin: 15px 0px 15px 0px; border-radius: 5px; border: 2px solid #ED4949;">
                            <p><i><?php echo $name; ?></i> is currently out of stock. Please come back later and try again.</p>
                        </div>
                    </center>
                </div>
            </div>
        <?php
        break;
    }
    }
    } else {
        $display = 'no_products';
    }
 }

1 个答案:

答案 0 :(得分:1)

首先你需要一个功能数据选择所有你喜欢的产品

function showproducts($conn,$id){
   $req="SELECT * FROM products ";
   $liste=$conn->query($req);
   return $liste->fetchAll();   
}

比你应该调用这个函数:

$list=$cc->showproducts($cc->conn);

和foreach行可以显示所有列

<?php
   foreach ($list as $l){ ?>
    <tr>
      <td><?php echo $l[0] ;?> </td>
      <td><?php echo $l[1] ;?> </td>
      <td><?php echo $l[2] ;?> </td>
      <td><?php echo $l[3] ;?> </td>
      <td><?php echo $l[7] ;?> </td>
   </tr>
<?php } ?>