Jump to content

Maximum Limit Per Page


arunpatal

Recommended Posts

Hi, when i add new item into list, it add in same page without limit....

I want that as soon as there are number of amount (10 or 20 items) are in one page

it show link to next page.....

 

Here is the displaying list code......

 

$dynamicList = "";
$sql = mysql_query("SELECT * FROM product ORDER BY date_added DESC LIMIT 999");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
		 $id = $row["id"];
   $product_name = $row["product_name"];
   $price = $row["price"];
   $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
   $dynamicList .= '<div style="float:left;">
 <p align="center"><a href="product_detail.php?id=' . $id . '">
 <img style="border:#666 1px solid;" src="product_images/' . $id . '.jpg" title=" ' . $product_name . '" alt=" ' . $product_name . '" width="80" height="105" border="1" hspace="10" vspace="10" style="FILTER: alpha(opacity=100);-moz-opacity: 1.0; opacity: 1.0;" onmouseover=BeginOpacity(this,100,40) onmouseout=EndOpacity(this,100) /></a><br />' . $product_name . '</div>';
   }
} else {
$dynamicList = "We have no products listed in our store yet";
}
mysql_close();
?>

Link to comment
Share on other sites

here you need to get a solution with the pagination. Pagination is a concept you’re familiar with even if you don’t know the term. When you use a search engine like Google, it displays the results as a series of pages and not as one long list. Here in your script could

benefit from this same feature.

 

This link will help you to learn the subject.. http://www.phpfreaks.com/tutorial/basic-pagination

Edited by thara
Link to comment
Share on other sites

here you need to get a solution with the pagination. Pagination is a concept you’re familiar with even if you don’t know the term. When you use a search engine like Google, it displays the results as a series of pages and not as one long list. Here in your script could

benefit from this same feature.

 

This link will help you to learn the subject.. http://www.phpfreaks...asic-pagination

 

 

Thanks...... I will try this and get back with result :)

Link to comment
Share on other sites

Notice: Undefined variable: conn in C:\xampp\htdocs\products.php on line 28

 

Warning: mysql_query() expects parameter 2 to be resource, null given in C:\xampp\htdocs\products.php on line 28

 

Fatal error: SQL in C:\xampp\htdocs\products.php on line 28

 

 

This is line 28

 

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

 

SORRY I forgot to add database connection info...

I am still testing

Edited by arunpatal
Link to comment
Share on other sites

So i got

 

Fatal error: SQL in C:\xampp\htdocs\products.php on line 73

 

 

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

 

What do i have to give in this line

$sql = "SELECT id, number FROM product LIMIT $offset, $rowsperpage";

 

product is my table...

Edited by arunpatal
Link to comment
Share on other sites

This is all code

 

 

<?php
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
// Run a select query to get my letest 6 items
// Connect to the MySQL database 
include "viks2007_script/connect_to_mysql.php";
$dynamicList = "";
$sql = mysql_query("SELECT * FROM product ORDER BY date_added DESC LIMIT 999");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
		 $id = $row["id"];
   $product_name = $row["product_name"];
   $price = $row["price"];
   $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
   $dynamicList .= '<div style="float:left;">
 <p align="center"><a href="product_detail.php?id=' . $id . '">
 <img style="border:#666 1px solid;" src="product_images/' . $id . '.jpg" title=" ' . $product_name . '" alt=" ' . $product_name . '" width="80" height="105" border="1" hspace="10" vspace="10" style="FILTER: alpha(opacity=100);-moz-opacity: 1.0; opacity: 1.0;" onmouseover=BeginOpacity(this,100,40) onmouseout=EndOpacity(this,100) /></a><br />' . $product_name . '</div>';
   }
} else {
$dynamicList = "We have no products listed in our store yet";
}
mysql_close();
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Store Home Page</title>
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen" />
</head>
<body>
<script language=Javascript>
<!--
var itv = 50;
var step = 10;
var start = 0;
var end = 0;
var currentOpac;
//change the opacity for different browsers
function changeOpac(obj, opacity) {
var object = obj.style;
object.opacity = (opacity / 100);
object.MozOpacity = (opacity / 100);
object.KhtmlOpacity = (opacity / 100);
object.filter = "alpha(opacity=" + opacity + ")";
}
function BeginOpacity(obj, s, e)
{
start = s;
end = e;
currentOpac = s;
theobject=obj;
changing=setInterval("opacityit(theobject)",itv);
}
function EndOpacity(obj, end){
clearInterval(changing);
changeOpac(obj, end);
}
function opacityit(obj){
if(start > end) {
 if (currentOpac>end){
  currentOpac = currentOpac - step;
  changeOpac(obj,currentOpac);
 }
 else if (window.highlighting)
  clearInterval(highlighting);
} else if(start < end) {
 if (currentOpac<end){
  currentOpac = currentOpac + step;
  changeOpac(obj,currentOpac);
 }
 else if (window.changing)
  clearInterval(changing);
}
}
//-->
</script>


<div align="center" id="mainWrapper">
 <?php include_once("viks2007_header.php");?></div >


 <table id="products_page_maintable">
 <tr>
   <td id="left_products_td" valign="top"> <h3 id="left_products_td_headline">Latest Designer Fashions</h3>
  <?php echo $dynamicList; ?>
  </td>

   <td id="right_products_td" valign="top"><h3 id="right_products_td_headline">Handy Tips</h3>
  If you operate any store online you should read the documentation provided to you by the online payment gateway you choose for handling the checkout process. You can get much more insight than I can offer on the various details of a gateway, from the gateway providers themselves. They are there to help you with whatever you need since they get a cut of your online business dealings.</td>
 </tr>
</table>

 </div>
 <?php include_once("viks2007_footer.php");?>
</div>
</body>
</html>

 

 

This is table

 

$sqlCommand = "CREATE TABLE product (

id int(11) NOT NULL auto_increment,

product_name varchar(255) NOT NULL,

price varchar(16) NOT NULL,

details text NOT NULL,

img1 text NOT NULL,

img2 text NOT NULL,

img3 text NOT NULL,

img4 text NOT NULL,

date_added date NOT NULL,

PRIMARY KEY (id),

UNIQUE KEY product_name (product_name)

) ";

Link to comment
Share on other sites

This is exactly you are looking for.. I coded this but not checked. This should be work with some minor modification or not..

 

<?php
$display = 10;

// Determine how many pages there are...
if (isset($_GET['p']) && is_numeric ($_GET['p'])) { // Already been determined.

 $pages = $_GET['p'];

} else { // Need to determine.

 // Count the number of records:
 $q = "SELECT COUNT( id ) FROM product AS p";
 $r = @mysqli_query ($dbc, $q);
 $row = @mysqli_fetch_array ($r, MYSQLI_NUM);
 $records = $row[0];

 // Calculate the number of pages...
 if ($records > $display) { // More than 1 page.
  $pages = ceil ($records/$display);
 } else {
  $pages = 1;
 }
} // End of p IF.

// Determine where in the database to start returning results...
if (isset($_GET['s']) && is_numeric($_GET['s'])) {
 $start = $_GET['s'];
} else {
 $start = 0;
}

$q = "SELECT * FROM product ORDER BY date_added DESC LIMIT $start, $display";  
$r = mysqli_query( $dbc, $q);

if ( mysqli_num_rows($r) >= 1) {

 while ( $row = mysqli_fetch_array( $r, MYSQLI_ASSOC)) {

     $id = $row["id"];
  $product_name = $row["product_name"];
  $price = $row["price"];
  $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));

  echo '<div style="float:left;">
 <p align="center"><a href="product_detail.php?id=' . $id . '">
 <img style="border:#666 1px solid;" src="product_images/' . $id . '.jpg" title=" ' . 
 $product_name . '" alt=" ' . $product_name . '" width="80" height="105" border="1" 
 hspace="10" vspace="10" style="FILTER: alpha(opacity=100);-moz-opacity: 1.0; opacity: 1.0;" 
 onmouseover=BeginOpacity(this,100,40) onmouseout=EndOpacity(this,100) /></a><br />' . 
 $product_name . 
  '</div>'; 
 }

} else { 
 echo 'We have no products listed in our store yet';  
}

// Make the links to other pages, if necessary.
if ($pages > 1) {

 echo '<p>';

 // Determine what page the script is on:
 $current_page = ($start/$display) + 1;

 // If it's not the first page, make a Previous link:
 if ($current_page != 1) {
  echo '<a href="products.php?s=' . ($start - $display) . '&p=' . $pages . '"><</a>';
 }


   // Make all the numbered pages:
   for ($i = 1; $i <= $pages; $i++) {
 if ($i != $current_page) {
  echo '<a href="products.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> ';
 } else {
  echo $i; 
 }
   } // End of FOR loop.  


   // If it's not the last page, make a Next button:
   if ($current_page != $pages) {
 echo '<a href="products.php?s=' . ($start + $display) . '&p=' . $pages . '">></a>';
   }

   echo '</p>'; // Close the paragraph.  
 } // End of links section. 
?>

Link to comment
Share on other sites

This is exactly you are looking for.. I coded this but not checked. This should be work with some minor modification or not..

 

<?php
$display = 10;

// Determine how many pages there are...
if (isset($_GET['p']) && is_numeric ($_GET['p'])) { // Already been determined.

$pages = $_GET['p'];

} else { // Need to determine.

// Count the number of records:
$q = "SELECT COUNT( id ) FROM product AS p";
$r = @mysqli_query ($dbc, $q);
$row = @mysqli_fetch_array ($r, MYSQLI_NUM);
$records = $row[0];

// Calculate the number of pages...
if ($records > $display) { // More than 1 page.
$pages = ceil ($records/$display);
} else {
$pages = 1;
}
} // End of p IF.

// Determine where in the database to start returning results...
if (isset($_GET['s']) && is_numeric($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}

$q = "SELECT * FROM product ORDER BY date_added DESC LIMIT $start, $display";
$r = mysqli_query( $dbc, $q);

if ( mysqli_num_rows($r) >= 1) {

while ( $row = mysqli_fetch_array( $r, MYSQLI_ASSOC)) {

$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));

echo '<div style="float:left;">
 <p align="center"><a href="product_detail.php?id=' . $id . '">
 <img style="border:#666 1px solid;" src="product_images/' . $id . '.jpg" title=" ' .
 $product_name . '" alt=" ' . $product_name . '" width="80" height="105" border="1"
 hspace="10" vspace="10" style="FILTER: alpha(opacity=100);-moz-opacity: 1.0; opacity: 1.0;"
 onmouseover=BeginOpacity(this,100,40) onmouseout=EndOpacity(this,100) /></a><br />' .
 $product_name .
 '</div>';
}

} else {
echo 'We have no products listed in our store yet';
}

// Make the links to other pages, if necessary.
if ($pages > 1) {

echo '<p>';

// Determine what page the script is on:
$current_page = ($start/$display) + 1;

// If it's not the first page, make a Previous link:
if ($current_page != 1) {
echo '<a href="products.php?s=' . ($start - $display) . '&p=' . $pages . '"><</a>';
}


// Make all the numbered pages:
for ($i = 1; $i <= $pages; $i++) {
 if ($i != $current_page) {
 echo '<a href="products.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> ';
 } else {
 echo $i;
 }
} // End of FOR loop.


// If it's not the last page, make a Next button:
if ($current_page != $pages) {
 echo '<a href="products.php?s=' . ($start + $display) . '&p=' . $pages . '">></a>';
}

echo '</p>'; // Close the paragraph.
} // End of links section.
?>

 

I am very new to php and trying........

But it would be nice if you can connect this code with my code so that i can copy and paste it.....

Hope that would not be a problem for you...............

 

Thanks alot

Edited by arunpatal
Link to comment
Share on other sites

I modified the code with your product.php page. This is the complete script and you do need to nothing with it..

 

<?php
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Store Home Page</title>
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen" />
 </head>


 <body>

<script language=Javascript> <!--
 var itv = 50;
 var step = 10;
 var start = 0;
 var end = 0;
 var currentOpac;

 //change the opacity for different browsers
 function changeOpac(obj, opacity) {
  var object = obj.style;
  object.opacity = (opacity / 100);
  object.MozOpacity = (opacity / 100);
  object.KhtmlOpacity = (opacity / 100);
  object.filter = "alpha(opacity=" + opacity + ")";
 }


 function BeginOpacity(obj, s, e) {
  start = s;
  end = e;
  currentOpac = s;
  theobject=obj;
  changing=setInterval("opacityit(theobject)",itv);
 }


 function EndOpacity(obj, end){
  clearInterval(changing);
  changeOpac(obj, end);
 }


 function opacityit(obj){
  if(start > end) {
   if (currentOpac>end){
 currentOpac = currentOpac - step;
 changeOpac(obj,currentOpac);
   }
   else if (window.highlighting)
 clearInterval(highlighting);
  } else if(start < end) {

   if (currentOpac<end){
   currentOpac = currentOpac + step;
   changeOpac(obj,currentOpac);
   } else if (window.changing)
 clearInterval(changing);
  }
 } //-->
</script>



<div align="center" id="mainWrapper">
 <?php include_once("viks2007_header.php");?></div >


<table id="products_page_maintable">
 <tr>
  <td id="left_products_td" valign="top"> <h3 id="left_products_td_headline">Latest Designer Fashions</h3>


<?php
// Connect to the MySQL database 
include "viks2007_script/connect_to_mysql.php";


$display = 10;

// Determine how many pages there are...
if (isset($_GET['p']) && is_numeric ($_GET['p'])) { // Already been determined.

 $pages = $_GET['p'];

} else { // Need to determine.

 // Count the number of records:
 $q = "SELECT COUNT( id ) FROM product AS p";
 $r = @mysqli_query ($dbc, $q);
 $row = @mysqli_fetch_array ($r, MYSQLI_NUM);
 $records = $row[0];

 // Calculate the number of pages...
 if ($records > $display) { // More than 1 page.
  $pages = ceil ($records/$display);
 } else {
  $pages = 1;
 }
} // End of p IF.

// Determine where in the database to start returning results...
if (isset($_GET['s']) && is_numeric($_GET['s'])) {
 $start = $_GET['s'];
} else {
 $start = 0;
}

$q = "SELECT * FROM product ORDER BY date_added DESC LIMIT $start, $display";  
$r = mysqli_query( $dbc, $q);

if ( mysqli_num_rows($r) >= 1) {

 while ( $row = mysqli_fetch_array( $r, MYSQLI_ASSOC)) {

     $id = $row["id"];
  $product_name = $row["product_name"];
  $price = $row["price"];
  $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));

  echo '<div style="float:left;">
 <p align="center"><a href="product_detail.php?id=' . $id . '">
 <img style="border:#666 1px solid;" src="product_images/' . $id . '.jpg" title=" ' . 
 $product_name . '" alt=" ' . $product_name . '" width="80" height="105" border="1" 
 hspace="10" vspace="10" style="FILTER: alpha(opacity=100);-moz-opacity: 1.0; opacity: 1.0;" 
 onmouseover=BeginOpacity(this,100,40) onmouseout=EndOpacity(this,100) /></a><br />' . 
 $product_name . 
  '</div>'; 
 }

} else { 
 echo 'We have no products listed in our store yet';  
}

// Make the links to other pages, if necessary.
if ($pages > 1) {

 echo '<p>';

 // Determine what page the script is on:
 $current_page = ($start/$display) + 1;

 // If it's not the first page, make a Previous link:
 if ($current_page != 1) {
  echo '<a href="products.php?s=' . ($start - $display) . '&p=' . $pages . '"><</a>';
 }


   // Make all the numbered pages:
   for ($i = 1; $i <= $pages; $i++) {
 if ($i != $current_page) {
  echo '<a href="products.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> ';
 } else {
  echo $i; 
 }
   } // End of FOR loop.  


   // If it's not the last page, make a Next button:
   if ($current_page != $pages) {
 echo '<a href="products.php?s=' . ($start + $display) . '&p=' . $pages . '">></a>';
   }

   echo '</p>'; // Close the paragraph.  
 } // End of links section. 
?>

  </td>

  <td id="right_products_td" valign="top">
   <h3 id="right_products_td_headline">Handy Tips</h3>
   If you operate any store online you should read the documentation provided to you by the online payment gateway you choose for handling the checkout process. You can get much more insight than I can offer on the various details of a gateway, from the gateway providers themselves. They are there to help you with whatever you need since they get a cut of your online business dealings.
  </td>
 </tr>
</table>
</div>

<?php include_once("viks2007_footer.php");?>
</div>
 </body>
</html>

Link to comment
Share on other sites

THANKS.... the help from you is very useful......

 

but I got an error...

 

 

Notice: Undefined variable: dbc in C:\xampp\htdocs\products.php on line 114

 

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\products.php on line 114

 

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\products.php on line 116

We have no products listed in our store yet

 

 

THESE ARE TWO LINE

 

$r = mysqli_query( $dbc, $q);

if ( mysqli_num_rows($r) >= 1) {

Edited by arunpatal
Link to comment
Share on other sites

oh.. its a mistake.. and just add your db connection there...

 

$r = mysqli_query($your-connection, $q);

 

 

$r = mysqli_query( $dbc, $q);

 

Will it be like this

 

$r = mysqli_query( $r = mysqli_query( $"localhost", "my_user", "my_password", $q);

Edited by arunpatal
Link to comment
Share on other sites

How those information exist in your 'connect_to_mysql.php' page...

 

In that script this connections info must have something similar to this..

 

$link = mysqli_connect("localhost", "my_user", "my_password" );

 

if so.... then mysql query should be..

 

$r = mysqli_query( $link, $q);

Edited by thara
Link to comment
Share on other sites

How those information exist in your 'connect_to_mysql.php' page...

 

In that script this connections info must have something similar to this..

 

$link = mysqli_connect("localhost", "my_user", "my_password" );

 

if so.... then mysql query should be..

 

$r = mysqli_query( $link, $q);

 

No.... there is nothing like $link

 

Here is the code in connect_to_mysql

 

<?php
$db_host = "localhost";
$db_username = "cart";
$db_pass = "";
$db_name = "cart";
// Run the actual connection here
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");			
?>

Edited by arunpatal
Link to comment
Share on other sites

when you creating a connection with mysql always use a variable to keep the connection. If the connection was made, the variable, short for database connection, will become a reference point for all of your subsequent database interactions. Most of the PHP functions for working with MySQL will take this variable as its first argument.

Link to comment
Share on other sites

This is a good practice code for your connection.php page..

 

<?php 
// This file also establishes a connection to MySQL
// and selects the database.


// Set the database access information as constants:
DEFINE ('DB_USER', 'username');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'sitename');
// Make the connection:
$link = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );
?>

 

try with this...

Link to comment
Share on other sites

@Muddy_Funster... you are correct. I couldn't mentioned to OP about it..

 

@arunpatal try to understand the difference.. Here I use mysql functions with mysqli extension. These functions provide improved performance and take advantage of added features (among other benefits).

Link to comment
Share on other sites

Update the script with this code.. (in page linking section)

 

// If it's not the first page, make a Previous button:
if ($current_page != 1) {
echo '<a href="products.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a> ';
}


// Make all the numbered pages:
for ($i = 1; $i <= $pages; $i++) {
if ($i != $current_page) {
 echo '<a href="products.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> ';
} else {
 echo $i . ' ';
}
} // End of FOR loop.


// If it's not the last page, make a Next button:
if ($current_page != $pages) {
echo '<a href="products.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>';
}


echo '</p>'; // Close the paragraph.

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.