简体   繁体   中英

Display two td per row

Here my code for retrieve data and want to display them into a table. As specific where each row should only contain two td with the data.

//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++;
                        }
                    }
                }

The result of this process should like this:

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

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

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

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

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

Please try with this code:

$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 = 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==0)
                        {
                             echo "</tr>";
                        }
                        $counter1++;

                    }
                }

The second condition should be compared with 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++;

                }
            }

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