简体   繁体   English

PHP:如果计数器为奇数,则为备用表列

[英]PHP : Alternate Table Column if counter is odd

I am generating something close to a contact list. 我正在生成接近联系人列表的内容。 I want my contacts to be listed next to each other depending on their position. 我希望我的联系人根据位置而彼此相邻。

Example: 例:

Contacts 联络人

pos 0 | 位置0 | Pos 1 位置1

pos 2 | 位置2 | pos 3 位置3

I have manage to get the results into table but for some reason they are loading this way Screenshot , with test1 being item at position 0, I want Test Test to be in the left column and Test2 Test2 to be in the right column. 我设法将结果放入表中,但是由于某种原因,它们以这种方式加载屏幕快照 ,而test1在位置0处,我希望Test Test在左列,而Test2 Test2在右列。

My Code: 我的代码:

<?php
$connection = connectToSql();
$query = "SELECT * FROM visitorsystem.employee";
$result = mysqli_query($connection,$query)
          or die("Error in query: ". mysqli_error($connection));
?>
<form action="#" method="POST"> 
    <table class="table" style = "margin-top:50px">
        <?php 
        $i=0;
        while ($row = mysqli_fetch_assoc($result)){ 
            echo "$i";
            if($i % 2 == 0  &&  $row != count($result)){ 
                ?>
                <tr>
                    <td>
                        <button type="button" class="btn btn-link"  style="width:100%;">
                            <h4> <?php echo "$row[name]". " " . "$row[surname]";?> </h4>
                        </button>
                    </td>
                    <?php
            }else
                ?> 
                <td>
                    <button type="button" class="btn btn-link"  style="width:100%;">
                        <h4> <?php echo "$row[name]". " " . "$row[surname]";?> </h4>
                    </button>
                </td>
            </tr>
            <?php
            $i++;
        }
        ?>
echo '<tr>';
while ($row = mysqli_fetch_assoc($result)){ 
    ?>
    <td>
        <button type="button" class="btn btn-link"  style="width:100%;">
            <h4> <?php echo "$row[name]". " " . "$row[surname]";?> </h4>
        </button>
    </td>
    <?php
    $i++;
    if($i % 2 == 0){
        echo '</tr><tr>';
    }
}
echo '</tr>';

Always echo the same <td> , and change row every 2 entries. 始终回显相同的<td> ,并每2个条目更改行。

Don't use PHP for this, use CSS, something like this. 不要为此使用PHP,而应使用CSS之类的东西。

tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}

Simply using the modulus to generate the table structure is not a reliable method considering the possibility of an odd count in the resultset. 考虑到结果集中出现奇数计数的可能性,仅使用模数来生成表结构并不是一种可靠的方法。 You need some way of completing the row when there is insufficient data. 当数据不足时,您需要一些完成行的方法。

Here is one option via my own sample data that will "square up" the table whether the data is even or odd: ( PHP Demo ) 这是通过我自己的示例数据提供的一个选项,无论数据是偶数还是奇数,它都会“摆正”表:( PHP Demo

$rows=[
    ['name'=>'Bob','surname'=>'Evans'],
    ['name'=>'John','surname'=>'Doe'],
    ['name'=>'Sue','surname'=>'Heck'],
    ['name'=>'Kevin','surname'=>'James'],    
    ['name'=>'James','surname'=>'Brown']
];
$newtr=true;
foreach($rows as $row){
    if($newtr){echo "<tr>";}  // start a new row
    echo "<td>";
        echo "<button type=\"button\" class=\"btn btn-link\" style=\"width:100%;\">";
            echo "<h4> {$row['name']} {$row['surname']} </h4>";
        echo "</button>";
    echo "</td>";
    if(!$newtr){echo "</tr>";}  // end a row
    $newtr=!$newtr;  // toggle value
}
if(!$newtr){echo "<td></td></tr>";} // if odd count in resultset, add a phantom cell and end row

Output: 输出:

<tr>
    <td>
        <button type="button" class="btn btn-link" style="width:100%;">
            <h4> Bob Evans </h4>
        </button>
    </td>
    <td>
        <button type="button" class="btn btn-link" style="width:100%;">
            <h4> John Doe </h4>
        </button>
    </td>
</tr>
<tr>
    <td>
        <button type="button" class="btn btn-link" style="width:100%;">
            <h4> Sue Heck </h4>
        </button>
    </td>
    <td>
        <button type="button" class="btn btn-link" style="width:100%;">
            <h4> Kevin James </h4>
        </button>
    </td>
</tr>
<tr>
    <td>
        <button type="button" class="btn btn-link" style="width:100%;">
            <h4> James Brown </h4>
        </button>
    </td>
    <td></td>
</tr>

If you particularly want to use a modulus technique, this will function the same as above: (assuming there is at least one row in the resultset) 如果您特别想使用模数技术,则其功能将与上述相同:(假设结果集中至少有一行)

Code: ( Demo ) 代码:( 演示

$i=0;
echo "<tr>";
foreach($rows as $row){
    if($i>0 && $i%2==0){echo "</tr><tr>";}
    echo "<td>";
        echo "<button type=\"button\" class=\"btn btn-link\" style=\"width:100%;\">";
            echo "<h4> {$row['name']} {$row['surname']} </h4>";
        echo "</button>";
    echo "</td>";
    ++$i;
}
if($i%2){echo "<td></td>";} // if odd count in resultset, add a phantom cell and end row
echo '</tr>';

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

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