Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with next results! any help pls?

Status
Not open for further replies.

hisham

IS-IT--Management
Nov 6, 2000
194
Am trying to run the following code from :

<?php
$page = $_REQUEST['page'];
$query = $_POST['query'];

// check to see if $page is set
if (!$page) {
$page = 1;
}

// Change $query to a request super global after the first page.
if($page > 1){
$query = $_REQUEST['query'];
}


//set up some limits
$limit = $page * 10;
$limit = $limit - 10;

//get the count from the database table
$sql_num = mysql_query(&quot;SELECT * FROM table WHERE column LIKE %query%&quot;);
$num = mysql_num_rows($sql_num);

// query your database for the final results
$sql_results = mysql_query(&quot;SELECT something FROM table WHERE column LIKE %query% LIMIT $limit,10&quot;);


if($num < 10){
$through = $num;
} else {
$through = $limit + 10;
}
if ($through > $num){
$through = $total;
}

if($page > 1){
$from = $limit +1;
} else {
$from = $limit;
}
if($from == 0){
$from = $from +1;
}

echo &quot;Search Results<br><br>&quot;;
echo &quot;<p align=\&quot;right\&quot;>&quot;;
if ($page > 1) {
echo &quot;<a
href=\&quot;$PHP_SELF?query=$query&page=&quot;.($page -1).&quot;\&quot;>Previous</a>&nbsp;&nbsp;&quot;;
}

if (($num > 10) && (($limit + 10) < $num)) {
echo &quot;<a href=\&quot;$PHP_SELF?query=$query&page=&quot;.($page +1).&quot;\&quot;>Next</a>&quot;;
}

// Build the list from the $sql query above and display results

while(list($results)=mysql_fetch_array($sql_results)){
echo &quot;Results: $results&quot;;
}

if ($page > 1) {
echo &quot;<a
href=\&quot;$PHP_SELF?query=$query&page=&quot;.($page -1).&quot;\&quot;>Previous</a>&nbsp;&nbsp;&quot;;
}

if (($num > 10) && (($limit + 10) < $num)) {
echo &quot;<a href=\&quot;$PHP_SELF?query=$query&page=&quot;.($page +1).&quot;\&quot;>Next</a>&quot;;
}

?>

It returns the first 10 records of 24, but when I click Next , this take me to
(the parameter query seems null) with only the world Previous and without any record.
 
I'd try

echo &quot;<a href=\&quot;$PHP_SELF?query=&quot;.$query.&quot;&page=&quot;.($page +1).&quot;\&quot;>Next</a>&quot;;

with &quot; and . so that $query is not part of the text.

The code below is not causing you a problem at the moment but will when you get to page 3.

if($num < 10){
$through = $num;
} else {
$through = $limit + 10;
}
if ($through > $num){
$through = $total;
}

I don't see a $total anywhere else but $num is the total anyway and on page 3 your message will say &quot;displaying results 20 - 30&quot; when you only have 24. I'd change it to:

if($num < $limit + 10){
$through = $num;
} else {
$through = $limit + 10;
}

And your message will be:
$message = &quot;Displaying &quot;.$limit.&quot; through &quot;.$through.&quot; of &quot;.$num.&quot; matches &quot;;

If you really want to tidy up the code, change:

<?php
$page = $_REQUEST['page'];
$query = $_POST['query'];

// check to see if $page is set
if (!$page) {
$page = 1;
}

// Change $query to a request super global after the first page.
if($page > 1){
$query = $_REQUEST['query'];
}

To combine them like this:

<?php
$page = $_REQUEST['page'];

// check to see if $page is set
if (!$page) {
$page = 1;
$query = $_POST['query'];
} else {
$query = $_REQUEST['query'];
}

 
By the way, as a courtesy you should send the finished code with it's improvements to the site where you got the original code from.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top