duanbamo0127 2011-05-10 13:14
浏览 27
已采纳

使用按钮使用php迭代mysql结果集

Hi people as a new web programming im having a scenario and i need some help.

Im trying to have a 'next' button on the page which will pull the next question from the database in a iterative manner. im passing php values straight to the button as text.

Currently I can grab the mysql resultset and print them all or i can grab just the one row and return its fields but what is the strategy to grab one with the next button, then another, and so on.

$result = mysql_query("SELECT * FROM $DB_QUEST_NAME ORDER BY pk_Id DESC;") or die(mysql_error());

    while ($row = mysql_fetch_array($result)){

    echo $pk_id = $row[0] . "<br>";
    echo $question = $row[1].  "<br>";
    echo $answera = $row[2]  .  "<br>";
    echo $answerb = $row[3] . "<br>";
    echo $answerc = $row[4] . "<br>";
    echo $answerd = $row[5] . "<br><br>";

    break;  
    }

In the mark up include the getNextPoll.php (above) which initial grabs the first result to displays them as button. but with every subsequent click I want to get the next, then the next and so on. I know how to set the button texts and Im also aware of server side client side responsibilites but as Im quite new to this type of development the methodology behind it a lil tricky.

Any feedback would be appreciated. Thanks.

  • 写回答

4条回答 默认 最新

  • donglao4370 2011-05-10 13:17
    关注

    I believe you're looking for MySQL's "offset". This allows you to run a query like ... limit 1 offset 0 to return the first result, then ... limit 1 offset 1 to return the 2nd result...and so on.

    (You don't really need "offset 0", since that's the default anyway, but - just trying to illustrate the point.)

    EDIT:

    If you store the page variable in the URL like this: www.myPage.com/questions.php?pg=2

    Then you can do this:

    $numPerPage = 1;
    $pg = $_GET['pg'];
    $nxtPage = $pg++;
    $offset = ($numPerPage * $pg) - 1;
    $question = mysql_query("SELECT * FROM questions LIMIT " . $numPerPage . 
    " OFFSET " . $offset);
    

    Then, you're "next" link could be:

    <a href="questions.php?pg=<?=$nxtPage?>">NEXT</a>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line