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.

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

报告相同问题?

悬赏问题

  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?