如何在从数据库获取输入的 php 中拆分表


How to split the table in php which is taking input from database?

我想把我的桌子分成两部分即每侧 15 行。这可能吗?我也试过jQuery了还是没用:(

$conn = new mysqli('localhost', '', '', 'test');
if ($conn->connect_error)
{
    die("Connection failed: " . $conn->connect_error);
}   
$sql="SELECT dateofattendance,timeofattendance, status,timeofdeparture FROM attendance Where emp='$name'";
$result = $conn->query($sql);           
if ($result->num_rows > 0)         
{
    echo "<table class='table table-condensed table-bordered table-hover'><thead> <tr><th> Date </th><th>IN</th><th>OUT</th><th>Status</th></tr></thead>";
    // output data of each row
    while($row = $result->fetch_assoc())
    {
        echo "<tbody><tr class='warning'><td>" . $row["dateofattendance"]. "</td><td>" . $row["timeofattendance"]. "</td><td>" . $row["timeofdeparture"]. "</td><td>"  . $row["status"]."</td></tr></tbody></table>";

您已有的代码会将数据输出到单个表中

if($counter<15)
{
    echo "<table id='my' class='table table-condensed table-bordered table-hover'><thead> <tr><th > Date </th><th>IN</th><th>OUT</th><th>Status</th></tr></thead>";             
    // This is the problem. The line below will not stop after 15 rows
    // it will run until all data is retrieved. Incrementing the counter
    // does not matter at this point because we're already inside the if statement
    while($row = $result->fetch_assoc())
    {
        // this is also wrong. you are declaring a new tbody element for each row
        // tbody should be declared once, with the table declaration   
        echo "<tbody><tr class='warning'><td>" . $row["dateofattendance"]. "</td><td>" . $row["timeofattendance"]. "</td><td>" . $row["timeofdeparture"]. "</td><td>"  . $row["status"]."</td></tr></tbody>";               
    }
    echo "</table>";
    $counter++;
}
else
{
    // also you can't give both tables an id of 'my'. ids must be unique
    echo "<table id='my' class='table table-condensed table-bordered table-hover'><thead> <tr><th > Date </th><th>IN</th><th>OUT</th><th>Status</th>                        </tr></thead>";             
    ...
}

if 语句需要位于 while 语句内。请尝试此代码。

// create wrapper table
echo "<table><tbody><tr></td>";  
// create the opening table
echo "<table id='my1' style='float:left;' class='table table-condensed table-bordered table-hover'><thead> <tr><th> Date </th><th>IN</th><th>OUT</th><th>Status</th></tr></thead><tbody>";             
while($row = $result->fetch_assoc())
{
    // create the row
    echo "<tr class='warning'><td>" . $row["dateofattendance"]. "</td><td>" . $row["timeofattendance"]. "</td><td>" . $row["timeofdeparture"]. "</td><td>"  . $row["status"]."</td></tr>"; 
    $counter++;  
    // when the counter is 15, close this table and open another
    if($counter == 15)
    {
        echo "</td><td>"; // move to the next cell in our wrap table
        echo "</tbody></table>";
        echo "<table id='my2' style='float:left;' class='table table-condensed table-bordered table-hover'><thead> <tr><th> Date </th><th>IN</th><th>OUT</th><th>Status</th></tr></thead><tbody>";  
    }            
}
// close the last table
echo "</tbody></table>";
// close our wrapper table
echo "</td></tr></tbody></table>";

这将输出两个表,每个表有 15 行,它们彼此并排浮动

想知道这是否有效

if ($result->num_rows > 0)         
{
    echo "<table class='table table-condensed table-bordered table-hover'><thead> <tr><th> Date </th><th>IN</th><th>OUT</th><th>Status</th></tr></thead><tbody>";
    // output data of each row
    while($row = $result->fetch_assoc())
    {
        echo "<tr class='warning'><td>" . $row["dateofattendance"]. "</td><td>" . $row["timeofattendance"]. "</td><td>" . $row["timeofdeparture"]. "</td><td>"  . $row["status"]."</td></tr>";
    }
    echo "</tbody></table>";
}

我认为问题出在您的第一个 while 条件中,它将解析所有获取的结果,第二个条件是无用的。

尝试用这个更新你的代码,它可能会起作用

if ($result->num_rows > 0)         
{          
    while($row = $result->fetch_assoc())
    {
        if($counter == 14)
        {
            echo "</tbody></table>";  
        }
        else if($counter ==0 || $counter == 14)
        {
            echo "<table id='my' class='table table-condensed table-bordered table-hover'><thead> <tr><th > Date </th><th>IN</th><th>OUT</th><th>Status</th></tr></thead><tbody>";                    
        }        
            echo "<tr class='warning'><td>" . $row["dateofattendance"]. "</td><td>" . $row["timeofattendance"]. "</td><td>" . $row["timeofdeparture"]. "</td><td>"  . $row["status"]."</td></tr>";                    
        $counter++;
    }
    echo "</table>";
}