Jump to content

next page's link on return of query ?


nbbcj

Recommended Posts

hi there alll newbie here lol

 

just needed help with the code for the next page of results ?

 

IE i have say 30 results returned from a sql query but only want to show ten at a time i no how to limit the result with the LIMIT 10 but how do i get it to put link show the other ten and so on.

the code i have is here at the bottom of page http://www.phpfreaks.com/forums/index.php/topic,307971.0.html

 

thanks in advance for any help

 

kaine

Link to comment
Share on other sites

Thanks premiso that link was just what im looking for the only prob i have now is putting it with my code

 

im getting this error Fatal error: SQL in /customers/nbbcj.co.uk/nbbcj.co.uk/httpd.www/testd/test.php on line 10

with this code ###### i wanted to leave the $cat bit in so i can use the dropdown menu i have alredy on page

 

 

<?php error_reporting(E_ALL) ; ini_set('display_errors','1'); ?>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>test page one</title>
</head>

<body>
Welcome to the test page #
<form method="POST" action="test.php">
<select size="1" name="D1">
<option value="audio">audio</option>
<option selected value="internet">internet</option>
<option value="file man">file man</option>
<option value="games">games</option>
</select><input type="submit" value="Go Get It"></p>
</form>
Ues the drop down box to select the cat you want 
<?php
//thanks to Crayon Violent for the pagination code 
// database connection info
$conn = mysql_connect('conn','user','pass') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('db name',$conn) or trigger_error("SQL", E_USER_ERROR);

// find out how many rows are in the table 
$cat = mysql_real_escape_string($_POST['D1']); 
$sql = "SELECT COUNT(*) FROM penapps WHERE `cat` = '$cat' LIMIT 10";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 10;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if

// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;

// get the info from the db 
$sql = "SELECT id, number FROM numbers LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) {
   // echo data
   echo $list['id'] . " : " . $list['number'] . "<br />";
} // end while

/******  build the pagination links ******/
// range of num links to show
$range = 3;

// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if 

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
      } // end else
   } // end if 
} // end for
                 
// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page 
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
   // echo forward link for lastpage
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if
/****** end build pagination links ******/
?>
########################################
i also tryed it like this 

// find out how many rows are in the table 
// $cat = mysql_real_escape_string($_POST['D1']); 
$sql = "SELECT COUNT(*) FROM penapps ";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];

but just get blank page :(

 

please help its driving me mad lol

 

thanks in advance

kaine

Link to comment
Share on other sites

dumb question, but did you change the info you need to supply in

 

$conn = mysql_connect('conn','user','pass') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('db name',$conn) or trigger_error("SQL", E_USER_ERROR);

 

yer changed the conn,user,and pass and db name t there proper ones :P

 

Link to comment
Share on other sites

Alll Sorted now thank u every one did a bit of fiddling and it works now part form one thing

 

If you could help again please

 

i can get the dropdown box working but not when i change page it drops the cat selection and no results.

 

I got the ddb to work by changing.....

// get the info from the db 

$sql = "SELECT * FROM penapps LIMIT $offset, $rowsperpage";

$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

 

to

 

// get the info from the db

$cat = mysql_real_escape_string($_POST['D1']);

$sql = "SELECT * FROM penapps WHERE `category` = '$cat' LIMIT $offset, $rowsperpage";

$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

 

Witch make this work

 

<form method="POST" action="test1.php">

<select size="1" name="D1">

<option selected value="audio">audio</option>

<option value="internet">internet</option>

<option value="file man">file man</option>

<option value="games">games</option>

</select><input type="submit" value="Go Get It"></p>

</form>

 

But any help on getting the ddm selection to stay after page change may be by adding $cat to this

 

echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";

  // get previous page num

  $prevpage = $currentpage - 1;

  // show < link to go back to 1 page

  echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";

 

thanks in advance :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.