简体   繁体   中英

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 | Pos 1

pos 2 | pos 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.

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.

Don't use PHP for this, use CSS, something like this.

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 )

$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>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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