我将我的页面分成两部分,我想从左到右显示数据库显示的结果.目前,左侧和右侧显示相同的记录.怎么办,左侧会显示第一条记录,右侧会显示下一条记录,依此类推?
$result = $wpdb->get_results("SELECT company_province, company_district, company_city, company_name, tel, company_nip, company_desc FROM test WHERE company_province = '$woj' AND company_district = '$pow' AND company_city = '$nazwa' ORDER BY RAND()");
foreach ( $result as $k => $v ) {
$c_name = stripslashes($v->company_name);
$c_opis = stripslashes($v->company_desc);
$c_mob_nr = stripslashes($v->tel);
$c_nip = stripslashes($v->company_nip);
?>
<table>
<tbody>
<tr style="height: 50%;">
<td style="width: 50%;"><?php echo $c_name; ?></td>
<td style="width: 50%;"><?php echo $c_name; ?></td>
</tr>
<tr style="height: 50%;">
<td style="width: 50%;"><?php echo $c_nip ?></td>
<td style="width: 50%;"><?php echo $c_nip ?></td>
</tr>
<tr style="height: 50%;">
<td style="width: 50%;"><?php echo $c_mob_nr ?></td>
<td style="width: 50%;"><?php echo $c_mob_nr ?></td>
</tr>
<tr style="height: 50%;">
<td style="width: 50%;"><?php echo $c_opis ?></td>
<td style="width: 50%;"><?php echo $c_opis ?></td>
</tr>
</tbody>
</table>
最佳答案 你也可以这样做
<table>
<tbody>
<?php
$result = $wpdb->get_results("SELECT company_province, company_district, company_city, company_name, tel, company_nip, company_desc FROM test WHERE company_province = '$woj' AND company_district = '$pow' AND company_city = '$nazwa' ORDER BY RAND()");
for ($i=0; $i <count($result); $i++) {
$c_name = stripslashes($result[$i]->company_name);
$c_opis = stripslashes($result[$i]->company_desc);
$c_mob_nr = stripslashes($result[$i]->tel);
$c_nip = stripslashes($result[$i]->company_nip);
$i++
if ( ($i) < count($result)) {
$c_name2 = stripslashes($result[$i]->company_name);
$c_opis2 = stripslashes($result[$i]->company_desc);
$c_mob_nr2 = stripslashes($result[$i]->tel);
$c_nip2 = stripslashes($result[$i]->company_nip);
} else {
$c_name2 = '';
$c_opis2 = '';
$c_mob_nr2 = '';
$c_nip2 = '';
}
echo '<tr style="height: 50%;">
<td style="width: 50%;">' . $c_name .'</td>
<td style="width: 50%;">' . $c_name2 .'</td>
</tr>
<tr style="height: 50%;">
<td style="width: 50%;">' . $c_nip .'</td>
<td style="width: 50%;">' . $c_nip2 .'</td>
</tr>
<tr style="height: 50%;">
<td style="width: 50%;">' . $c_mob_nr .'</td>
<td style="width: 50%;">' . $c_mob_nr2 .'</td>
</tr>
<tr style="height: 50%;">
<td style="width: 50%;">' . $c_opis.'</td>
<td style="width: 50%;">' . $c_opis2 .'</td>
</tr> ':
}
?>
</tbody>
</table>