<legend id='zznEl'><style id='zznEl'><dir id='zznEl'><q id='zznEl'></q></dir></style></legend>
      <tfoot id='zznEl'></tfoot>

      <small id='zznEl'></small><noframes id='zznEl'>

        <bdo id='zznEl'></bdo><ul id='zznEl'></ul>

      1. <i id='zznEl'><tr id='zznEl'><dt id='zznEl'><q id='zznEl'><span id='zznEl'><b id='zznEl'><form id='zznEl'><ins id='zznEl'></ins><ul id='zznEl'></ul><sub id='zznEl'></sub></form><legend id='zznEl'></legend><bdo id='zznEl'><pre id='zznEl'><center id='zznEl'></center></pre></bdo></b><th id='zznEl'></th></span></q></dt></tr></i><div id='zznEl'><tfoot id='zznEl'></tfoot><dl id='zznEl'><fieldset id='zznEl'></fieldset></dl></div>
      2. PHP:以五个为一组显示数据库中的条目?

        时间:2023-09-20
        • <legend id='G7t7h'><style id='G7t7h'><dir id='G7t7h'><q id='G7t7h'></q></dir></style></legend>
        • <small id='G7t7h'></small><noframes id='G7t7h'>

          <tfoot id='G7t7h'></tfoot>
                <tbody id='G7t7h'></tbody>

            • <i id='G7t7h'><tr id='G7t7h'><dt id='G7t7h'><q id='G7t7h'><span id='G7t7h'><b id='G7t7h'><form id='G7t7h'><ins id='G7t7h'></ins><ul id='G7t7h'></ul><sub id='G7t7h'></sub></form><legend id='G7t7h'></legend><bdo id='G7t7h'><pre id='G7t7h'><center id='G7t7h'></center></pre></bdo></b><th id='G7t7h'></th></span></q></dt></tr></i><div id='G7t7h'><tfoot id='G7t7h'></tfoot><dl id='G7t7h'><fieldset id='G7t7h'></fieldset></dl></div>
                <bdo id='G7t7h'></bdo><ul id='G7t7h'></ul>

                1. 本文介绍了PHP:以五个为一组显示数据库中的条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  含义:例如,我的数据库中总共有 15 条记录,然后我想像这样显示我的数据:

                  Record[1], Record[2], Record[3], Record[4], Record[5]

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

                  <div class="11-15">Record[11], Record[12], Record[13], Record[14], Record[15]

                  我不确定是否可以使用 SQL 语句来完成,或者我必须编写某种do...while"或循环来检索每组数据.我也想过一些关于数组的事情,但没有得到结果.

                  谢谢

                  解决方案

                  我发现 array_chunk() 对这种事情非常有用.

                  //将所有记录拉入一个数组$query = mysql_query('SELECT * FROM mytable');$rows = array();而 ($row = mysql_fetch_array($query)) {$rows[] = $row;}//这将一个数组变成一个数组数组,其中每个子数组是//5 个原始条目$groups = array_chunk($rows, 5);//一个接一个地处理每一组$开始= 1;foreach ($groups as $group) {$end = $start + 4;//$group 是一组 5 行.按要求处理$content = implode(', ', $group);回声<<<END<div class="$start-$end">$content</div>结尾;$开始 += 5;}

                  您当然可以不先阅读所有内容而执行此操作,但是如果您无论如何都要阅读所有内容,则没有太大区别,并且上述版本可能比实现适当的中断条件更具可读性(s) 当您从数据库中读取行时.

                  Is it possible and if so, how can I do it, to select all entries in a table in my database and then display five results at the time in one group.

                  Meaning: A example is that I've 15 records total in my database, then I want to present my data like this:

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

                  I'm not completely sure if I can do it with an SQL statement or I've to write some sort of "do...while" or loop to retrieve each set of data. I've also thought about something with arrays but haven't got up with a result.

                  Thanks

                  解决方案

                  I find array_chunk() to be pretty useful for this kind of thing.

                  // 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;
                  }
                  

                  You can of course do this without reading them all in first but if you're going to read them all anyway, it doesn't make much difference and the above version will probably be far more readable than implementing the appropriate break condition(s) as you read the rows from the DB.

                  这篇关于PHP:以五个为一组显示数据库中的条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PHP 错误:ob_flush() [ref.outcontrol]:未能刷新缓冲区.没 下一篇:Mysql 取数组、表结果

                  相关文章

                  最新文章

                2. <tfoot id='rxNoI'></tfoot>
                      <bdo id='rxNoI'></bdo><ul id='rxNoI'></ul>
                    <legend id='rxNoI'><style id='rxNoI'><dir id='rxNoI'><q id='rxNoI'></q></dir></style></legend>

                      <small id='rxNoI'></small><noframes id='rxNoI'>

                      <i id='rxNoI'><tr id='rxNoI'><dt id='rxNoI'><q id='rxNoI'><span id='rxNoI'><b id='rxNoI'><form id='rxNoI'><ins id='rxNoI'></ins><ul id='rxNoI'></ul><sub id='rxNoI'></sub></form><legend id='rxNoI'></legend><bdo id='rxNoI'><pre id='rxNoI'><center id='rxNoI'></center></pre></bdo></b><th id='rxNoI'></th></span></q></dt></tr></i><div id='rxNoI'><tfoot id='rxNoI'></tfoot><dl id='rxNoI'><fieldset id='rxNoI'></fieldset></dl></div>