dongqiang8058 2017-07-20 18:20 采纳率: 100%
浏览 69
已采纳

用户名一起显示结果

im trying to print out 1 result for each users orders so each item the user orders is saved has its own row so i would like to group them all together and print them out but my coding prints them out 1 by 1

which displays this enter image description here

Has you can see it outputs them item by item but i would like to output everything shadow has ordered inside 1 box then the next user in the other box etc

My table info is here http://prntscr.com/fxv2fr

there are 2 boxes right?? For the same users? different results /item name but the same user i need to group the select by users so it posts everything each user wants into 1 box instead of having 100 boxes for 100 items for 1 user so if users 1 wants 2 different items and user 2 wants 3 different items instead of having 5 boxes there would be 2 due to 2 users in the table with 2 items in the first box for user1 and 3 in the second box for user 2 and it would keep doing this for all

                         <?php

                          $id2 = "1";                     
                         $paid = "1";
$query = $db->prepare('SELECT * FROM orders WHERE takeawayid=:restaurantid AND paid =:paid ORDER BY id ');
 $query->execute(array(':restaurantid' => $id2,':paid' => $paid));

    // alternatively you could use PDOStatement::fetchAll() and get rid of the loop
    // this is dependent upon the design of your app


    while ($row = $query->fetch(PDO::FETCH_ASSOC)) {



    $query2 = $db->prepare("SELECT * FROM orders WHERE takeawayid=:restaurantid AND paid =:paid AND forwho=:forwho ORDER BY id");
 $query2->execute(array(':restaurantid' => $id2,':paid' => $paid,':forwho' => $row['forwho']));


        while ($d = $query2->fetch(PDO::FETCH_ASSOC))



 {









    ?>


                     <div class="col-md-8">
                        <div class="widget">
                           <div class="widget-body">


                       <!-- end: ddsdsd -->        







<div class="row">
<div class="restaurant-listing">







                        <div class="col-xs-12 col-sm-12 col-md-6 single-restaurant grill fish thaifood pizza">
                            <div class="restaurant-wrap">
                                <div class="row">
                                    <div class="col-xs-12 col-sm-3 col-md-12 col-lg-3 text-xs-center">
                                        <a class="" href="#"> <img src="http://santetotal.com/wp-content/uploads/2014/05/default-user.png" alt="Restaurant logo" height="92" width="102"> </a>
                                    </div>
                                    <!--end:col -->
                                    <div class="col-xs-12 col-sm-9 col-md-12 col-lg-9">
                                        <h5><a href="profile.html"><?php echo $d['itemname'] ;?></a></h5> <span></span>
                                        <div class="bottom-part">

                                            <div class="ratings"> <span>








                                                </span>  </div>
                                        </div>
                                    </div>
                                    <!-- end:col -->
                                </div>
                                <!-- end:row -->
                            </div>
                            <!--end:Restaurant wrap -->
                        </div>
                        <!--end: col -->



                    </div>
                </div>
       <!-- end: ddsdsd -->   

        <?php echo $d['forwho'] ;  $price.=$d['price'];?>      







                           </div>
                           <!-- end: Widget -->
                        </div>
                        <!-- /REGISTER -->
                     </div>
                     <!-- WHY? -->


        <?php


    }
    echo "Total: ".$price."</div>";
}

                        ?>  

my above code doesnt group the users so what i need is for each user inside forwho column has 1 box each with all there results in instead its making 1 box for each results

  • 写回答

1条回答 默认 最新

  • dongman5539 2017-07-21 00:27
    关注

    Give it a try.

    • You should create only one PDO instance, e.g. connection. It should be passed as argument to each function that uses it.
    • Of course, transfer style attributes to classes. I implemented my own html.

    Some recommendations:

    • Try to move to OOP.
    • Apply exception handling and activate error reporting/handling. I gave you a general view by throwing only Exception. Normally you should throw and handle the SPL (Standard PHP Library) subtypes of it.
    • Always read in the PHP Manual what PHP functions are returning so that you can correctly apply handle cases (exceptions, bools, etc).

    Resources:


    Here is the code, spread (as it should) on four PHP pages. index.php is the main page, the others are included in it.

    index.php

    <?php
    require_once 'configs.php';
    require_once 'generalFunctions.php';
    require_once 'ordersFunctions.php';
    
    // Activate error reporting (only on development).
    activateErrorReporting();
    
    try {
        // Create db connection.
        $connection = createConnection(
                MYSQL_HOST
                , MYSQL_DATABASE
                , MYSQL_USERNAME
                , MYSQL_PASSWORD
                , MYSQL_PORT
                , MYSQL_CHARSET
        );
    
        $takeawayId = 1;
        $paid = 1;
    
        // Fetch paid orders by takeaway.
        $fetchedOrders = fetchPaidOrdersByTakeaway($connection, $takeawayId, $paid);
    
        // For testing.
        // printData($fetchedOrders, TRUE);
        // Close connection
        closeConnection($connection);
    } catch (PDOException $pdoException) {
        // On development.
        printData($pdoException, TRUE);
    
        // On production.
        // echo $pdoException->getMessage();
        exit();
    } catch (Exception $exception) {
        // On development.
        printData($exception, TRUE);
    
        // On production.
        // echo $exception->getMessage();
        exit();
    }
    
    // Group orders by users.
    $ordersByUsers = groupOrdersByUsers($fetchedOrders);
    
    // For testing.
    // printData($ordersByUsers, TRUE);
    ?>
    
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>Orders</title>
    
            <!-- Bootstrap -->
            <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
    
            <!-- jQuery -->
            <script src="https://code.jquery.com/jquery-3.2.1.min.js" type="text/javascript" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
    
            <!-- Bootstrap -->
            <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" type="text/javascript"></script>
        </head>
        <body>
    
            <div class="container" style="margin-top: 40px;">
                <div class="row">
                    <div class="col-xs-12">
                        <?php
                        foreach ($ordersByUsers as $userId => $item) {
                            $username = $item['username'];
                            $ordersPrice = $item['ordersPrice'];
                            $orders = $item['orders'];
                            ?>
                            <div class="row">
                                <div class="col-xs-12 col-md-10" style="background-color: #FAFAF8; border: 1px solid #ddd; padding: 20px; margin-bottom: 20px;">
                                    <div class="row" style="padding: 20px;">
                                        <div class="col-xs-12 col-sm-3">
                                            <div class="row">
                                                <a href="#">
                                                    <img src="http://santetotal.com/wp-content/uploads/2014/05/default-user.png" class="img-responsive" alt="Restaurant logo" height="92" width="102">
                                                </a>
                                            </div>
                                            <div class="row" style="padding-top: 10px;">
                                                <a href="#" style="text-transform: uppercase;">
                                                    <?php echo $username; ?>
                                                </a>
                                            </div>
                                        </div>
                                        <div class="col-xs-12 col-sm-9">
                                            <?php
                                            foreach ($orders as $order) {
                                                $orderId = $order['orderId'];
                                                $itemname = $order['itemname'];
                                                $price = $order['price'];
                                                $date = $order['date'];
                                                ?>
                                                <div class="row">
                                                    <div class="col-xs-12" style="background-color: #FFF; border: 1px solid #ddd; padding: 20px; margin-bottom: 20px;">
                                                        <h4>
                                                            <a href="profile.html" style="color: orange;">
                                                                <?php echo $itemname; ?>
                                                            </a>
                                                        </h4>
                                                        <div>
                                                            <div>
                                                                <span>
                                                                    <?php echo $date; ?>
                                                                </span>
                                                            </div>
                                                            <div class="ratings">
                                                                <span>5 STARS</span>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                                <?php
                                            }
                                            ?>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-xs-12 col-md-2" style="padding: 20px; padding-top: 40px; font-size: 20px; color: #666; text-transform: uppercase;">
                                    Total: <?php echo $ordersPrice; ?>
                                </div>
                            </div>
                            <?php
                        }
                        ?>
    
                    </div>
                </div>
            </div>
    
        </body>
    </html>
    

    orderFunctions.php

    <?php
    
    /*
     * ---------------------
     * Orders functions
     * ---------------------
     */
    
    /**
     * Fetch paid orders by takeaway.
     * 
     * @param PDO $connection Connection instance.
     * @param integer $takeawayId Takeaway ID.
     * @param integer $paid Paid.
     * @throws Exception
     */
    function fetchPaidOrdersByTakeaway($connection, $takeawayId, $paid) {
        if (!isset($takeawayId)) {
            throw new Exception('Takeaway ID not provided!');
        }
    
        if (!isset($paid)) {
            throw new Exception('Paid not provided!');
        }
    
        // Sql statement.
        $sql = 'SELECT 
                    ord.id AS orderId,
                    usr.id AS userId,
                    usr.username,
                    ord.itemname,
                    ord.price,
                    ord.date
                FROM orders AS ord 
                LEFT JOIN users AS usr ON usr.id = ord.user_id 
                WHERE 
                    takeawayid = :takeawayid 
                    AND paid = :paid 
                ORDER BY 
                    usr.username ASC,
                    ord.date DESC';
    
        // Prepare and check sql statement (returns PDO statement).
        $statement = $connection->prepare($sql);
        if (!$statement) {
            throw new Exception('The SQL statement can not be prepared!');
        }
    
        // Bind values to sql statement parameters.
        $statement->bindValue(':takeawayid', $takeawayId, getInputParameterDataType($takeawayId));
        $statement->bindValue(':paid', $paid, getInputParameterDataType($paid));
    
        // Execute and check PDO statement.
        if (!$statement->execute()) {
            throw new Exception('The PDO statement can not be executed!');
        }
    
        // Fetch data.
        $fetchedData = $statement->fetchAll(PDO::FETCH_ASSOC);
        if ($fetchedData === FALSE) {
            throw new Exception('Fetching data failed!');
        }
    
        return $fetchedData;
    }
    
    /**
     * Group orders by users.
     * 
     * @param array $orders [optional] Orders list.
     * @return array Orders list grouped by users.
     * @throws Exception
     */
    function groupOrdersByUsers(array $orders = array()) {
        $groupedOrders = array();
    
        foreach ($orders as $order) {
            $userId = $order['userId'];
            $username = $order['username'];
            $price = $order['price'];
    
            // Check and add user name as key, if not already.
            if (!array_key_exists($userId, $groupedOrders)) {
                $groupedOrders[$userId] = array(
                    'username' => $username,
                    'orders' => array(),
                    'ordersPrice' => 0,
                );
            }
    
            // Add order to grouped orders list.
            $groupedOrders[$userId]['orders'][] = $order;
            $groupedOrders[$userId]['ordersPrice'] += $price;
        }
    
        return $groupedOrders;
    }
    

    configs.php

    <?php
    
    /*
     * ----------------
     * Database configs
     * ----------------
     */
    
    define('MYSQL_HOST', '...');
    define('MYSQL_PORT', '3306');
    define('MYSQL_DATABASE', '...');
    define('MYSQL_CHARSET', 'utf8');
    define('MYSQL_USERNAME', '...');
    define('MYSQL_PASSWORD', '...');
    

    generalFunctions.php

    <?php
    
    /*
     * ---------------------
     * Data access functions
     * ---------------------
     */
    
    /**
     * Create a new db connection.
     * 
     * @param string $host Host.
     * @param string $dbname Database name.
     * @param string $username Username.
     * @param string $password Password.
     * @param string $port [optional] Port.
     * @param array $charset [optional] Character set.
     * @param array $options [optional] Driver options.
     * @return PDO Db connection.
     */
    function createConnection($host, $dbname, $username, $password, $port = '3306', $charset = 'utf8', $options = array(
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_EMULATE_PREPARES => false,
        PDO::ATTR_PERSISTENT => true,
    )) {
        $dsn = getDsn($host, $dbname, $port, $charset);
        $connection = new PDO($dsn, $username, $password);
        foreach ($options as $key => $value) {
            $connection->setAttribute($key, $value);
        }
        return $connection;
    }
    
    /**
     * Create a mysql DSN string.
     * 
     * @param string $host Host.
     * @param string $dbname Database name.
     * @param string $port [optional] Port.
     * @param array $charset [optional] Character set.
     * @return string DSN string.
     */
    function getDsn($host, $dbname, $port = '3306', $charset = 'utf8') {
        $dsn = sprintf('mysql:host=%s;port=%s;dbname=%s;charset=%s'
                , $host
                , $port
                , $dbname
                , $charset
        );
        return $dsn;
    }
    
    /**
     * Close a db connection.
     * 
     * @param PDO $connection Db connection.
     * @return void
     */
    function closeConnection($connection) {
        $connection = NULL;
    }
    
    /**
     * Get the data type of a binding value.
     * 
     * @param mixed $value Binding value.
     * @return mixed Data type of the binding value.
     */
    function getInputParameterDataType($value) {
        $dataType = PDO::PARAM_STR;
        if (is_int($value)) {
            $dataType = PDO::PARAM_INT;
        } elseif (is_bool($value)) {
            $dataType = PDO::PARAM_BOOL;
        }
        return $dataType;
    }
    
    /*
     * ---------------
     * Print functions
     * ---------------
     */
    
    /**
     * Print data on screen.
     * 
     * @param mixed $data Data to print.
     * @param bool $preformatted Print preformatted if TRUE, print normal otherwise.
     * @return void
     */
    function printData($data, $preformatted = FALSE) {
        if ($preformatted) {
            echo '<pre>' . print_r($data, true) . '</pre>';
        } else {
            echo $data;
        }
    }
    
    /*
     * -------------------------
     * Error reporting functions
     * -------------------------
     */
    
    /**
     * Toggle error reporting.
     * 
     * @param integer $level Error level.
     * @param bool $displayErrors Display errors if TRUE, hide them otherwise.
     * @return void
     */
    function activateErrorReporting($level = E_ALL, $displayErrors = TRUE) {
        error_reporting($level);
        ini_set('display_errors', ($displayErrors ? 1 : 0));
    }
    

    Used tables:

    `users` table

    users table

    `items` table

    items table

    `orders` table

    users table

    Result:

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么