【问题标题】:Result after adding pagination code is getting messed up in PHP添加分页代码后的结果在 PHP 中变得混乱
【发布时间】:2013-03-04 10:58:02
【问题描述】:

以下是我的代码的一部分。我创建了一个文本框,当用户在文本框中输入单词/短语时,它会在数据库中搜索关键字。此代码还包含分页代码。 我在这段代码中面临的问题是它第一次显示页码,但在第一次它没有显示所需的结果。当我从一个页面移动到另一个页面时,它会丢失变量“$each”的值并显示数据库中的所有结果。而这段代码应该只显示符合查询条件的部分数据

    <input type="text" name="k" 
        value="<?php if(isset($_GET['k'])){echo htmlentities($_GET['k']);} ?>" style="border: 1px, thin; width:92%; "/> 
    <input type="image" style="margin-bottom: 0; margin-top: 2px;" src="search.png"  value="submit" />
    </div>
    </fieldset>
</form>
</div>
<table cellpadding="0" cellspacing="0" border="1">
<tbody>    
<?php
$connection = mysql_connect('', '', '');
if(!$connection)
    echo "No database connected";
$dbase = mysql_select_db("", $connection);
if(!$dbase)
    echo "No datatable connected";

if(isset($_GET['k'])){ 
    $k1 = $_GET['k']; 
} else { 
    $k1 = ''; 
}

echo $k1;

$term = explode(" ", $k1);

$query = "SELECT * FROM kcpdatabase ";

foreach ($term as $each) {
   echo $each;
    $i++;
   if($i==1) {
        $query .= "WHERE keywords LIKE '%$each%' ";
   } else {
        $query .= "OR WHERE keywords LIKE '%$each%' ";
    }
}

echo $query;

$per_pages=3;
$page_query = mysql_query("SELECT COUNT('title') FROM kcpdatabase");

$pages = ceil(mysql_result($page_query, 0)/$per_pages)  or die($page_query."<br/><br/>".mysql_error()); //Calculating the number of pages and round it to higher side

$page = (isset($_GET['page'])) ? (int) ($_GET['page']) : 1; //Checking wheather value of page is set or not. 
                                                         //If not then assign it as a default value of 1.
$start = ($page - 1) * $per_pages; // Point to the record zero always to start displaying the data

$query .= "LIMIT $start, $per_pages";

$ourquery1 = mysql_query ($query);
if(!$ourquery1)
    echo "No query found";

    $row1 = mysql_num_rows ($ourquery1);

if($pages >= 1 && $page <= $pages){ 
   for($x = 1; $x <= $pages; $x++) {
        echo '<a href="?page='.$x.'">'.$x.'</a> ';
   }

    if ($row1 > 0) {

        while($result = mysql_fetch_assoc($ourquery1)) {
         echo "<tr>";
            echo "<td>";
            $title = $result['title'];
            $link = $result['link'];
            $region = $result['region'];
            $sector = $result['sector'];
            $theme = $result['theme'];      
            echo "<td> <a href=$link><h3>$title<h3></a>";
            echo "<h4>Sector: $sector Theme: $theme &nbsp; 
            <br> Region: $region </td> </tr>";
        }
    }   
}
echo "</tbody>";

【问题讨论】:

    标签: php html mysql css


    【解决方案1】:

    您需要将您的搜索条件添加到您的分页链接 (k=$k1),因为它目前仅在您提交表单时设置。尝试类似 -

    if($pages >= 1 && $page <= $pages){ 
       for($x = 1; $x <= $pages; $x++) {
            echo '<a href="?k='.$k1.'&page='.$x.'">'.$x.'</a> ';
       }
    

    注意 - 您当前在 $k1 => $each 中使用未经处理的用户输入。

    【讨论】:

      【解决方案2】:

      Please, don't use mysql_* functions 在新代码中。它们不再维护and are officially deprecated. 参见red box?改为了解 prepared statements,并使用 PDOMySQLi - this article 将帮助您决定哪个。如果选择PDOhere is a good tutorial.

      【讨论】:

      • 虽然这是很好的信息,但这应该是评论,而不是答案,因为它无助于解决 OP 的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多