需要帮助来获取MYSQLi行以创建表

时间:2018-12-11 04:58:58

标签: php mysqli

我在通过php创建表的查询行中需要帮助。下面是我的mysql表中名为“例程”的行。

enter image description here

现在,我想在一个循环中获取整个工作日,如下所示:

enter image description here

enter image description here

enter image description here

一个循环可能吗?在这里,我想通过星期分隔行,并通过时间分隔自动列。

1 个答案:

答案 0 :(得分:1)

是的!单循环是可能的,但是我们使用php数组...

尝试一下... 数据插入php编码-insert.php

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $database = "my_db";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $database);

    $week = "Friday";
    $subject = "Physics";
    $time = 3;

    $sql = "INSERT INTO routine (Week, Subject, Time)VALUES ('".$week."', '".$subject."', '".$time."')";

    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $conn->close();

?>

Php代码,用于显示每周的行数和显示时间的自动列-view.php

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $database = "my_db";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $database);

    //Initial Declarations
    $data = [];     

    $data[0][0] = "<tr><td>Week</td>";
    $data[0][1] = "<tr><td>Monday</td>";
    $data[0][2] = 0;

    $data[1][0] = "<tr><td>Week</td>";
    $data[1][1] = "<tr><td>Tuesday</td>";
    $data[1][2] = 0;

    $data[2][0] = "<tr><td>Week</td>";
    $data[2][1] = "<tr><td>Wednesday</td>";
    $data[2][2] = 0;

    $data[3][0] = "<tr><td>Week</td>";
    $data[3][1] = "<tr><td>Thursday</td>";
    $data[3][2] = 0;

    $data[4][0] = "<tr><td>Week</td>";
    $data[4][1] = "<tr><td>Friday</td>";
    $data[4][2] = 0;


    $sql = "SELECT * FROM routine";
    $result = mysqli_query($conn, $sql);

    while($row = mysqli_fetch_array($result, MYSQLI_NUM))       // Table Fields - Week ($row[0]), Subject($row[1]), Time($row[2])
    {
        if($row[0] == "Monday")
        {
            $data[0][0] = $data[0][0] . "<td>" . $row[2] . "</td>";
            $data[0][1] = $data[0][1] . "<td>" . $row[1] . "</td>";
            $data[0][2] = intval($data[0][2]) + 1;
        }
        else if($row[0] == "Tuesday")
        {
            $data[1][0] = $data[1][0] . "<td>" . $row[2] . "</td>";
            $data[1][1] = $data[1][1] . "<td>" . $row[1] . "</td>";
            $data[1][2] = intval($data[1][2]) + 1;
        }
        else if($row[0] == "Wednesday")
        {
            $data[2][0] = $data[2][0] . "<td>" . $row[2] . "</td>";
            $data[2][1] = $data[2][1] . "<td>" . $row[1] . "</td>";
            $data[2][2] = intval($data[2][2]) + 1;
        }
        else if($row[0] == "Thursday")
        {
            $data[3][0] = $data[3][0] . "<td>" . $row[2] . "</td>";
            $data[3][1] = $data[3][1] . "<td>" . $row[1] . "</td>";
            $data[3][2] = intval($data[3][2]) + 1;
        }
        else if($row[0] == "Friday")
        {
            $data[4][0] = $data[4][0] . "<td>" . $row[2] . "</td>";
            $data[4][1] = $data[4][1] . "<td>" . $row[1] . "</td>";
            $data[4][2] = intval($data[4][2]) + 1;
        }

    }

    //Final Assigning
    $data[0][0] = $data[0][0] . "</tr>";
    $data[0][1] = $data[0][1] . "</tr>";

    $data[1][0] = $data[1][0] . "</tr>";
    $data[1][1] = $data[1][1] . "</tr>";

    $data[2][0] = $data[2][0] . "</tr>";
    $data[2][1] = $data[2][1] . "</tr>";

    $data[3][0] = $data[3][0] . "</tr>";
    $data[3][1] = $data[3][1] . "</tr>";

    $data[4][0] = $data[4][0] . "</tr>";
    $data[4][1] = $data[4][1] . "</tr>";


    // Display Result
    if($data[0][2] > 0)
        echo "<table border='1' cellspacing = 0 cellpadding = '4'>".$data[0][0] . $data[0][1] . "</table><br>";
    if($data[1][2] > 0)
        echo "<table border='1' cellspacing = 0 cellpadding = '4'>".$data[1][0] . $data[1][1] . "</table><br>";
    if($data[2][2] > 0)
        echo "<table border='1' cellspacing = 0 cellpadding = '4'>".$data[2][0] . $data[2][1] . "</table><br>";
    if($data[3][2] > 0)
        echo "<table border='1' cellspacing = 0 cellpadding = '4'>".$data[3][0] . $data[3][1] . "</table><br>";
    if($data[4][2] > 0)
        echo "<table border='1' cellspacing = 0 cellpadding = '4'>".$data[4][0] . $data[4][1] . "</table><br>";

?>