页面链接超过一个在php搜索分页中不起作用

时间:2018-11-24 07:32:46

标签: php forms pagination

我想在数据库上进行搜索,而且一开始,当未单击搜索按钮时,它会显示所有表行。

问题是当我进行搜索时。例如:结果有两个页面,当我单击页面2时,它将显示整个数据库表的页面2,而不是我的搜索结果。

////////////////////////////////////pagination/////////////////////////////
$per_page = 20;
if(isset($_GET['page'])) {
$page = $_GET['page'];
}  else {
$page = 1;
 }
$start = $per_page * $page;
$start = $start - $per_page;

if (isset($_POST['search'])) 
{

    $sharh = ($_POST['sharh']);
    $mark = ($_POST['mark']);

    $query = "SELECT * FROM shams_tbl WHERE sharhe_kala='$sharh' OR Mark='$mark'  LIMIT $start , $per_page";
    $result = mysqli_query($db,$query);

    $query2 = "SELECT COUNT(*) as total FROM shams_tbl WHERE sharhe_kala='$sharh' OR Mark='$mark' ";
    $result2 = mysqli_query($db,$query2);
}
else
{
    $query = "SELECT * FROM shams_tbl LIMIT $start , $per_page";
    $result = mysqli_query($db,$query);

    $query2 = "SELECT COUNT(*) as total FROM shams_tbl";
    $result2 = mysqli_query($db,$query2);
}


  echo " <form method='post' enctype='multipart/form-data'>
  <table class='blueTable'><thead><tr><th>شماره</th><th>شرح کالا</th><th>مارک</th><th>تعداد1</th><th>قیمت غیر نقدی 1</th><th>قیمت نقدی 1</th><th>تعداد 2</th><th>قیمت غیرنقدی 2</th><th>قیمت نقدی 2</th><th>تعداد 3</th><th>قیمت غیرنقدی 3</th><th>قیمت نقدی 3</th><th>افزایش قیمت</th></tr></thead>";

  while($row = $result->fetch_assoc())
  {
    echo "<tr><td>".$row["id"]."</td><td>".$row["sharhe_kala"]."</td><td>".$row["Mark"]."</td><td>".$row["tedad1"]."</td><td>".$row["price_gheyrnaqd1"]."</td><td>".$row["price_naqd1"]."</td><td>".$row["tedad2"]."</td><td>".$row["price_gheyrnaqd2"]."</td><td>".$row["price_naqd2"]."</td><td>".$row["tedad3"]."</td><td>".$row["price_gheyrnaqd3"]."</td><td>".$row["price_naqd3"]."</td><td>".$row["Afzayeshqeymat"]."</td></tr>";
  }
  echo "</table></form> ";



$total = mysqli_fetch_assoc($result2);
$total_page = (ceil($total['total'] / $per_page));
echo"<table class='pagination'>";
        $prev = $page-1;
        if($page <=1) {
            echo "
            <td> << </td>
        ";
        }else {
            echo "
            <td><a href=\"?page=".$prev."\"> << </a></td>
        ";
        }

        for($i=1;$i<=$total_page;$i++){

            if($i==$page) {
                echo "
                <td class='active'>$i</td>
            ";
            }
            else {
                echo "
                <td><a href=\"?page=".$i."\">".$i."</a></td>
            ";
            }
        }
        $next = $page+1;
        if($page>=$total_page) {
            echo "
             <td>>></td>
        ";
        } else {
            echo "
             <td><a href=\"?page=$next\"> >></a></td>
        ";
        }
        echo"</table>";

1 个答案:

答案 0 :(得分:0)

我将网址更改为:

 else {
       echo "
       <td><a href=\"?page=".$i."&sharh=".$sharh."&mark=".$mark."\">".$i."</a></td> ";
      }

并添加一些条件:

if(isset($_GET['mark']) or isset($_GET['mark']))  // if anything in url
    {
    $mark = $_GET['mark'];
    $sharh = $_GET['sharh'];
    } 
else   //first time that search button clicked and there is no url link
    {
    $sharh = ($_POST['sharh']);
    $mark = ($_POST['mark']);
    }
    $query = "SELECT * FROM shams_tbl WHERE sharhe_kala='$sharh' OR Mark='$mark'  LIMIT $start , $per_page";
    $result = mysqli_query($db,$query);

    $query2 = "SELECT COUNT(*) as total FROM shams_tbl WHERE sharhe_kala='$sharh' OR Mark='$mark' ";
    $result2 = mysqli_query($db,$query2);

if ($mark=="" and $sharh=="")   //if not searching
{
    $query = "SELECT * FROM shams_tbl LIMIT $start , $per_page";
    $result = mysqli_query($db,$query);

    $query2 = "SELECT COUNT(*) as total FROM shams_tbl";
    $result2 = mysqli_query($db,$query2);
}