dongshang6790 2014-05-19 16:29
浏览 207
已采纳

在php中一次取10个数组元素?

       echo "<table title='mxit:table:full' style='width: 100%' width='100%'><colgroup span='2' width='50%'></colgroup>";




        foreach($arr['chart_data'] as $key => $element){



          echo "<tr>";


            foreach($element as $subkey => $subelement){

              // $subelement =chop($subelement,'DIRECTSegment');

                 if($subkey++ < 2) {
                if($key == 0)
                {

               echo "<td align='center;' style='color:white;'>$subelement</td>";
                }

                else if($subkey == 1)
                {
                    echo "<td align='center;' style='color:white;'>$subelement</td>";   
                }

                else
                {

                echo "<td align='center;' style='color:white;'><a href='getdata.php?key=$key'>".$subelement."</a></td>";
                }
                 }

            }

              }
           echo "</tr>";

       echo "</table>";

How do i take only 10 elements of my array $arr['chart_data'] at a time ?

  • 写回答

2条回答 默认 最新

  • douxue7196 2014-05-20 06:14
    关注

    Anyway for all those who gave me negative votes and for my own betterment i solved my issue:

    Here is what i did:

     $totalfiles = count($arr['chart_data']);
      $limit = 10;
      $pages = ceil($totalfiles / $limit);
    
    
      if (!isset($_GET['startrow']) or!is_numeric($_GET['startrow'])) {
          //we give the value of the starting row to 0 because nothing was found in URL
          $startrow = 0;
          //otherwise we take the value from the URL
      } else {
          $startrow = (int) $_GET['startrow'];
      }
    
      echo "<table title='mxit:table:full' style='width: 100%' width='100%'><colgroup span='2' width='50%'></colgroup>";
    
    
      $data = array_slice($arr['chart_data'], $startrow, 10); // same as offset 0 limit 50 in sql
    
      foreach($data as $key => $element) {
    
    
    
          echo "<tr>";
    
    
          foreach($element as $subkey => $subelement) {
    
              $subelement = chop($subelement, 'DIRECTSegment');
    
              if ($subkey++ < 2) {
                  if ($key == 0 && $startrow == 0) {
    
                      echo "<td align='center;' style='color:white;'>$subelement</td>";
                  } else if ($subkey == 1) {
                      echo "<td align='center;' style='color:white;'>$subelement</td>";
                  } else {
    
                      echo "<td align='center;' style='color:white;'><a href='getdata.php?key=$key'>".$subelement.
                      "</a></td>";
                  }
              }
    
          }
          echo "</tr>";
    
      }
    
    
      echo "</table>";
    
    
    
    
      /* free result set */
      $result - > close();
      }
    
    
      /* close connection */
      $mysqli - > close();
    
       //now this is the link..
      echo '<a href="'.$_SERVER['PHP_SELF'].
      '?startrow='.($startrow + 10).
      '">Next</a>';
    
      $prev = $startrow - 10;
    
       //only print a "Previous" link if a "Next" was clicked
      if ($prev >= 0)
    
          echo '  <a href="'.$_SERVER['PHP_SELF'].
      '?startrow='.$prev.
      '">Previous</a>';
    

    Implemented paging and using array slice to display 10 elements at a time.

    Thanks for all the help

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!