gpt4 book ai didi

php - 带分页的 MySQL PHP 搜索

转载 作者:太空宇宙 更新时间:2023-11-03 11:06:19 25 4
gpt4 key购买 nike

我正在尝试对我目前正在从事的项目进行搜索。我面临的问题是,当我转到第二页时,搜索结果将是错误的。

这是我的 header.php 中的搜索表单

<form name="search" id="search" method="post" action="search.php">
<input type="text" tabindex="1" class="input" id="term" name="term" value=""/>
<input type="submit" tabindex="2" id="submit" value="SEARCH" />

这是我的搜索页面php代码

<?php include ('header.php'); 

$term = $_POST['term'];

$queryn = mysql_query("select * from scripts where title like '%$term%' or discription like '%$term%' and approved='1'") or die (mysql_error());
$search_num_rows = mysql_num_rows($queryn);
?>
<div class="cat_script_container">
<div class="nresults">Found <?php echo $search_num_rows;?> results for "<?php echo $term;?>"</div>
<?php
// How many adjacent pages should be shown on each side?
$adjacents = 3;

/*
First get total number of rows in data table.
If you have a WHERE clause in your query, make sure you mirror it here.
*/
//$query = "SELECT COUNT(*) as num FROM $tbl_name";
$query = "select COUNT(*) as num from scripts where title like '%$term%' or discription like '%$term%' and approved='1' order by script_id desc";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];

/* Setup vars for query. */
//$targetpage = "category-$cr[cname]-$cid.html"; //your file name (the name of this file)
$catn = strtolower($cr['cname']); //Turn category name in to lowercase
$catn = preg_replace("![^a-z0-9]+!i", "-", $catn);
$limit = 2; //how many items to show per page
$page = $_GET['page'];

if($page)
$start = ($page - 1) * $limit; //first item to display on this page
else
$start = 0; //if no page var is given, set start to 0
/* Get data. */
$sql = "select * from scripts where title like '%$term%' or discription like '%$term%' and approved='1' order by script_id desc LIMIT $start, $limit";

$result = mysql_query($sql);

/* Setup page vars for display. */
if ($page == 0) $page = 1; //if no page var is given, default to 1.
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
$lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1

/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
//previous button
if ($page > 1)
$pagination.= "<a href=\"search-$prev.html\">« previous</a>";
else
$pagination.= "<span class=\"disabled\">« previous</span>";

//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"search-$counter.html\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"search-$counter.html\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"search-$lpm1.html\">$lpm1</a>";
$pagination.= "<a href=\"search-$lastpage.html\">$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"search-1.html\">1</a>";
$pagination.= "<a href=\"search-2.html\">2</a>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"search-$counter.html\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"search-$lpm1.html\">$lpm1</a>";
$pagination.= "<a href=\"search-$lastpage.html\">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href=\"search-1.html\">1</a>";
$pagination.= "<a href=\"search-2.html\">2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"search-$counter.html\">$counter</a>";
}
}
}

//next button
if ($page < $counter - 1)
$pagination.= "<a href=\"search-$next.html\">next »</a>";
else
$pagination.= "<span class=\"disabled\">next »</span>";
$pagination.= "</div>\n";
}

$q=mysql_query("select * from scripts where title like '%$term%' or discription like '%$term%' and approved='1' order by script_id desc limit $start,$limit");
$numr=mysql_num_rows($q);
if ($numr==0)
{
echo '<div class="msg_info_box">No results were found. Please try a different search.</div>';
}
while($script=mysql_fetch_assoc($q)){
$str = $script['discription'];
$wcount = strlen($str);
if ($wcount > 295){
$dis = substr($str, 0, 295).'...';
}else{
$dis = $str;

}
$sname = $script['title'];
$sname = preg_replace("![^a-z0-9]+!i", "-", $sname);
$sname = strtolower($sname);
$lid = $script['licens_id'];

$license= mysql_query("SELECT * FROM licens WHERE id='$lid' LIMIT 1") or die (mysql_error());
$lrow = mysql_fetch_array($license);

?>
<div class="new_scripts">
<div class="n_title"><a href="script-<?php echo $script['script_id'];?>-<?php echo $sname;?>.html"><?php echo $script['title'];?></a></div>
<div class="n_dis"><?php echo $dis;?></div>
<div class="n_menu">
<!--#-->
<div class="ii_info"><span class="is_lable"><a class="s_link" href="<?php echo $script['script_url'];?>" target="_blank">Visit Publisher Site</a></span> | </div>
<div class="ii_info"><span class="is_lable"><a class="s_link" href="<?php echo $script['script_demo'];?>" target="_blank">View Demo</a></span></div>
<div class="i_info"><span class="is_lable">License Type:</span><span class="license"> <?php echo $lrow['lname'];?></span></div>
<!--#-->
</div>
</div>
<?php } ?>

<div class="pagediv"><?php echo $pagination;?></div>
</div>

我想知道是否有任何方法可以像 action="search.php?term=keyword 这样的搜索表单操作。我想如果我可以像这样发布 url,我可以使用 $_GET 获取 keyworkd。如果有人可以无论如何,请帮助我解决这个问题。在此先感谢。

最佳答案

action 属性更改为 get

<form name="search" id="search" method="get" action="search.php">

这样,您的表单值将在提交时附加到 search.php

例如:

search.php?term=the_term

关于php - 带分页的 MySQL PHP 搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11700868/

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