0

hi
i create a page that select 6 item form certain table in database and then in each page it shows 6 item of them order by id desc and now i decided to increase the number of item for displaying in each pages to 12.
so the question is here how can i change the code for showing 12 items in each page instead of 6 item.
please help me with this .

<?php 
include "db.php";
 
//Rows
$qtyRow = '<tr>';
$priceRow = '<tr>';
 
$nn=6; //Number of items, we split this later
 
$s=mysql_query("SELECT * FROM `test1`");
 
$c=mysql_num_rows($s);
 
if($c%$nn==0){
	$cc=$c/$nn;	
}else{
	$cc=$c/$nn+1;	
}
 
//$cc : Number of pages
 
$ss=@$_REQUEST['ss']; //Start position (eg: 6)
$other_tables = "";
 
if($ss=="" || $ss==null){
	$ss = 0; //Set to zero because it is incremented later
}
 
//Pages
echo "<table border=\"1\" align=\"center\">";
$sql=mysql_query("SELECT * FROM `test1` ORDER BY `id` DESC LIMIT $ss,$nn");
$inc = 0;
while($r=mysql_fetch_array($sql)){
	$id=$r['id'];  
	$name=$r['name'];
	if($inc == 0){
		$qtyRow .= "<tr>";
		$priceRow .= "<tr>";
	}
	$qtyRow .= "<td>$id</td>";
	$priceRow .= "<td>$name</td>";
	++$inc;
	if($inc == 3 || $inc == 6 || $inc == mysql_num_rows($sql)){
		//Display Table
		echo $qtyRow."</tr>";
		echo $priceRow."</tr>";
		echo "</table>";
 
		//To remove extra table
		if(mysql_num_rows($sql) <= 3){
			$inc = 7;
		}
 
		//Reset
		if($inc == 3){
			echo "<br><table border='1' align='center'>";
			$qtyRow = "<tr>";
			$priceRow = "<tr>";
		}
	}
}
echo "</table>";
$j=0;
echo"page : ";
for($i=1;$i<=$cc;$i++){
	echo"<a href=\"select.php?ss=$j\">$i</a>";
	echo"|";
	$j=$j+$nn; 
}
?>
3
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by diafol
0

Have you tried...

<?php 
include "db.php";
 
//Rows
$qtyRow = '<tr>';
$priceRow = '<tr>';
 
$nn=12; //Number of items, we split this later
 
$s=mysql_query("SELECT * FROM `test1`");
 
$c=mysql_num_rows($s);
 
if($c%$nn==0){
	$cc=$c/$nn;	
}else{
	$cc=$c/$nn+1;	
}
 
//$cc : Number of pages
 
$ss=@$_REQUEST['ss']; //Start position (eg: 6)
$other_tables = "";
 
if($ss=="" || $ss==null){
	$ss = 0; //Set to zero because it is incremented later
}
 
//Pages
echo "<table border=\"1\" align=\"center\">";
$sql=mysql_query("SELECT * FROM `test1` ORDER BY `id` DESC LIMIT $ss,$nn");
$inc = 0;
while($r=mysql_fetch_array($sql)){
	$id=$r['id'];  
	$name=$r['name'];
	if($inc == 0){
		$qtyRow .= "<tr>";
		$priceRow .= "<tr>";
	}
	$qtyRow .= "<td>$id</td>";
	$priceRow .= "<td>$name</td>";
	++$inc;
	if($inc == 3 || $inc == 6 || $inc == mysql_num_rows($sql)){
		//Display Table
		echo $qtyRow."</tr>";
		echo $priceRow."</tr>";
		echo "</table>";
 
		//To remove extra table
		if(mysql_num_rows($sql) <= 3){
			$inc = 7;
		}
 
		//Reset
		if($inc == 3){
			echo "<br><table border='1' align='center'>";
			$qtyRow = "<tr>";
			$priceRow = "<tr>";
		}
	}
}
echo "</table>";
$j=0;
echo"page : ";
for($i=1;$i<=$cc;$i++){
	echo"<a href=\"select.php?ss=$j\">$i</a>";
	echo"|";
	$j=$j+$nn; 
}
?>
0

it doesn't work did you test it ? please test it and you will be see what happen !
what is this ?

1

it doesn't work did you test it ? please test it and you will be see what happen !
what is this ?

It's your job to test it and report back. Contributors have got lives too you know. The fact that contributors don't have your MYSQL tables, means that they cannot directly test the code tailored to your particular needs.

So, as I said, YOU test it and tell us what happens.

0

this page has a simple table in database with two columns one of them is id that it is primary and auto increment and the other one is simple text that u insert some words.

987654321sadddsadsasadadsadsdsadsasddassadsadadadssadsaddsapage : 1|

this is the result for pagination
AS I SAID this code tested for the result of 6 item in each page
i requested for changing it in 12 items in each page ?
Do you notice clearly ?

Edited by mehdi.yazdani: n/a

1

I understand the question you originally posed.

What I don't understand is the error that phorce's code gave you. Is this the error?

987654321sadddsadsasadadsadsdsadsasddassadsadadadssadsaddsapage : 1|

If you want a pagination system, just search this forum. There are about 2 questions like yours every week.

Usually:

//CONFIG
$items_pp = 6; //or 12

//DEFAULTS
$page = 1;
$start_item = 1;

//COMMENCE
$r = mysql_query("SELECT * FROM `test1`");
$total_items = mysql_num_rows($r);
$num_pages = ceil($total_items/$items_pp);

//TEST page parameter in url
if(isset($_GET['page']) && intval($_GET['page']) > 1 && intval($_GET['page']) <= $num_pages){
  $page = intval($_GET['page']);
  $start_item = ($page - 1)*$items_pp + 1;
}

//CREATE PAGE LINKS
for($x=1;$x<$num_pages+1;$x++){
  $pages[] = ($x == $page) ? $x : "<a href=\"select.php?page=$x\">$x</a>";
} 
$pagestring = "<p>Choose Page: " . implode(" | ", $pages) . "</p>";


//CREATE DISPLAY TABLE
$r = mysql_query("SELECT * FROM `test1` ORDER BY `id` LIMIT $start_item,$items_pp");
if(mysql_num_rows($r)){
  $tbl = "<table><thead><tr><th>ID</th><th>NAME</th></thead><tbody>";
  while($d = mysql_fetch_array($r)){
     $tbl .= "<tr><td>{$d['id']}</td><td>{$d['name']}</td></tr>";
  } 
  $tbl .= "</tbody></table>";
}else{
  $tbl = "No records found in the DB for this page.";
}

//DISPLAY TABLE AND LINKS:
echo $tbl;
echo $pagestring;

This is NOT tested. I just made it up.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.