如何在表格中显示所有记录?

时间:2018-11-30 02:31:06

标签: php mysql html-table

这是我的代码...

<fieldset>

<legend id="leg"> DAILY RECORD </legend>

    <table cellpadding="12" border="5">
        <tr>

            <td width="7%"> <p id="allrnd"> Record Date:<br><br> <?php echo "<p> $recdate </p>"?> </p> </td>
            <td width="8%"> <p id="allrnd"> Activity No.:<br><br><?php echo "<p> $actno </p>" ?> </p> </td>
            <td width="6%"> <p id="allrnd"> Bldg No.:<br><br><?php echo "<p>$bldgno </p>" ?> </p> </td>
            <td width="6%"> <p id="allrnd"> Floor No.:<br><br><?php echo "<p> $flrno </p>" ?> </p> </td>
            <td width="6%"> <p id="allrnd"> Unit No.:<br><br><?php echo "<p> $unino </p" ?> </p> </td>
            <td width="10%"> <p id="allrnd"> Type:<br><br><?php echo "<p> $type </p>" ?> </p> </td>
            <td width="10.5%"> <p id="allrnd"> Specifics:<br><br> <?php echo "<p> $specs </p> " ?> </p> </td>
            <td width="10%"> <p id="allrnd"> Materials Outgoing:<br><br><?php echo "<p> $mout </p>" ?> </p> </td>
            <td width="10%"> <p id="allrnd"> Materials Incoming:<br><br><?php echo "<p> $min </p>" ?> </p> </td>
            <td width="13%"> <p id="allrnd"> Requested By / Delivered By:<br><br><?php echo "<p> $rqstd </p>" ?> </p> </td>
            <td width="8%"> <p id="allrnd"> Remarks / Name:<br><br><?php echo "<p> $rem </p>" ?> </p> </td>
            <td width="5%"> <p id="allrnd"> DR No.:<br><br><?php echo "<p> $drno </p>" ?> </p> </td>
        </tr>           

    </table>

    <br>
    <br>


</fieldset>

我想在表中显示所有记录,但是,每列仅显示1条记录。

剩下的就是这里了

<?php

    $con = mysqli_connect("localhost", "root", "", "inventory");

    if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
    } 

    $sql = "SELECT recdate, actno, bldgno, flrno, unino, type, specs, mout, min , rqstd, drno, rem FROM scaffoldingrecs";
    $result = $con->query($sql);



    if ($result->num_rows >  0) {

    while($row = $result->fetch_assoc()) {



        $recdate = $row["recdate"];
        $actno = $row["actno"];
        $bldgno= $row["bldgno"];
        $flrno = $row["flrno"];
        $unino = $row["unino"];
        $type = $row["type"];
        $specs = $row["specs"];
        $mout = $row["mout"];
        $min = $row["min"];
        $rqstd = $row["rqstd"];
        $drno = $row["drno"];
        $rem = $row["rem"];



    }
        } else {
            echo "0 results";
    }

?>

1 个答案:

答案 0 :(得分:0)

<?php

    $con = mysqli_connect("localhost", "root", "", "inventory");

    if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
    } 

    $sql = "SELECT recdate, actno, bldgno, flrno, unino, type, specs, mout, min , rqstd, drno, rem FROM scaffoldingrecs";
    $result = $con->query($sql);



    if ($result->num_rows >  0) {
    $dataArr = [];
    while($row = $result->fetch_assoc()) {
        $temp=[];


        $temp['recdate'] = $row["recdate"];
        $temp['actno']   = $row["actno"];
        $temp['bldgno']  = $row["bldgno"];
        $temp['flrno']   = $row["flrno"];
        $temp['unino']   = $row["unino"];
        $temp['type']    = $row["type"];
        $temp['specs']   = $row["specs"];
        $temp['mout']    = $row["mout"];
        $temp['min']     = $row["min"];
        $temp['rqstd']   = $row["rqstd"];
        $temp['drno']    = $row["drno"];
        $temp['rem']     = $row["rem"];

        $dataArr[] = $temp;//adding current set of results

    }
        } else {
            echo "0 results";
    }

?>

要显示结果: 用以下代码替换问题中的第一段代码:

<fieldset>

<legend id="leg"> DAILY RECORD </legend>

    <table cellpadding="12" border="5">
        <?php if(isset($dataArr) && count($dataArr)){ 
        foreach($dataArr as $key => $row){

        $recdate = $row["recdate"];
        $actno = $row["actno"];
        $bldgno= $row["bldgno"];
        $flrno = $row["flrno"];
        $unino = $row["unino"];
        $type = $row["type"];
        $specs = $row["specs"];
        $mout = $row["mout"];
        $min = $row["min"];
        $rqstd = $row["rqstd"];
        $drno = $row["drno"];
        $rem = $row["rem"];

        ?>
        <tr>
            <td width="7%"> <p id="allrnd"> Record Date:<br><br> <?php echo "<p> $recdate </p>"?> </p> </td>
            <td width="8%"> <p id="allrnd"> Activity No.:<br><br><?php echo "<p> $actno </p>" ?> </p> </td>
            <td width="6%"> <p id="allrnd"> Bldg No.:<br><br><?php echo "<p>$bldgno </p>" ?> </p> </td>
            <td width="6%"> <p id="allrnd"> Floor No.:<br><br><?php echo "<p> $flrno </p>" ?> </p> </td>
            <td width="6%"> <p id="allrnd"> Unit No.:<br><br><?php echo "<p> $unino </p" ?> </p> </td>
            <td width="10%"> <p id="allrnd"> Type:<br><br><?php echo "<p> $type </p>" ?> </p> </td>
            <td width="10.5%"> <p id="allrnd"> Specifics:<br><br> <?php echo "<p> $specs </p> " ?> </p> </td>
            <td width="10%"> <p id="allrnd"> Materials Outgoing:<br><br><?php echo "<p> $mout </p>" ?> </p> </td>
            <td width="10%"> <p id="allrnd"> Materials Incoming:<br><br><?php echo "<p> $min </p>" ?> </p> </td>
            <td width="13%"> <p id="allrnd"> Requested By / Delivered By:<br><br><?php echo "<p> $rqstd </p>" ?> </p> </td>
            <td width="8%"> <p id="allrnd"> Remarks / Name:<br><br><?php echo "<p> $rem </p>" ?> </p> </td>
            <td width="5%"> <p id="allrnd"> DR No.:<br><br><?php echo "<p> $drno </p>" ?> </p> </td>
        </tr>           
        <?php } else { ?>    
        <tr> No data found. </tr>
        <?php } ?>
    </table>

    <br>
    <br>


</fieldset>