douwen3362 2015-04-22 15:35
浏览 68
已采纳

使用PHP“prev”和“next”来移动数组

I have two "buttons" to move the array cursor to the next record or to the prior record. The function using "next" to move to the next (older) record works correctly.

However, the function to move to the prior ("prev") record (which is toward the top of the array) does not work correctly. I suspect that may be related to using "mysqli_data_seek". My belief was that it would set the cursor to the current location and then backtrack to the previous record. Instead the cursor continues to moves forward as if "next" was used instead of "prev". How can I backtrack to the prior record?

function return_next_issue()
    {
    // Newer Issue  - Moves up towards the top of the array.
    // mysqli_data_seek($_SESSION['result'], 0);
    while($row=mysqli_fetch_array($_SESSION['result'], MYSQL_NUM))
            {
                if($_SESSION['issuenum'] == $row[0]) 
                    {                                                       
                        break;
                    }   
            }   
    prev($_SESSION['result']);
    $row=mysqli_fetch_row($_SESSION['result']); 
    return $row[0];
    } 
  • 写回答

1条回答 默认 最新

  • dsaob80228 2015-04-22 15:54
    关注

    since the mysqli_result object is in the session and shared, you might want to initialize the internal pointer to 0 to make sure you know you are going to seek from the start. You can also create a variable that increments over the iteration to identify the index you need and seek for the previous one.

    function return_next_issue()
    {
       // Newer Issue  - Moves up towards the top of the array.
       $i = 0;
       mysqli_data_seek($_SESSION['result'], 0);
       while($row=mysqli_fetch_array($_SESSION['result'], MYSQL_NUM))
       {
          if($_SESSION['issuenum'] == $row[0]) 
          {                                                       
             break;
          }   
          $i++;
    
       }   
       mysqli_data_seek($_SESSION['result'], $i - 1);
       $row=mysqli_fetch_row($_SESSION['result']); 
       return $row[0];
    }
    

    Please note that your code won't work if $_SESSION['issuenum'] is 0 or if is greater than the number of rows available, and therefore so do the code I am providing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?