duanfu9523 2018-02-05 11:10
浏览 54
已采纳

PHP函数循环SQL结果并生成HTML表

I want to write an php function that will echo sql query results in a specific manner.

Example I have table 1 which is 10.000 rows * 43 columns:

NO NAME Prof Date of birth 1 John Teacher 1987 2 Nick Engineer 1976 3 4 5

And so on. Based on No which is an integer (1-10.000), I want to generate tables such as:

Name: John Prof: Teacher Date of birth: 1987

Name: Nick Prof: Engineer Date of birth: 1976

So far my code:

`

    $hostname = "";     
    $username = "";
    $password = "";
    $dbname = "db1";

    //connection to the database
    $con = mysqli_connect($hostname, $username, $password, $dbname)
        or die("Unable to connect to MySQL");
    mysqli_set_charset($con, 'utf8');
    /*echo "Connected to db1 database <br>";
    else
    echo "Could not connect"; */

    $query1 = "SELECT * FROM `table 1` WHERE CODE_NO = 1";
    $query2 = "SHOW COLUMNS FROM table 1";
    $result1 = mysqli_query($con, $query1);
    $result2 = mysqli_query($con, $query2);

    // Check result
    // Useful for debugging.
     if (!$result1) {
        $message  = 'Invalid query: ' . mysqli_error($con) . "
";
        $message .= 'Whole query: ' . $query1;
        die($message);
    }

    echo "<table>"; // table tag in the HTML
        while($row = mysqli_fetch_array($result1))
        //Creates a loop to loop through results
        {
        echo

        "<tr>
            <th>CODE_NO:</th>
            <td>" . $row['CODE_NO'] . "</td>
        </tr>
        <tr>
            <th>FIRST_NAME:</th>
            <td>" . $row['FIRST_NAME'] . "</td>
        </tr>
        <tr>
            <th>SURNAME:</th>
            <td>" . $row['SURNAME'] . "</td>
        </tr>
        <tr>
            <th>Date of birth:</th>
            <td>" . $row['DOB'] . "</td>
        </tr>
        <tr>
            <th>Date of death:</th>
            <td> " . $row['DOD'] . "</td>
        </tr>";
        }
    echo "</table>"; //Close the table in HTML

    ?>`

Is it possbile to code the process once, without hard-coding anything, so it could be repeated as many times as needed?

Edit:

$x = 1; $query = "SELECT * FROMpersonsWHERE CODE_NO = '$x'"; $result = mysqli_query($con, $query);

  • 写回答

2条回答 默认 最新

  • doutao5499 2018-02-05 11:32
    关注

    If I understood you correctly, this would do what you need:

    function generateTableFromResult($result) {
       $html = "<table>";
       while($row = mysqli_fetch_array($result)) {
          foreach($row as $column => $value) {
            $html.="<tr><th>".$column."</th><td>".$value."</td></tr>";
          }
       }
       $html.="</table>";
       return $html;
    }
    
    // usage:
    // ...
    $result1 = mysqli_query($con, $query1);
    echo generateTableFromResult($result1);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题