gpt4 book ai didi

php - 在带有行限制的表中搜索结果php

转载 作者:行者123 更新时间:2023-11-29 13:10:30 25 4
gpt4 key购买 nike

我创建了一个网站,并添加了在表格中显示搜索结果的搜索选项,我想将每页的行数限制为 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>";
}
?>

谢谢。

最佳答案

首先将您的查询更改为

$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 行的分页,请明确说明。

如果您正在寻找分页:

我们应该首先将数据存储到表存储中。例如,我们有一个名为 search 的表,其中甚至只有一个字段,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>&nbsp;'.$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>';

关于php - 在带有行限制的表中搜索结果php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22165284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com