PHP循环从记录编号开始
我有两张桌子。一个飘向左边,一个飘向右边。每个表包含8行,我希望显示其中的查询结果。我希望左侧的表显示前8条记录,右侧的表显示以下8条记录。现在我一直在想如何让右边的桌子显示8-16号记录 这是我在左边桌子上的循环:PHP循环从记录编号开始,php,html,mysql,Php,Html,Mysql,我有两张桌子。一个飘向左边,一个飘向右边。每个表包含8行,我希望显示其中的查询结果。我希望左侧的表显示前8条记录,右侧的表显示以下8条记录。现在我一直在想如何让右边的桌子显示8-16号记录 这是我在左边桌子上的循环: <?php do { ?> <tr> <td></td> <td><?php echo $row_rsSystems['systemName']; ?></td
<?php do { ?>
<tr>
<td></td>
<td><?php echo $row_rsSystems['systemName']; ?></td>
</tr>
<?php } while ($row_rsSystems = mysql_fetch_assoc($rsSystems)); ?>
嗯,不确定您的表是什么样子的,但您只需执行两个查询,然后在两个查询中都添加一个
限制
第一个:限制0,8
第二个:限制8,8
可能不是最好的方法,但它很有效。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
}
$results=mysql\u查询($query,$connection);
$i=0;
$skipahead=8//跳到第8条记录:
而($result=mysql_fetch_assoc($results)){
如果($i),则应采用以下方法:
<?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
如果只需要显示16个条目(使用LIMIT
关键字),并且如果需要显示下一批(例如,16-32),那么查询应该只获取16个条目,并在查询中包括该参数(关键字OFFSET
)
对于提到的关键字。首先,您可能想使用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++;
}
$i=0;
而($row\rsSystems=mysql\u fetch\u assoc($rsSystems)){
如果($i<8){
//左边桌子的东西
$right_table=数组(…);
}否则{
//摆对桌子的东西
$left_table=数组(…);
}
$i++;
}
这样,为了获得最佳效果,您可以一次性获得所有数据。尝试以下方法:
<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>
如果您先将所有行推入PHP数组,然后在所有数据都以索引可访问格式后执行代码生成表,会怎么样?因为您使用的是两个表。您可以使用两个查询,并使用限制和偏移来筛选数据库结果。第一个不应该是0,4
,第二个不应该是4,4
?No、 他想要0-8
然后8-16
。我想知道如何在while循环中开始和结束这两个表:)我必须为我仓促的回答道歉。我的意思是首先使用两个不同的数组临时存储数据,然后分别使用这些数组创建表。