Jump to content

Next Page


Recommended Posts

I have the following code of the news

 

<?php
$getnews = mysql_query("select * from news ORDER BY id DESC");
while($r=mysql_fetch_array($getnews)){
extract($r);

echo "<p><table border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='100%' id='AutoNumber1' height='157'>
  <tr>
    <td width='100%' height='19' bgcolor='#008000' bordercolorlight='#008000' bordercolordark='#008000' background='bkgimg.gif'>
    <p align='center'><b><font color='#008000'>$title</font></b></td>
  </tr>
  <tr>
    <td width='100%' height='117' bgcolor='#FFFF00' bordercolorlight='#FFFF00' bordercolordark='#FFFF00' background='bkgimglng.gif'>
    <p align='justify'><font color='#008000'>$news</font></td>
  </tr>
  <tr>
    <td width='100%' height='19' bgcolor='#008000' bordercolorlight='#008000' bordercolordark='#008000' background='bkgimg.gif'>
    <p align='center'><b><font color='#008000'>Posted on $date</font></b></td>
  </tr>
</table></p>";
?>

 

 

I want news as 10 per page how can i do?

 

 

Link to comment
Share on other sites

to start, you'd need to adjust the query. Here's an example (but advisable not to just copy/paste to your script as it does no checks for valid input):

 

<?php
// human page number starts from 1. offset page starts from 0, so
// subtract one from requested page
if ($_GET['page'])
{
   $page = $_GET['page'] - 1;
}
else
{
   $page = 0;
}

// what record to start from...result will be 0,10,20,30, etc
// simply multiply page by number per page
$offset = $page * 10; 

$getnews = mysql_query("select * from news ORDER BY id DESC LIMIT $offset, 10");
?>

 

you could do worse than hunting down a tutorial on 'pagination' - there are plenty around and each has different ways of achieving the same result, but the above should start you off.

 

cheers

Link to comment
Share on other sites

YOu would have to do a  num_rows query similar to this since this is the code I have used on differnet projects and it has not failed me yet, you can use it if you like or change it to suit your needs.

 

<?php 
if(isset($submit)) {

  $page = $_GET["page"];
  $rowmax = 10; //Set this to the number of items you want returned per page

  if(!isset($page)) {
   $page = 1;
}

 if($page == 1) {	 	
 	$pagein = 0;
 }	

if($page > 1) { 
	$pagein = $page * $rowmax - $rowmax;
}

//Below is where the total number of rows will be calculated
$query = mysql_query("SELECT * FROM tablename WHERE text LIKE '%$searchterm%'"); 

$num_rows = mysql_num_rows($query);

//Below is where you would do the selected query to draw only paginated results.
$query1 = "SELECT * FROM tablename WHERE text LIKE '%$searchterm%' LIMIT $pagein, $rowmax"; 

$result1 = mysql_query($query1);

while($row = mysql_fetch_array($result1)){
//This is the area where the paginated ruslts will be displayed by row results
}

//Below will only do calutaions if query was successful
if($result1 != null) {
$pagecount = ceil($num_rows / $rowmax);
$resultinput = $pagein + 1;
$resultsoutput = $pagein + $rowmax;
if($resultsoutput > $num_rows) {$resultsoutput = $num_rows;}//Change $resultsoutput to $num_rows on last page here

//Only displays pagination links if necessary below here
if ($pagecount > 1) {
  echo "Page ";
  $x = 1;
  $y = $pagecount;
	  for( $i = $x; $i <= $y; $i++ ) {
  print "<a href='index.php?page=$i'><b>[$i]</b></a> ";
	  }

}
echo "<p align='center'>There were $num_rows results.<br />//Shows the total number of resutls on this line
	You are viewing results $resultinput through $resultsoutput.</p>";//shows which results you are viewing here
  }
}
?>

 

I hope this will give you a solid foundation to start from.

Link to comment
Share on other sites

×
×
  • 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.