显示左侧数据库中的一条记录,右侧显示下一条记录

时间:2016-06-02 11:49:25

标签: html

我将我的页面拆分为两部分,我希望从左到右显示数据库显示的结果。目前,左侧和右侧显示相同的记录。怎么办,左边会显示第一条记录,右边会显示第一条记录,等等?

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

3 个答案:

答案 0 :(得分:1)

你也可以这样做

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

答案 1 :(得分:0)

首先准备数据,而不是显示:

$rows = [];
$i = 1; // use $k + 1 if it's indexed array

foreach ( $result as $k => $v )   {
    $key = $i % 2 == 0 ? 'right' : 'left';
    $rows[] = [
        $key => [
            'name' => stripslashes($v->company_name),
            'opis' => stripslashes($v->company_desc),
            'mob_nr' => stripslashes($v->tel),
            'nip' => stripslashes($v->company_nip),
        ];
    ];

    $i++;
}

echo '<table>';

foreach ($rows as $row) {
    echo "<tr>";
    echo "<td>{$row['left']['name']}</td>";
    echo "<td>{$row['right']['name']}</td>";
    echo "</tr>";

    echo "<tr>";
    echo "<td>{$row['left']['nip']}</td>";
    echo "<td>{$row['right']['nip']}</td>";
    echo "</tr>";

    echo "<tr>";
    echo "<td>{$row['left']['mob_nr']}</td>";
    echo "<td>{$row['right']['mob_nr']}</td>";
    echo "</tr>";

    echo "<tr>";
    echo "<td>{$row['left']['opis']}</td>";
    echo "<td>{$row['right']['opis']}</td>";
    echo "</tr>";
}

echo '</table>';

答案 2 :(得分:0)

每次获取一条记录,然后你实际编码它以回显两次相同的变量。您应该让for each完成并构建一个可以循环的数组,索引每个循环递增2。

<?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()");
$customers = array();
x = 0; 
foreach ( $result as $k => $v )   {
    $customers[x].c_name = stripslashes($v->company_name);
    $customers[x].c_opis = stripslashes($v->company_desc);
    $customers[x].c_mob_nr = stripslashes($v->tel);
    $customers[x].c_nip = stripslashes($v->company_nip); 
    x++;
}

for(i=0;i<sizeof($customer);i=i+2){ 
// lets drop to back html for sanity 
?>
<table>
<tbody>
    <tr style="height: 50%;">
        <td style="width: 50%;"><?php echo $customer[i].c_name; ?></td>
        <td style="width: 50%;"><?php echo $customer[i+1].c_name; ?></td>
    </tr>
    <tr style="height: 50%;">
        <td style="width: 50%;"><?php echo $customer[i].c_nip ?></td>
        <td style="width: 50%;"><?php echo $customer[i+1].c_nip ?></td>
    </tr>
     <tr style="height: 50%;">
        <td style="width: 50%;"><?php echo $customer[i].c_mob_nr ?></td>
        <td style="width: 50%;"><?php echo $customer[i+1].c_mob_nr?></td>
    </tr>
     <tr style="height: 50%;">
        <td style="width: 50%;"><?php echo $customer[i].c_opis ?></td>
        <td style="width: 50%;"><?php echo $customer[i+1].c_opis ?></td>
    </tr>
</tbody>
</table>
<?php 
} //end for loop
?>