如何显示每一行从数据库在不同的页面在php

how to display each row from database in different page in php?

本文关键字:php 数据库 一行 显示 何显示      更新时间:2023-09-26

我有一个代码显示3个问题和多个答案从数据库供用户选择。选择答案后,用户将点击提交按钮。

当前代码:

<?php
$today=date("Y-m-d");
echo "<form method='post' id='submit' action='checkresult.php' dir='rtl'>";
$sql="SELECT * FROM cquestions where showdate='$today' limit 3";
$result=mysql_query($sql);

while ($row = mysql_fetch_array($result)) {
   echo "<p>" . $row['cqtext'] . "</p>";
   $sql2="SELECT * FROM canswers where cqid=".$row['cqid'];
   $result2=mysql_query($sql2);
   while($row2=mysql_fetch_assoc($result2))
   {
      echo "<input type='radio' name='".$row['cqid']."' value='".$row2['cqans']."' />".$row2['aatext']; 
   }
}

$tomorrow= date("Y-m-d", strtotime("tomorrow"));
$sql4="SELECT * FROM qupdate";
$result4=mysql_query($sql4);
$last_update=mysql_result($result4,"last_update");
if($last_update==$today)
{
    $cqid=mysql_result($result,"cqid");
    $update1="update cquestions set showdate='$tomorrow' where showdate='0000-00-00' and cqid!='$cqid' order by cqid limit 3";
    mysql_query($update1);
    $update2="update qupdate set last_update='$tomorrow'";
    mysql_query($update2);
    $sql3="SELECT * FROM qupdate";
    $result3=mysql_query($sql3);
    $last_update=mysql_result($result3,"last_update");  
}
echo"<br>";
echo"<br>";
echo"<input type='submit' id='submit' name='submit' value='Submit Answers' />";
echo "</form>" ;
?>

以上代码的输出

question1
ans1, ans2, ans3, ans4
question2
ans1, ans2, ans3, ans4
question3
ans1, ans2, ans3, ans4
SUBMIT

我想做一些改变。我想让它在第一页显示第一个问题,用户点击下一步,然后它必须去第二个问题,然后第三个问题,最后提交按钮。

期待输出

page1
Question 1
ans1, ans2, ans3, ans4
Next
after choosing the ans user clicks next
page2
question 2
ans1, ans2, ans3, ans4
Next
page3
question 3
ans1, ans2, ans3, ans4
SUBMIT Button.

怎么做?

您可以像

那样使用jquery show/hide or fadeIn/fadeOut
<form>
<div id="question1" class="questions">
//php question and its answers in radio
<a href="javascript:void(0)" onclick="getnext('question2')"></a>
</div>
<div id="question2" class="questions" style="display:none">
//php question and its answers in radio
<a href="javascript:void(0)" onclick="getnext('question3')"></a>
</div>
<div id="question3" class="questions" style="display:none">
//php question and its answers in radio
<input type="submit"/>
</div>

<script type="text/javascript">
function getnext(id){
//for fade effect
$(".questions").fadeOut("fast");
$("#"+id).fadeIn("slow");
//for show/hide
$(".questions").hide();
$("#"+id).show();
 }
 </script>

确保包含了jquery

在HTML中

第一页末尾:

<a href="?gotopage=2">Next</a>

第二页末尾:

<a href="?gotopage=3">Next</a>

第三页末尾:

<a href="?dosubmit">Submit</a>
在PHP中

if( isset($_GET['gotopage'])  && $_GET['gotopage'] == 2 ){
   //codes for 2 page
} 

分页是一个简单的概念,其中每条记录都被写入显示为页面的a中,并且具有指向具有其他记录数据的其他页面的链接。

参考:简单易用的PHP分页