这是我的代码,用于检索数据并希望将它们显示在表中。具体来说,每行应该只包含两个带数据的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 |
| |
=============
答案 0 :(得分:0)
请尝试使用此代码:
$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++;
}
}
答案 1 :(得分:0)
第二个条件应与 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++;
}
}