PHP:五人一组显示数据库中的条目?

PHP:五人一组显示数据库中的条目?,php,sql,database,loops,while-loop,Php,Sql,Database,Loops,While Loop,有没有可能,如果有,我该如何做,选择数据库中一个表中的所有条目,然后在一个组中同时显示五个结果 意思:一个例子是,我的数据库中总共有15条记录,然后我想这样表示我的数据: <div class="1-5">Record[1], Record[2], Record[3], Record[4], Record[5]</div> <div class="6-10">Record[6], Record[7], Record[8], Record[9], Record

有没有可能,如果有,我该如何做,选择数据库中一个表中的所有条目,然后在一个组中同时显示五个结果

意思:一个例子是,我的数据库中总共有15条记录,然后我想这样表示我的数据:

<div class="1-5">Record[1], Record[2], Record[3], Record[4], Record[5]</div>

<div class="6-10">Record[6], Record[7], Record[8], Record[9], Record[10]</div>

<div class="11-15">Record[11], Record[12], Record[13], Record[14], Record[15]</div>
记录[1],记录[2],记录[3],记录[4],记录[5]
记录[6],记录[7],记录[8],记录[9],记录[10]
记录[11],记录[12],记录[13],记录[14],记录[15]
我不完全确定我是否可以用SQL语句来实现,或者我必须编写某种“do…while”或循环来检索每组数据。我也考虑过使用数组,但还没有得到结果

谢谢

  • 梅斯蒂卡
我发现它对这类事情非常有用

// pull all the records into an array
$query = mysql_query('SELECT * FROM mytable');
$rows = array();
while ($row = mysql_fetch_array($query)) {
  $rows[] = $row;
}

// this turns an array into an array of arrays where each sub-array is
// 5 entries from the original
$groups = array_chunk($rows, 5);

// process each group one after the other
$start = 1;
foreach ($groups as $group) {
  $end = $start + 4;

  // $group is a group of 5 rows. process as required
  $content = implode(', ', $group);

  echo <<<END
<div class="$start-$end">$content</div>

END;
  $start += 5;
}
//将所有记录拉入一个数组
$query=mysql_query('SELECT*fromtable');
$rows=array();
while($row=mysql\u fetch\u array($query)){
$rows[]=$row;
}
//这会将一个数组转换为一组数组,其中每个子数组
//5份原件
$groups=array_chunk($rows,5);
//逐个处理每组
$start=1;
foreach($组作为$组){
$end=$start+4;
//$group是一个由5行组成的组。根据需要进行处理
$content=内爆(“,”,$group);

echo不知道我是否正确理解了问题,但如果你想将所有结果分成5组:


$i =1;    
while ($row = mysql_fetch_array($query)) {
 echo $row['name']."\n";
 if ($i % 5 == 0)
 {
   echo 'hr'; // Or any other separator you want
 }
 $i++;
}

这个答案很好,是一个快捷的解决方案…谢谢你,伙计