Jul 7th, 2009, 08:54 AM
#1
Thread Starter
Fanatic Member
paging a search variable
hi,
This is my 2nd post in this category
I have paging script it is working fine so far, but dont know how do I use it togather with serch form to post a search variable so it can still paging the variable up to it should be in page $i, next and prev.
attachment is a sql database, config.php search.php paging.php scripts
Any help is great and very much appreciate
ps. I will come back within 25 mins
Rgds,
Attached Files
Jul 8th, 2009, 01:36 AM
#2
Thread Starter
Fanatic Member
Re: paging a search variable
I have modify this script, but it return with search variable is empty:
vb Code:
<?php
include 'config.php';
$pg = $_GET[pg];
/* page default 1 */
if(!isset($_GET['pg'])){
$page = 1;
} else {
$page = $_GET['pg'];
}
/* item per page */
$max_results = 5;
/* calc: 1 x 5 = 5 , 5 - 5 = 0 , record start from 0*/
$from = (($page * $max_results) - $max_results);
$search=$_POST["search"];
if ($search != "") {
/* populate databse, LIMIT 0 to 5 */
$sql = mysql_query("SELECT * FROM content ORDER BY id WHERE header LIKE '%$search%' LIMIT $from, $max_results ");
//while($row = mysql_fetch_array($sql)){
/* display result, */
echo "<table>";
while ($r=@mysql_fetch_array($sql)){
echo ("
<tr><td>Id: <b>$r[id]</b></td></tr>
<tr><td>Word: $r[header]</td></tr>
<tr><td>Meaning: $r[content]<hr></td></tr>");
}
echo "</table>";
?>
<hr>
<?php
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM content"),0);
$total_pages = ceil($total_results / $max_results);
/* hiperlink pages*/
echo "<center>Select a Page<br />";
/* Previous link */
if($pg > 1){
$prev = ($page - 1);
echo "<a href=$_SERVER[PHP_SELF]?pg=$prev> <-Previous </a> ";
}
for($i = 1; $i <= $total_pages; $i++){
if(($pg) == $i){
echo "$i ";
} else {
echo "<a href=$_SERVER[PHP_SELF]?pg=$i>$i</a> ";
}
}
/* Next link */
if($pg < $total_pages){
$next = ($page + 1);
echo "<a href=$_SERVER[PHP_SELF]?pg=$next>Next-></a>";
}
echo "</center>";
} else {
echo "Search is empty, Key-in a word first!!";
}
?>
Regards,
Last edited by ksuwanto8ksd; Jul 8th, 2009 at 01:39 AM .
Jul 8th, 2009, 03:39 AM
#3
Thread Starter
Fanatic Member
Re: paging a search variable
the script above is a bit complicated, now this is a nother search script use content database, to search header by letter and display its content,;
Code:
<?php
$host="localhost";
$username="root";
$password="1234";
$db_name="test";
$tbl_name="content";
mysql_connect("$host","$username", "$password")
or die("Cannot connect");
mysql_select_db("$db_name")
or die("Cannot select database");
$search=$_POST["search"];
if ($search != "") {
$result = mysql_query("SELECT * FROM $tbl_name
WHERE header LIKE '%$search%' ");
if (mysql_num_rows($result)>0) {
$numrows=mysql_num_rows($result);
echo "<p>" .$numrows . " results found for " . stripslashes("<b>$search</b>") . "</p>";
echo "<table>";
while ($r = mysql_fetch_array($result)){
echo ("
<tr><td>Id: <b>$r[id]</b></td></tr>
<tr><td>header: $r[header]</td></tr>
<tr><td>content: $r[content]<hr></td></tr>");
}
echo "</table>";
} else {
echo "No result found on <b>$search</b> ";
}
}
if(!$search){
echo "Key-in a word for searching";
}
?>
now Is there possible to do mysql_query that is searched by $result and requery into this script in order to make paging:
Code:
$pg = $_GET[pg];
/* page default 1 */
if(!isset($_GET['pg'])){
$page = 1;
} else {
$page = $_GET['pg'];
}
/* item per page is 5 */
$max_results = 5;
/* calc: 1 x 5 = 5 , 5 - 5 = 0 , record start from 0*/
$from = (($page * $max_results) - $max_results);
/* populate databse, LIMIT 0 to 5 */
$sql = mysql_query("SELECT * FROM $result ORDER BY id LIMIT $from, $max_results ");
and change this line to:
while ($r = mysql_fetch_array($sql)){
any advice ?
Regards,
Last edited by ksuwanto8ksd; Jul 8th, 2009 at 05:14 AM .
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width