简体   繁体   English

我如何获得分页在这里工作?

[英]How can I get the pagination to work here?

I'm fairly new to coding, so I'm open to critique as well as help. 我对编码还很陌生,因此我既乐意批评也乐于帮助。 I'm trying to apply pagination to my search results. 我正在尝试将分页应用于搜索结果。 I have returned the results I need, applied the limit and managed to get the pagination controls to present properly. 我已经返回了所需的结果,应用了限制并设法使分页控件正确显示。 However, when I select "next" or "previous" the pages have no results on. 但是,当我选择“下一个”或“上一个”时,这些页面都没有结果。 I'm sure there is something fundamentally wrong, but I just can't spot it. 我敢肯定有些根本上是错误的,但是我无法发现它。

?php
include_once("db_connex.php");
if (isset ($_POST ['search']))  {
    $searchq = $_POST ['search'];
    $count_query = mysql_query("SELECT * FROM activejobs WHERE jobtitle LIKE '%$searchq%' OR region LIKE '%$searchq%' AND status= '1'");
    $count = mysql_num_rows($count_query);
    // pagination starts here
    if (isset($_GET['page'])) {
      $page = pre_replace("#[^0-9]#","",$_GET['page']);
    }
    else {
       $page = 1;
    }

    $perPage = 3;
    $lastPage = ceil($count / $perPage);

    if ($page < 1) {
       $page = 1;
    } 
    else if ($page > $lastPage) {
       $page = $lastPage;
    }

    $limit = "LIMIT " . ($page - 1) * $perPage. ", $perPage";
    $query = mysql_query("SELECT * FROM activejobs WHERE jobtitle LIKE '%$searchq%' OR region LIKE '%$searchq%' AND status= '1' ORDER BY jobid DESC $limit");
 }
 if ($lastPage != 1) {
    if ($page != $lastPage) {
       $next = $page + 1;
       $pagination .= '<a href="jobsearch.php?page='.$next.'">Next</a>';
    }

    if ($page != 1) {
       $prev = $page - 1;
       $pagination .= '<a href="jobsearch.php?page='.$prev.'">Previous</a>';
    }
 }

 while ($row = mysql_fetch_array($query)) {
        $jobtitle = $row['jobtitle'];
        $region = $row['region'];
        $salary = $row['salary'];
        $jobdescription = $row ['jobdescription'];
        $joburl = $row ['joburl'];

        $output .= '<div id= "searchresults"><a href = "http://www.nursestation.co.uk/jobdetails.php?id=' . $jobid . '"><div id= "applybutton">Details</div></a><font id= "resultstitle">'.$jobtitle.'&nbsp-&nbsp'.$region.'&nbsp-&nbsp'.$salary.'</font><br>'.$jobdescription.'</div>';
 }

?>

This kinds of if-else hell: 这种if-else地狱:

// pagination starts here
if (isset($_GET['page'])) {
  $page = pre_replace("#[^0-9]#","",$_GET['page']);
}
else {
   $page = 1;
}

can be solved like this (default it at start): 可以这样解决(默认情况是在开始时):

// pagination starts here
$page = 1;
if (isset($_GET['page'])) {
  $page = pre_replace("#[^0-9]#","",$_GET['page']);
}

or even this (if you feel adventurous): 甚至(如果您喜欢冒险):

$page = (isset($_GET['page']) ? pre_replace("#[^0-9]#","",$_GET['page']) : 1);

Making a lot of If-else is easy at start and hard later, so keep it simple by reducing when you have nothing to do. 进行大量If-else的创建很容易,一开始就很困难,因此,通过减少无所事事的时间来简化操作。 Making your code smaller in a step closer to any solution. 使您的代码更小, 更接近任何解决方案。

Also this is common: 这也是常见的:

...
while ($row = mysql_fetch_array($query)) {
   $jobtitle = $row['jobtitle'];
   $region = $row['region'];
...

Why assigning $jobtitle to $row['jobtitle']; 为什么将$jobtitle分配给$row['jobtitle']; ? It doesnt make your code easier, it just adds more code and making you read harder. 它不会使您的代码更容易,它只会添加更多的代码并使您阅读起来更困难。 Give $row['X'] directly. 直接给$row['X']

Also, as @ojovirtual stated you need to pass "$search" parameter everytime, otherwise your entire code block will be ignored ( "$search" is not set) 另外,正如@ojovirtual所说,您每次都需要传递“ $ search”参数,否则整个代码块将被忽略(未设置“ $ search”

Finally, when working with MySQL you need to check the values you feed your queries with, in this example the $searchq . 最后,在使用MySQL时,您需要检查输入查询的值,在本例中为$searchq A malicious coder could make the $searchq look like a part of the query. 恶意编码人员可能使$searchq看起来像查询的一部分。 There is a simple fix for that: 有一个简单的解决方法:

Instead of plain: 而不是简单的:

$count_query = mysql_query("SELECT * FROM activejobs WHERE jobtitle LIKE '%$searchq%' OR region LIKE '%$searchq%' AND status= '1'");

make it a habit doing this: 养成这样做的习惯:

$searchq = mysql_real_escape_string($searchq);
$count_query = mysql_query("SELECT * FROM activejobs WHERE jobtitle LIKE '%$searchq%' OR region LIKE '%$searchq%' AND status= '1'");

Not a universal solution but a starter before dive into new technologies as a starter. 不是通用解决方案,而是在作为入门者进入新技术之前的入门者。 This is a must for fields like username, password etc. 这是用户名,密码等字段的必填项。

Finally, change from: 最后,从:

if (isset ($_POST ['search']))  {
    $searchq = $_POST ['search'];

to: 至:

if (isset ($_GET['search']))  {
    $searchq = $_GET['search'];

You need to pass search again in your next and previuos buttons. 您需要通过search你再nextpreviuos按钮。 A quick fix would be: 一个快速的解决方法是:

Change $_POST to $_REQUEST : $_POST更改$_POST $_REQUEST

if (isset ($_REQUEST ['search']))  ...

Add the search to your next and previous buttons: 将搜索添加到您的nextprevious按钮:

$pagination.= '<a href="jobsearch.php?search='.urlencode($_REQUEST["search"]).'&page='.$next.'">Next</a>';

Same with the previous button. 与上previous按钮相同。

As someone stated, you should do some input sanitation before any database query. 就像有人说的那样,您应该在进行任何数据库查询之前进行一些输入清理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM