使用分页和MySQL查询

时间:2013-03-15 15:50:39

标签: php html5

我们使用了分页123按钮,如果我们点击下一个按钮也意味着它应该转到下一页。但是这里它没有进入下一页,因为这里我将sql查询中的_POST变量传递给where条件,所以如果单击下一页则它不显示相关页面。 我们认为在点击其他页面后,POST变量值不会更新。这是我的代码。提前谢谢。

if(isset($_POST['submit']))
 {
  $bool = true;     
  $gender=$_POST['gender'];      
 } 

 if(($bool == true) 
 {

   $bool1=true;
   $connection = mysql_connect("localhost","root","Sakthi");      
  if (!$connection)
  {
   die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("nursingcarein",$connection);

  if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; };
  $start_from = ($page-1) * 3;

  $gender1=$gender;
  $profession1=$profession;
  $state1=$state;

  /**** Here gender value doesnt update when click the next page i think we have a error in this query ****/      
  $sql = "SELECT id,description,name FROM nursereg WHERE gender='$gender1' LIMIT     $start_from, 3";

  $rs_result = mysql_query ($sql,$connection);
  echo "<table>
  <tr><td>Image</td><td>Description</td></tr>";
   while($row=mysql_fetch_array($rs_result)) {
    echo" <tr>";
        echo"<td>"; echo "<img src='image1.php?id=".$row['id']."'>"; echo"</td>";
        echo"<td>"; echo  $row['name']; echo"</td>";
        echo"<td>"; echo  $row['description']; echo"</td>";
        echo"</tr>";
     };
  echo"</table>"; 
  $sql = "SELECT COUNT(Name) FROM nursereg";
 $rs_result = mysql_query($sql,$connection);
  $row = mysql_fetch_row($rs_result);
  $total_records = $row[0];
  $total_pages = ceil($total_records / 5);    
 for ($i=1; $i<=$total_pages; $i++) {
   $pageno = $i;
       echo "<a href='index2.php?page=".$i."'>".$i."</a> ";   
     };
 };

 ?>

1 个答案:

答案 0 :(得分:0)

您的分页使用了一个href,这不会像您的代码所暗示的那样调用POST

$gender1=$_POST['gender'];

尝试将性别添加到分页并通过GET获取

echo "<a href=\"index2.php?page=$i&gender=$gender1\">".$i."</a> "; 

然后在你的代码中

$gender1 = isset($_POST['gender']) ? $_POST['gender'] : $_GET['gender'];