MySQL PHP-Suche mit Paginierung
Frage
Ich versuche, eine Suche nach einem Projekt zu implementieren, an dem ich gerade arbeite.Das Problem, mit dem ich konfrontiert bin, ist, dass das Suchergebnis falsch ist, wenn ich zur zweiten Seite gehe.
Hier ist das Suchformular, das sich in meiner header.php befindet
<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" />
Hier ist der PHP-Code meiner Suchseite
<?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>
Ich frage mich, ob es eine Möglichkeit gibt, dem Suchformular eine Aktion wie action="search.php?term=keyword" zu geben.Ich denke, wenn ich die URL so posten kann, kann ich $_GET verwenden, um den Schlüssel zu übernehmen.Wenn mir irgendjemand dabei irgendwie helfen kann, habe ich mich wirklich angeeignet.Danke im Voraus.
Lösung
Ändere dein action
zuschreiben get
<form name="search" id="search" method="get" action="search.php">
Auf diese Weise werden die Werte Ihres Formulars angehängt search.php
bei der Einreichung.
Zum Beispiel:
search.php?term=the_term
Andere Tipps
anstelle von Inline-Papaginierungscode Sie haben eine Klasse und kann sie dann verwenden, indem Sie seine Funktionen anrufen.Auf diese Weise können Sie neu kodieren und ein besseres Programm zu programmieren.
Sie müssen nichts mit der Klasse tun, aber nur den Anruf überprüfen.
anrufen :
generasacodicetagpre.pager class
generasacodicetagpre.