简体   繁体   English

从SQL数据库格式化HTML表

[英]Formatting HTML Table from SQL Database

My database contains deals from my local area. 我的数据库包含本地区域的交易。 So each deal contains a location, deal_id (primary key), day of the week it applies, and the deal itself. 因此,每笔交易都包含一个位置,deal_id(主键),适用的星期几以及交易本身。

数据库架构

Ideally, I list out the data with the headers of my table being: 理想情况下,我列出表头为的数据:

Location , Monday , Tuesday , Wednesday... so on. 位置,星期一,星期二,星期三等等。

With only one instance of each location per row. 每行每个位置只有一个实例。 Problem is, I haven't been able to get that to happen, so far it puts Mondays deal from the first location in the first row for all days. 问题是,我无法做到这一点,到目前为止,它整天都在第一行的第一个位置进行周一交易。 Then the next row is the same place with the Tuesdays deals for each day, like so: 然后,下一行与每天的星期二交易在同一位置,如下所示:

HTML输出表

Here is my PHP code to convert my data to the HTML table: I figure the error is in my loop. 这是将我的数据转换为HTML表的PHP代码:我认为错误出在我的循环中。 Not sure how to switch from one $row to the next 不确定如何从一个$ row切换到下一个

<!DOCTYPE HTML>
<html lang=en>
    <head>
        <title>Ashdeals</title>
        <meta charset = "UTF-8">
        <link rel="stylesheet" type="text/css" href="stylesheet.css">
    </head>
<body>


    <?php 
    //Create Connection
    $mysqli = new MySQLi("localhost", "root", "", "ashdeals");

    $sql = "SELECT * FROM deals";
     // Query Database  
    $result = $mysqli->query($sql);

    //Count the returned rows
    if ($result->num_rows !=0){
    //Turn results into an array   

         echo "<table>
            <tr>
                <th>Location</th>
                <th>Monday</th>
                <th>Tuesday</th>
                <th>Wednesday</th>
                <th>Thursday</th>
                <th>Friday</th>
                <th>Saturday</th>
                <th>Sunday</th>

            </tr>";
         // output data of each row
         while($row = mysqli_fetch_array($result)) {

             $location = $row['location'];
             $deal = $row['deal'];

             echo 
             "<tr>

             <td>" . $location . "</td>
             <td>" . $deal . "</td>
             <td>" . $deal . "</td>
             <td>" . $deal . "</td>
             <td>" . $deal . "</td>
             <td>" . $deal . "</td>
             <td>" . $deal . "</td>
             <td>" . $deal . "</td>
             </tr>";
         }
         echo "</table>";
    } else {
         echo "0 results";
    }

    ?>

</body>

</html>

Simple solution: switch case to every day. 简单的解决方案:每天更换机箱

Edit: Daily deal. 编辑:每日交易。

 <!DOCTYPE HTML>
<html lang=en>
    <head>
        <title>Ashdeals</title>
        <meta charset = "UTF-8">
        <link rel="stylesheet" type="text/css" href="stylesheet.css">
    </head>
<body>


    <?php 
    //Create Connection
    $mysqli = new MySQLi("localhost", "root", "", "ashdeals");

    $sql = "SELECT * FROM deals";
     // Query Database  
    $result = $mysqli->query($sql);

    //Count the returned rows
    if ($result->num_rows !=0){
    //Turn results into an array   

         echo "<table>
            <tr>
                <th>Location</th>
                <th>Monday</th>
                <th>Tuesday</th>
                <th>Wednesday</th>
                <th>Thursday</th>
                <th>Friday</th>
                <th>Saturday</th>
                <th>Sunday</th>

            </tr>";
         // output data of each row
         while($row = mysqli_fetch_array($result)) {
             $location = $row['location'];
             $deal = $row['deal'];
             $day = $row['day'];
             $moDeal = ""; // Monday Deal variable. 
             $tuDeal = ""; // Tuesday Deal variable. 
             $weDeal = ""; // Wednesday Deal variable. 
             $thDeal = ""; // Thursday Deal variable. 
             $frDeal = ""; // Friday Deal variable. 
             $saDeal = ""; // Saturday Deal variable. 
             $suDeal = ""; // Sunday Deal variable. 
             switch ($day) {
                 case 'Mo':
                     $moDeal = $deal; 
                     break;
                 case 'Tu':
                     $tuDeal = $deal;
                     break;
                 case 'We':
                     $weDeal = $deal; 
                     break;
                 case 'Th':
                     $thDeal = $deal; 
                     break;
                 case 'Fr':
                     $frDeal = $deal;
                     break;
                 case 'Sa':
                     $saDeal = $deal; 
                     break;
                 case 'Su':
                     $suDeal = $deal; 
                     break;                 
                 default:
                     # In case some daily deal: 
                    $moDeal = $deal; 
                    $tuDeal = $deal; 
                    $weDeal = $deal; 
                    $thDeal = $deal; 
                    $frDeal = $deal; 
                    $saDeal = $deal; 
                    $suDeal = $deal; 
                     break;
             }
             echo 
             "<tr>
             <td>" . $location . "</td>
             <td>" . $moDeal . "</td>
             <td>" . $tuDeal . "</td>
             <td>" . $weDeal . "</td>
             <td>" . $thDeal . "</td>
             <td>" . $frDeal . "</td>
             <td>" . $saDeal . "</td>
             <td>" . $suDeal . "</td>
             </tr>";

         }
         echo "</table>";
    } else {
         echo "0 results";
    }

    ?>

</body>

</html>

Just loop through: 只需循环:

$dayArray = ['Monday', 'Tuesday' ..... 'Sunday'];
while($row = mysqli_fetch_array($result)) {

    echo "<tr>";

    echo "<td>" . $row['locationField'] . "</td>";

    foreach ($dayArray as $dayField) {
        if ($dayField == $row['dealDayField'] ) {
            echo "<td>" . $row['dealField'] . "</td>";
        }
        else {
            echo "<td></td>";
        }
    }
    echo "</tr>";
}

echo "</table>";

No need to do the assign $valueX and could echo $row[] but shown here for clarity. 无需分配$ valueX并可以回显$ row [],但此处为了清楚起见而显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM