在这里,我的代码用于检索数据,并希望将其显示在表中。具体来说,每行应仅包含两个带有数据的td。

//seelct forklft query
    $select_part_query = "SELECT DISTINCT part_code, part_desc, part_size, oem, part_image FROM tblpartDetails WHERE part_code LIKE '%$txtSearch%' OR part_desc LIKE '%$txtSearch%' OR part_size LIKE '%$txtSearch%' OR oem LIKE '%$txtSearch%'";

    $result1 = mysqli_query ($mydatabase, $select_part_query);
                if($result1)
                {
                    $counter1 = 1;
                    while($info1 = mysqli_fetch_array( $result1 ))
                    {
                        if( $counter1 == 1 )
                        {
                            echo "<tr>";
                        }
                            echo '<td style="width:460px; padding:15px;">';
                                echo "<div style='width:430px; height:210px; padding:10px; background-color:rgba(190, 190, 190, 0.8);box-shadow: 12px 0 15px -4px rgba(255, 165, 0, 0.8), -12px 0 8px -4px rgba(255, 165, 0, 0.8);'>";
                                    echo "<div style='float:left'>";
                                        echo "<img src=".$info1['part_image'] ." width=210px height=210px ></img>";
                                    echo "</div>";

                                    echo "<div style='width:220px; height:200px; margin-left:210px; padding-left:10px'>";
                                        echo "<p class='txtCode' title='Part'>".$info1['part_code']."</p>";
                                        echo "<p>".$info1['part_desc']."</p>";
                                        echo "<div id='btnView' style='width:100px; margin-top:100px; margin-left:120px;'>";
                                            echo "<img src='images/view-detail.png' width='100px' height='40px'/>";
                                        echo "</div>";
                                    echo "</div>";
                                echo "</div>";
                            echo "</td>";
                        if( $counter1 >= 2)
                        {
                             echo"</tr>";
                        }else{

                        $counter1++;
                        }
                    }
                }


该过程的结果应为:

=============     =============
|           |     |           |
|   Item 1  |     |   Item 2  |
|           |     |           |
=============     =============

=============     =============
|           |     |           |
|   Item 3  |     |   Item 4  |
|           |     |           |
=============     =============

=============
|           |
|   Item 5  |
|           |
=============

=============
|           |
|   Item 6  |
|           |
=============

=============
|           |
|   Item 7  |
|           |
=============

最佳答案

第二个条件应与if($ counter1%2 == 1)进行比较

$result1 = mysqli_query ($mydatabase, $select_part_query);
            if($result1)
            {
                $counter1 = 0;
                while($info1 = mysqli_fetch_array( $result1 ))
                {
                    if( $counter1%2==0 )
                    {
                        echo "<tr>";
                    }
                        echo '<td style="width:460px; padding:15px;">';
                            echo "<div style='width:430px; height:210px; padding:10px; background-color:rgba(190, 190, 190, 0.8);box-shadow: 12px 0 15px -4px rgba(255, 165, 0, 0.8), -12px 0 8px -4px rgba(255, 165, 0, 0.8);'>";
                                echo "<div style='float:left'>";
                                    echo "<img src=".$info1['part_image'] ." width=210px height=210px ></img>";
                                echo "</div>";

                                echo "<div style='width:220px; height:200px; margin-left:210px; padding-left:10px'>";
                                    echo "<p class='txtCode' title='Part'>".$info1['part_code']."</p>";
                                    echo "<p>".$info1['part_desc']."</p>";
                                    echo "<div id='btnView' style='width:100px; margin-top:100px; margin-left:120px;'>";
                                        echo "<img src='images/view-detail.png' width='100px' height='40px'/>";
                                    echo "</div>";
                                echo "</div>";
                            echo "</div>";
                        echo "</td>";
                    if( $counter1%2==1)
                    {
                         echo "</tr>";
                    }
                    $counter1++;

                }
            }

关于php - 每行显示两个td,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23260210/

10-13 00:28