Current location: Hot Scripts Forums » Programming Languages » PHP » results page to display prev/next


results page to display prev/next

Reply
  #1 (permalink)  
Old 01-09-04, 10:04 PM
php-learner php-learner is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy results page to display prev/next

Help..

I am pulling my hair out. I am trying to create a script where after choosing some drop down selections in a search page and choosing values it posts to a results.php page where the database is queried and results can be printed.

I am trying to place the previous and next linking script into my code. I can still get the script to perform the search and results be displayed but when i click the next button i receive a message "there are no results". I have been looking around in why this does this and i have read that i should place [&column=$column\ along the a href line in my previous links / next link coding. I have got this working but only if i tell the query to SELECT & from table where (value = '2333333). I allready have this value in the database. This make the prev/next links work and i can browse through all the items with values 2333333.

I have a drop down box full of other options. I can not intergrate this in and make it work. I have tried replacing the value = 2333333 with $value_search which is the name which is posted after sending the search request.

I AM REALLY REALLY STUCK

CAN ANYBODY HELP

HAS ANYBODY ALLREADY HAVE A SCRIPT WHERE BY THEY CAN QUERY A DATABASE AFTER A SEARCH PAGE AND DISPLAY THE RESULTS.

IF IT REQUIRES PAYMENT AND AS LONG AS ITS AFFORDABLE I WILL PAY AS THIS HAS REALLY GOT ME.

THANKS IN ADVANCE

PHP-LEARNER
Reply With Quote
  #2 (permalink)  
Old 01-10-04, 12:55 AM
supirman45 supirman45 is offline
New Member
 
Join Date: Jan 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
some light to guide you...

in a guestbook that i made, i displayed 20 entries per page and put up links to all the other result pages.... here's most of the code.


function showEntries() {
$perPage=20;
global $conn, $page;
if(!$page)
$page = 0;
$StartPage =$page*20;

// find out how many entries total
$sql_howmany="SELECT * from becky where 1";
$res=mysql_query($sql_howmany, $conn);
$numRows=mysql_num_rows($res);
$StartPage2=$StartPage+1;

// set upper bound of entries to retrieve from db
$upper=$StartPage+20;
if($upper>$numRows)
$upper = $numRows;


$totalPages= $numRows/$perPage;

// fetch 20 entries starting at entry "$StartPage"
$sql = "SELECT * FROM becky ORDER BY id DESC LIMIT $StartPage,$perPage";

$sql_result = mysql_query($sql, $conn) or die("Couldnt exec Query $sql");

$thePage=$page+1;

$entry="<font size=2><div align=right>Displaying entries $StartPage2 - $upper of $numRows
<br>";
$prev=$page-1;
if($page)
$entry .= "&nbsp;<a href=\"guestbook.php?page=$prev\">< Previous Page </a>&nbsp;";

if($upper!=$numRows)
$entry .= "&nbsp;<a href=\"guestbook.php?page=$thePage\"> Next Page ></a>&nbsp;";
$entry .="<br>Go to page ";
for ($i=0;$i<$totalPages;$i++){
$old_page=$i;
$new_page=$i+1;
$entry .= " <a href=\"guestbook.php?page=$old_page\">$new_page</a> " ;
}
.
. // this stuff wasn't relevant to you.
.
}
Reply With Quote
  #3 (permalink)  
Old 01-10-04, 05:42 AM
php-learner php-learner is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Question

You are displaying all of the results in the guestbook over a set number of pages. I have had success in doing this to display all the items in my database. Its when i try changing the sql query to select items from table where searchtype like search term etc. The first page of results will be formed but when you hit the next link i get a message that there are no results to display.

I THINK THERE IS A PROBLEM WITH TAKING THE SEARCH VAR TO THE NEXT PAGE. NOT TOO SURE THOUGH.

IT IS REALLY GETTING TO ME.

HAS ANYBODY CREATED OR OBTAINED A SCRIPT WHEREBY THEY CAN PLACE INFO IN A SEARCH HTML PAGE AND QUERY A DATABASE TO DISPLAY ALL RESULTS WHICH RESULT TO THAT SEARCH USING THE PREV/NEXT LINK FUNCTIONS. HELP.....
Reply With Quote
  #4 (permalink)  
Old 01-10-04, 09:59 AM
Infinite_Hackers's Avatar
Infinite_Hackers Infinite_Hackers is offline
Coding Addict
 
Join Date: Dec 2003
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
if its a search just add this to the query line*

WHERE dl_name REGEXP '$keyword'


PHP Code:

 $sql "SELECT * FROM becky WHERE dl_name REGEXP '$keyword' ORDER BY id DESC LIMIT $StartPage,$perPage"
that will select all resuts were
Reply With Quote
  #5 (permalink)  
Old 01-10-04, 03:59 PM
php-learner php-learner is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Smile

Thanks for the tip on using the REGEXP. I placed it into my code when i placed it exactly how you told me i managed to get a search result. What i expected but i knew there was another two fields on another page. When i clicked on the next page i received the "there are no results to display message again"

any ideas how i can query a database a searchterm which is created by the user from a drop down selection on the search.html and displaying the results in a results.php way but using the page prev/next link without this error happening. I know i am nearly there but i just need a little bit more help.
Reply With Quote
  #6 (permalink)  
Old 01-10-04, 04:04 PM
Infinite_Hackers's Avatar
Infinite_Hackers Infinite_Hackers is offline
Coding Addict
 
Join Date: Dec 2003
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
put it in the url.
i.e

http://yourdoamin/search.php?keyword=$keyword


PHP Code:

function showEntries() {

$perPage=20;
global 
$conn$page;
if(!
$page)
$page 0;
$StartPage =$page*20;

// find out how many entries total
$sql_howmany="SELECT * from becky where 1";
$res=mysql_query($sql_howmany$conn);
$numRows=mysql_num_rows($res);
$StartPage2=$StartPage+1;

// set upper bound of entries to retrieve from db
$upper=$StartPage+20;
if(
$upper>$numRows)
$upper $numRows;


$totalPages$numRows/$perPage;

// fetch 20 entries starting at entry "$StartPage"
$sql "SELECT * FROM becky ORDER BY id DESC LIMIT $StartPage,$perPage";

$sql_result mysql_query($sql$conn) or die("Couldnt exec Query $sql");

$thePage=$page+1;

$entry="<font size=2><div align=right>Displaying entries $StartPage2 - $upper of $numRows
<br>"
;
$prev=$page-1;
if(
$page)
$entry .= "&nbsp;<a href=\"guestbook.php?page=$prev&keyword=$keyword\">< Previous Page </a>&nbsp;";

if(
$upper!=$numRows)
$entry .= "&nbsp;<a href=\"guestbook.php?page=$thePage&keyword=$keyword\"> Next Page ></a>&nbsp;";
$entry .="<br>Go to page ";
for (
$i=0;$i<$totalPages;$i++){
$old_page=$i;
$new_page=$i+1;
$entry .= " <a href=\"guestbook.php?page=$old_page&keyword=$keyword\">$new_page</a> " ;
}
.
// this stuff wasn't relevant to you.
.

i think that should do it.

So when it gets the keyword from the text field.
And you click next.

It will be using the info from the *keyword=w/e to get your results.
Reply With Quote
  #7 (permalink)  
Old 01-10-04, 04:16 PM
php-learner php-learner is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
A little better. I am querying database and receiving results to display over 2 pages which is correct. But when i click the 2nd page or next it displays another 6 pages which is all the results in the database. Then if i click previous all the item before the field i was searching for appears before it pushing the required search fields down the ladder. I have pasted the exact code i am using below. Please tell me if i am doing anything wrong. I have also placed brackets around $keyword value after the regexp as i received a mysql error if i didnt.

Thanks

<html><head>

</head>
<body>

<?
$page = $_GET['page'];


$search_value=$HTTP_POST_VARS['search_value'];

global $search_value,$result, $page;


$search_value = addslashes($search_value);

// include connections strings here

@ $db= mysql_pconnect('localhost', 'username', 'password');

if (!$db)
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}


mysql_select_db('dbname');

$limit = 4;
$query_count = "SELECT * FROM properties WHERE value REGEXP '($search_value)' order by value desc ";
$result_count = mysql_query($query_count);

$totalrows = mysql_num_rows($result_count);

if(empty($page)){
$page = 1;
}


$limitvalue = $page * $limit - ($limit);


$query = "SELECT * FROM properties WHERE value REGEXP '($search_value)' LIMIT $limitvalue, $limit";
$result = mysql_query($query);
$num=mysql_num_rows($result);
if(mysql_num_rows($result) == 0){
echo("Nothing to Display!");
}

$i = 0;
echo "Listing of items in our database<br><p>";

while ($i < $num):
$id=mysql_result($result,$i,"id");
$region=mysql_result($result,$i,"region");
$value=mysql_result($result,$i,"value");



echo "$region <br>";
echo "$value <br> ";
echo "<a href=\"view_item.php?id=$id\">View</a> <br>";
$i++;
endwhile;


if($page != 1){
$pageprev = $page-1;

echo("<a href=\"$PHP_SELF?page=$pageprev&$search_value=sear ch\">PREV ".$limit."</a>&nbsp;&nbsp;&nbsp;");
}else{
echo("PREV&nbsp;" .$limit."&nbsp;&nbsp;&nbsp;");
}

$numofpages = $totalrows / $limit;

for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo($i."&nbsp;");
}else{
echo("<a href=\"$PHP_SELF?page=$i&search=$search_value\">$i </a>&nbsp;");
}
}


if(($totalrows % $limit) != 0){
if($i == $page){
echo($i."&nbsp;");
}else{
echo("<a href=\"$PHP_SELF?page=$i&search=$search_value\">$i </a>&nbsp;");
}
}

if(($totalrows - ($limit * $page)) >=0){
$pagenext = $page+1;

echo("&nbsp;&nbsp;&nbsp;<a href=\"$PHP_SELF?page=$pagenext&search=$search_val ue\">NEXT ".$limit."</a>");
}else{
echo("&nbsp;&nbsp;&nbsp;NEXT&nbsp;" .$limit);
}

mysql_free_result($result);

?>

</center>
</body>
</html>
Reply With Quote
  #8 (permalink)  
Old 01-10-04, 06:43 PM
php-learner php-learner is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Smile prev/link error i think

My code is displaying the right return results after a query. I know there are six fields which should be produced and with 4 records to a page there should be 2 pages. This is correct. But when i navigate to the following page it displays the results but then it jumps from displaying 2 pages to 7 pages which then shows all my database results. I do not want this. I want the user to search for required field once queried then to display results and paged but not to change when pressing next.

I think i have an error in the code which creates and controls the use of the next/previous links. But i dont know where.

Could someone please check my code and see where i am going wrong. The code is produced and has not changed from the previous post of code. The only change i have made is on one of the url lines it said $search_item = value. i have changed this to what the other lines say just in case this was doing it but it was not.

Can some body help please!!!

php-learner
Reply With Quote
  #9 (permalink)  
Old 01-10-04, 07:04 PM
Infinite_Hackers's Avatar
Infinite_Hackers Infinite_Hackers is offline
Coding Addict
 
Join Date: Dec 2003
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
Hey, do yo have this up so i could see whats going on(url)?
Reply With Quote
  #10 (permalink)  
Old 01-11-04, 04:15 PM
HasansWeb HasansWeb is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
This is called pagination, right? Anyone know a good tutorial on this that really clearifies the process?
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Classified Ads skipper23 Perl 3 11-22-05 02:22 AM
Classified Ads skipper23 Perl 2 12-30-03 03:43 AM
index page not showing up skipper23 PHP 3 12-15-03 01:10 PM
Create a next page link when there are too many records to be display in a webpage Han84 ASP 0 10-19-03 10:32 PM
Which way to display results from mysql Peter_web PHP 2 09-13-03 05:39 PM


All times are GMT -5. The time now is 08:25 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.