PHP 1项目转换为PHP循环

时间:2014-10-31 20:18:52

标签: php loops

所以我正在尝试学习php,我有一个简单的1项订单表格,但是我需要在此订单表格中显示的数据库中有4个项目。在javascript中我只会为一个数组编写一个循环,但是从数据库中形成一个表的整个php查询让我感到慌乱,我不知道如何循环结果以显示更多信息中的1个项目和静止允许在1页上全部订购。原来,我让它列出了一个表,所以你点击该项目,它将重定向到每个项目的页面,以便每个项目都可以订购。

<?php

include_once 'helpers/helper.php';
include_once 'db_function.php';
session_start();
$show_query = "SELECT * FROM `meal_info` ORDER BY meal_time DESC LIMIT 4;";
$show_result = db_query($connect, $show_query);
if($show_result) {
$show_result_row = db_fetch_array($show_result);
}
?>
<?php include_once 'layout/header.php'; ?>
<?php 
    if(@$_SESSION['order_submit_msg']) {
        echo flash_message('order_submit_msg');
        unset($_SESSION['order_submit_msg']);
    }
?>
    <div>
        <form action="make_deal.php" method="post">



        <div>
            <h1><?php echo $show_result_row['meal_name'];?></h1>
        </div>
        <hr>
        <div>
            <img class="meal-img" id="meal-img" src="<?php echo $show_result_row['meal_image'];?>" alt="">
        </div>
        <br>
        <div>
            <h2>Feature: <?php echo $show_result_row['meal_content'];?></h2>
            <h3>Price: <?php echo $show_result_row['meal_price'];?></h3>
        </div>

        <input type="hidden" name="meal-id" value="<?php echo $show_result_row['meal_id'];?>">
        <input type="hidden" name="meal-price" value="<?php echo $show_result_row['meal_price'];?>">

       <br>
        <div>
         <legend>Name</legend>
            <input type="text" name="order-username" placeholder="Full Name" required>
        </div>
        <br>
        <div>
            <legend>Phone</legend>
            <input type="text" name="order-phone" placeholder="555-555-5555" required>
        </div>
        <br>
        <div>
            <legend>Address</legend>
            <input type="text" name="order-address" placeholder="555 N. Main Street" required>
        </div>
        <br>
        <div>
            <legend>Quantity (Dozens)</legend>
            <input type="text" name="order-count" placeholder="12" required>

        </div>

        <hr>
        <div>
             <button class="btn btn-warning btn-lg btn-block" type="submit">Submit Order</button>
            </div>
        </form>
    </div>

<?php include_once 'layout/footer.php'; ?>

1 个答案:

答案 0 :(得分:0)

您正在尝试显示数组。你需要遍历数组。

<?php foreach ($show_result_row as $result) { ?>
    <div>
        <h1><?php echo $result['meal_name'];?></h1>
    </div>
    <hr>
    <div>
        <img class="meal-img" id="meal-img" src="<?php echo $result['meal_image'];?>" alt="">
    </div>
<?php } ?>

类似的东西。