【问题标题】:how to display each (only one) search result each on an individual page mysql php如何在单个页面上显示每个(只有一个)搜索结果mysql php
【发布时间】:2012-06-27 05:50:43
【问题描述】:

您好,我有一个名为“BookPage”的表格,其列:“PageText”

我已经在“PageText”列上设置了搜索(参见下面的代码)。现在,当用户搜索关键字时,所有结果页面都将显示在另一个页面下方。

如果我可以通过单击某种“下一页”按钮链接将它们移至下一页,我可以在其各个页面上显示每个页面文本。

form.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search</title>
<link href="default.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>
<?php

// include MySQL-processing classes

require_once 'mysql.php'; 

try{

// connect to MySQL

$db=new MySQL(array('host'=>'','user'=>'','password'=>'','database'=>''));

$searchterm=$db->escapeString($_GET['searchterm']);

$result=$db->query("SELECT * FROM BookPage WHERE (PageText) like \"%$searchterm%\" ");

if(!$result->countRows()){

echo '<div class="maincontainer"><h2>No results were found. Go back and try a new search.</h2></div>'."n";

}

else{

// display search results

echo '<div class="maincontainer"><h2>Your search criteria
returned '.$result->countRows().' results.</h2>'."n";

while($row=$result->fetchRow()){

echo '<div class="rowcontainer"><p><strong>Book Id:
</strong>'.$row['BookId'].'<p><p><strong>Page Id:
</strong>'.$row['PageId'].'</p><p><strong>Page Text:
</strong>'.$row['PageText'].'</p></div>'."n"; 

}

}

echo '</div>';

}

catch(Exception $e){

echo $e->getMessage();

exit();

}

?>
</body>
</html>

您好,请看下面的修改代码不起作用,您能告诉我哪里出错了。

修改后的代码

<?php

// include MySQL-processing classes

require_once 'mysql.php'; 

try{

// connect to MySQL

$db=new MySQL(array('host'=>'',''=>'','password'=>'','database'=>''));

    //page
 if (isset($_GET['page'])) {
    $page_no = $_GET['page'];
    $next_pg = $page_no + 1;
} else {
    $page_no = 0;
    $next_pg = 1;
}

    $searchterm=$db->escapeString($_GET['searchterm']); 
    $result=$db->query("SELECT * FROM BookPage WHERE (PageText) like \"%$searchterm%\" ORDER BY BookId ASC LIMIT 0, $page_no");


if(!$result->countRows()){

echo '<div class="maincontainer"><h2>No results were found. Go back and try a new search.</h2></div>'."n";

}

else{

// display search results

echo '<div class="maincontainer"><h2>Your search criteria returned '.$result->countRows().' results.</h2>'."n";

while($row=$result->fetchRow()){

$searchvalue = implode('<span style="color:green;font-weight:800;background-color:yellow;">'.$_GET['searchterm'].'</span>',explode($_GET['searchterm'],$row['PageText'])); 
echo '<div class="rowcontainer"><p><strong>Book Id:
</strong>'.$row['BookId'].'<p><p><strong>Page Id:
</strong>'.$row['PageId'].'</p><p><strong>Page Text:
</strong>'.$searchvalue.'</p></div>'."n";  
}

}

echo '</div>';

}

catch(Exception $e){

echo $e->getMessage();

exit();

}

?>

【问题讨论】:

    标签: php jquery mysql search pagination


    【解决方案1】:

    您可以尝试将查询更改为:

    if (isset($_GET['page'])) {
        $page_no = $_GET['page'];
        $next_pg = $page_no + 1;
    } else {
        $page_no = 0;
        $next_pg = 1;
    }
    
    $result=$db->query("SELECT * FROM BookPage WHERE (PageText) like \"%$searchterm%\" ORDER BY id ASC LIMIT 1, $page_no");
    

    然后插入如下链接:

    <a href="thispage.php?page=<?php echo $next_pg; ?>">Next page</a>
    

    这将做的是一次获得一个结果,并根据您所在的页面抵消结果。执行此操作时唯一重要的事情是按某事对结果进行排序,以便每个查询都以相同的顺序放置结果。例如,按 ID ASC 排序(根据我的示例)。

    【讨论】:

    • 嗨,请查看我修改后的代码,我无法让它工作。任何帮助。
    【解决方案2】:

    您在 MySQL 中使用了 LIMIT 子句。跟踪您在 PHP 中所在的结果行,并通过您的链接传递变量以在下一个查询中使用。

    来自 MySQL 文档 (http://dev.mysql.com/doc/refman/5.1/en/select.html):

    LIMIT 子句可用于限制 SELECT 语句返回的行数。 LIMIT 接受一个或两个数字参数,它们都必须是非负整数常量(使用准备好的语句时除外)。

    有两个参数,第一个参数指定要返回的第一行的偏移量,第二个参数指定要返回的最大行数。初始行的偏移量为0(不是1)

    【讨论】:

    • 嗨,请查看我修改后的代码,我无法让它工作。任何帮助。非常感谢。
    猜你喜欢
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    相关资源
    最近更新 更多