0

我有 2 张桌子。一个向左飘,一个向右飘。每个表包含 8 行我想显示我的查询结果。我希望左侧的表格显示前 8 条记录,右侧的表格显示以下 8 条记录。现在我正试图弄清楚如何让右侧的表格显示记录号 8 - 16 .

这是我在左边桌子上的循环:

<?php do { ?>
      <tr>
        <td></td>
        <td><?php echo $row_rsSystems['systemName']; ?></td>
      </tr>
      <?php } while ($row_rsSystems = mysql_fetch_assoc($rsSystems)); ?>
4

5 回答 5

1

尝试这个:

    <table>

        <?php

        $i = 0;
        while ( $row = mysql_fetch_array($result) ){
            if ($i % 2 == 0){
                echo '<tr>';
            }
            echo '<td>'.$row['systemName'].'</td>';
            if ($i % 2 == 8){
                echo '</tr>';
            }
            $i++; 
        }

        //here is a check in case you don't have multiple of 3 rows
        if ($i % 8 != 0){
            echo '</tr>';
        }

        ?>

        </table>
于 2013-01-14T19:15:06.570 回答
1

好吧,不确定你的表看起来如何,但你可以做两个查询并添加一个LIMIT

第一:LIMIT 0, 8

第二个:LIMIT 8, 8

可能不是最好的方法,但它确实有效。

于 2013-01-14T18:37:13.120 回答
0

Do/While 处理 mysql 结果的方法可能不是必需的;改用以下内容:

$results = mysql_query($query, $connection);
while ($result = mysql_fetch_assoc($results)) { 
    echo $result['systemName'];
}

至于跳到某个记录,您可以使用迭代器执行此操作:

$results = mysql_query($query, $connection);

$i = 0;
$skipahead = 8; //skip to the 8th record:


while ($result = mysql_fetch_assoc($results)) { 
    if($i<$skipahead) continue; //this performs the skip
    echo $result['systemName'];
    $i++; //increment
}
于 2013-01-14T18:38:14.053 回答
0

首先,您可能想使用while(){}而不是do{...}while();.
为了将数据分成两个表,我建议您使用两个临时数组来存储数据,然后使用它们来创建所需的表。像这样的东西:

$i = 0;
while($row_rsSystems = mysql_fetch_assoc($rsSystems)){
    if($i < 8){
       //stuff for the left table
       $right_table = array(...);
    }else{
       //stuff for the right table
       $left_table = array(...);
    }
    $i++;
}

这样一来,您就可以一次性获得所有数据以达到最佳效果。

于 2013-01-14T18:42:51.430 回答
0

以下应该有效:

<?php $i = 0; ?>

对于每个表:

 <?php while ($row_rsSystems = mysql_fetch_assoc($rsSystems) && (++$i % 8)) {?>
      <tr>
        <td></td>
        <td><?php echo $row_rsSystems['systemName']; ?></td>
      </tr>
 <?php } ?>

执行上述功能以避免 c&p。

LIMIT如果您只需要显示 16 个条目(使用关键字),并且如果您需要显示下一批(例如,16-32),则您的查询应该只获取 16 个条目,并且在您的查询中包含该参数(关键字OFFSET)。

mysql SELECT 文档中提到的关键字。

于 2013-01-14T18:42:14.940 回答