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

在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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?