我创建了一个网站,我添加了搜索选项,在表格中显示搜索结果,我想将行数限制为5页面请帮助我这样做:
我还希望对每个搜索操作进行随机排序搜索结果,而不是每次搜索都会在第一页显示相同的结果:
这是我的搜索代码:
$query = mysql_query("SELECT * FROM table WHERE company LIKE '$company' and activity LIKE '$activity'");
echo "<h3>search results</h3><p>";
echo "<table border='1' align='center' >
<tr>
<th>phone</th>
<th>city</th>
<th>activity</th>
<th>company</th>
</tr>";
while($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['phone']. "</td>";
echo "<td>" . $row['city']. "</td>";
echo "<td>" . $row['activity']. "</td>";
echo "<td>" . $row['company']. "</td>";
echo "</tr>";
}
echo "</table>";
$anymatches=mysql_num_rows($result );
if ($anymatches == 0)
{
echo "<h3>sorry no results</h3>";
}
?>
谢谢。
答案 0 :(得分:1)
首先将您的查询更改为
$query = mysqli_query($con,"SELECT * FROM table WHERE company LIKE '%$company%' and activity LIKE '%$activity%' ORDER BY RAND()");
我们可以使用计数器将提取限制为5.
$counter=1;
while($row = mysqli_fetch_array($query)){
if($counter<6){
echo "<td>" . $row['phone']. "</td>";
echo "<td>" . $row['city']. "</td>";
echo "<td>" . $row['activity']. "</td>";
echo "<td>" . $row['company']. "</td>";
}
else {
/* NOTHING TO DO */
}
$counter=$counter+1;
} /* END OF WHILE LOOP */
$anymatches=mysqli_num_rows($result);
if ($anymatches == 0)
{
echo "<h3>sorry no results</h3>";
}
或您可以尝试此操作,只需将查询更改为:
$query = mysqli_query("SELECT * FROM table WHERE company LIKE '%$company%' and activity LIKE '%$activity%' ORDER BY RAND() LIMIT 5");
如果您只想显示5个结果或每页5行的分页,请说清楚。
如果您正在寻找分页:
我们应该首先将数据存储到表存储中。例如,我们有一个名为搜索的表,甚至只有一个字段 searchfield 。
$searchword=mysqli_real_escape_string($con,$_POST['searchword']);
mysqli_query($con,"UPDATE search SET searchfield='$searchword'");
/* this is where you store your search text for later purposes */
我们马上就会把它取出来(不要误解我的意思,这是出于分页的目的)
$selectsearch = "SELECT * FROM search";
$querysearch = mysqli_query($con, $selectsearch) or die(mysqli_error($selectsearch));
while($rows = mysqli_fetch_array($querysearch)){
$search = $rows['searchfield'];
}
$query = "SELECT * FROM table WHERE company LIKE '%$search%' OR activity LIKE '%$search%'"; /* do your query search */
$result = mysqli_query($con, $query);
$count = mysqli_num_rows($result);
$r = mysqli_fetch_row($result);
$numrows = $r[0];
echo '<b> '.$count.' result/s found for "'.$search.'"</b>';
$rowsperpage = 5;
$totalpages = ceil($count / $rowsperpage);
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
$currentpage = (int) $_GET['currentpage'];
} else {
$currentpage = 1;
}
if ($currentpage > $totalpages) {
$currentpage = $totalpages;
}
if ($currentpage < 1) {
$currentpage = 1;
}
$offset = ($currentpage - 1) * $rowsperpage;
$select="SELECT * FROM table WHERE company LIKE '%$search%' OR activity LIKE '%$search%' LIMIT $offset, $rowsperpage"; /* do the query again but with limit */
$result=mysqli_query($con, $select);
/*start of the table*/
{
echo "<table border='1'>
<tr>
<th>phone</th>
<th>city</th>
<th>activity</th>
<th>company</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['city'] . "</td>";
echo "<td>" . $row['activity']. "</td>";
echo "<td>" . $row['company']. "</td>";
echo "</tr>";
}
echo "</table>";
}
echo '<table border="0"><tr><td>';
/* ***** build the pagination links ***** */
$range = 2;
if ($currentpage > 1) {
$prevpage = $currentpage - 1;
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'>Previous</a> ";
}
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
echo " <font color='#546f3e'><b>$x</b></font> ";
} else {
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
}
}
}
if ($currentpage != $totalpages) {
$nextpage = $currentpage + 1;
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>Next</a> ";
} // end if
/* ***** end build pagination links ***** */
echo '</td></tr></table>';
答案 1 :(得分:0)
您尚未编写任何分页代码,请使用MySQL LIMIT,子句。
如需随机订购,请尝试以下方法:
SELECT * FROM table WHERE company LIKE '$company' and activity LIKE '$activity' ORDER BY RAND()
答案 2 :(得分:0)
从这开始。在这里你需要传递URL中的页码(例如yourwebsite / your-php.php?page = 0)
$page = $_GET['page'];
$perPage = 5;
$start = $page * $perPage;
$query = mysql_query("SELECT * FROM table WHERE company LIKE '$company' and activity LIKE '$activity' limit " . $start . ',' . $perPage);
echo "<h3>search results</h3><p>";
echo "<table border='1' align='center' >
<tr>
<th>phone</th>
<th>city</th>
<th>activity</th>
<th>company</th>
</tr>";
while($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['phone']. "</td>";
echo "<td>" . $row['city']. "</td>";
echo "<td>" . $row['activity']. "</td>";
echo "<td>" . $row['company']. "</td>";
echo "< /tr>";
}
echo "</table>";
$anymatches=mysql_num_rows($result );
if ($anymatches == 0)
{
echo "<h3>sorry no results</h3>";
}
?>
您需要通过提供指向不同页码的链接来实现分页。
我建议使用一些MVC框架,而不是将所有数据检索和视图放在一个文件中。