duanchi0897 2015-04-22 13:44
浏览 137

PHP foreach输出四次(mysql,sql,数据库管理)

I have some code here which I have a feeling can be made much more efficient. I have been building a library catalogue database, and I am looking for a way to store multiple columns (with multiple entries) extracted from an SQL database into one PHP array. This would mean only one foreach loop would then be required in the 'view' code to print the results out and store them in a table. Any of you guys have useful ideas? :)

My code in its current state is below:

MODEL CODE:

$matches["loandates"] = array();
        $matches["loanduedates"] = array();
        $matches["itemnames"] = array();
        $matches["itemauthors"] = array();

        try { // if we do find an item, then we run this query against the database to get a list of all the current loans.
            $results = $db->prepare("
                SELECT il.loan_date, il.loan_duedate, i.item_name, i.item_author 
                FROM itemloan il 
                INNER JOIN libraryitem li
                ON il.libraryItem_id = li.libraryItem_id
                INNER JOIN item i
                ON li.item_id = i.item_id
                WHERE il.user_id = ?
                ORDER BY il.loan_date asc
                    ");
            $results->bindParam(1,$userid); // puts user id value where the placeholder is. First value is character of placeholder, second value is variable we want to bind to placeholder).
            $results->execute();
            } catch (Exception $e) {
                echo "Data could not be retrieved from the database.";
                exit;
            }

             while ($row = $results->fetch(PDO::FETCH_ASSOC)) { // we loop through the loans one at a time, and add them to our matches variable
                $matches["loandates"][] = $row["loan_date"];

                $matches["loanduedates"][] = $row["loan_duedate"];

                $matches["itemnames"][] = $row["item_name"];

                $matches["itemauthors"][] = $row["item_author"];

            }

          return $matches;                  
    }

VIEW CODE:

<table id="name">

                    <?php 
                                    foreach($user["itemnames"] as $itemname) { 

                                            echo "<tr><td id='radiocell'><input type='radio' name='libraryitemid' id='radio'></td><td class='user'>" . $itemname . "</td></tr>";
                                            }
                    ?>

                </table>

                <table id="author">
                    <?php

                                    foreach($user["itemauthors"] as $itemauthor) {

                                            echo "<tr><td class='user'>" . $itemauthor . "</td></tr>";

                                            }
                            ?>

                </table>

                <table id="date">
                    <?php



                                    foreach($user["loandates"] as $loandate) {

                                            $time = strtotime($loandate);
                                            $myFormatForView = date("l d F Y", $time);

                                            echo "<tr><td class='user'>" . $myFormatForView  . "</td></tr>";

                                            }
                            ?>

                </table>

                <table id="duedate">
                    <?php

                                    foreach($user["loanduedates"] as $loanduedate) {

                                            $time = strtotime($loanduedate);
                                            $myFormatForView = date("l d F Y", $time);

                                            echo "<tr><td class='user'>" . $myFormatForView . "</td></tr>";

                                            }
                            ?>

                </table>

I ultimately want just one table with one foreach loop rather than four tables with four foreach loops. If you get my meaning.

I would be very thankful if anyone could help! Thanks heaps in advance!!

Regards,

Robert, London UK

  • 写回答

2条回答 默认 最新

  • download1214 2015-04-22 14:06
    关注
     $matches[] = array("loandates" => $row["loan_date"],
                   "loanduedates" => $row["loan_duedate"],
                    "itemnames" => $row["item_name"],
                    "itemauthors" => $row["item_author"]);
    

    Now you have one array and you can display it with one foreach.

    foreach($matches as $match) {
       echo $match['loandates'] . '</td><td>' . $match['loan_duedate'] ...
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大