Jump to content

Pagination help..


Recommended Posts

Hi,

 

I'm really new to php and I am trying to learn it slowly. So far I have been able to create login/register scripts and can display my products all on one page. Now i want to split this page into several different pages, and Ive been using the following tutorial:

 

http://phpeasystep.com/phptu/29.html

 

In the last part where u enter your own while loop, i have:

 

<?php
        while($row = mysql_fetch_array($result))
        {
    
        $result=mysql_query($query);
 
 
    $num=mysql_numrows($result);
 
 
    mysql_close();
 
    $i=0;
    while ($i < $num) {
 
    
    $product_name=mysql_result($result,$i,"product_name");
    $product_line=mysql_result($result,$i,"product_line");
    $product_description=mysql_result($result,$i,"product_description");
    $image=mysql_result($result,$i,"image");
    $price=mysql_result($result,$i,"price");
 
 
 
    echo "<font color=\"white\"><u>$product_name</u></b><br><br>$product_line<br><br></font>";
    echo "<img alt=\"product image\" width=\"250\" height=\"265\" src=\"images/".$image." \"/>";
    echo "<font color=\"white\"></br><u>Product Description</u><br><br>$product_description<br><br>£$price<hr><br></font>";
 
    $i++;
}
    
        }
    ?>

 

 

Then im getting these errors:

 

Notice: Use of undefined constant num - assumed 'num' in C:\wamp\www\pagination.php on line 23

 

Notice: Undefined index: page in C:\wamp\www\pagination.php on line 28

 

Warning: mysql_result() [function.mysql-result]: product_name not found in MySQL result index 6 in C:\wamp\www\pagination.php on line 144

 

Warning: mysql_result() [function.mysql-result]: product_line not found in MySQL result index 6 in C:\wamp\www\pagination.php on line 145

 

Warning: mysql_result() [function.mysql-result]: product_description not found in MySQL result index 6 in C:\wamp\www\pagination.php on line 148

 

Warning: mysql_result() [function.mysql-result]: image not found in MySQL result index 6 in C:\wamp\www\pagination.php on line 150

 

Warning: mysql_result() [function.mysql-result]: price not found in MySQL result index 6 in C:\wamp\www\pagination.php on line 152

 

 

 

 

Can someone please tell me where im going wrong?

 

Many thanks for any help!!

Link to comment
https://forums.phpfreaks.com/topic/157063-pagination-help/
Share on other sites

Update:

 

I commented out the line:

 

  $result=mysql_query($query);

 

 

 

and the page now displays the product name, productline, the image, the description and the price....but no page numbers and im still getting these errors:

 

Notice: Use of undefined constant num - assumed 'num' in C:\wamp\www\pagination.php on line 23

 

Notice: Undefined index: page in C:\wamp\www\pagination.php on line 28

Link to comment
https://forums.phpfreaks.com/topic/157063-pagination-help/#findComment-827364
Share on other sites

Please post all the code or highlight what line it actually is. Line 23 shows blank for me.

 

Heres all of the code I have so far:

 

<?php

$username="root";
$password="";
$database="productDatabase";
$host="localhost";

mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

include('db.php');	// include your code to connect to DB.

$tbl_name="products";		//your table name
// How many adjacent pages should be shown on each side?
$adjacents = 2;

/* 
   First get total number of rows in data table. 
   If you have a WHERE clause in your query, make sure you mirror it here.
*/
$query = "SELECT COUNT(*) as num FROM $tbl_name";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

/* Setup vars for query. */
$targetpage = "pagination.php"; 	//your file name  (the name of this file)
$limit = 2; 								//how many items to show per page
$page = $_GET['page'];
if($page) 
	$start = ($page - 1) * $limit; 			//first item to display on this page
else
	$start = 0;								//if no page var is given, set start to 0

/* Get data. */
$sql = "SELECT * FROM $tbl_name LIMIT $start, $limit";
$result = mysql_query($sql);

/* Setup page vars for display. */
if ($page == 0) $page = 1;					//if no page var is given, default to 1.
$prev = $page - 1;							//previous page is page - 1
$next = $page + 1;							//next page is page + 1
$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1;						//last page minus 1

/* 
	Now we apply our rules and draw the pagination object. 
	We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{	
	$pagination .= "<div class=\"pagination\">";
	//previous button
	if ($page > 1) 
		$pagination.= "<a href=\"$targetpage?page=$prev\">« previous</a>";
	else
		$pagination.= "<span class=\"disabled\">« previous</span>";	

	//pages	
	if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
	{	
		for ($counter = 1; $counter <= $lastpage; $counter++)
		{
			if ($counter == $page)
				$pagination.= "<span class=\"current\">$counter</span>";
			else
				$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
		}
	}
	elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
	{
		//close to beginning; only hide later pages
		if($page < 1 + ($adjacents * 2))		
		{
			for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
			{
				if ($counter == $page)
					$pagination.= "<span class=\"current\">$counter</span>";
				else
					$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
			}
			$pagination.= "...";
			$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
			$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";		
		}
		//in middle; hide some front and some back
		elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
		{
			$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
			$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
			$pagination.= "...";
			for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
			{
				if ($counter == $page)
					$pagination.= "<span class=\"current\">$counter</span>";
				else
					$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
			}
			$pagination.= "...";
			$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
			$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";		
		}
		//close to end; only hide early pages
		else
		{
			$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
			$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
			$pagination.= "...";
			for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
			{
				if ($counter == $page)
					$pagination.= "<span class=\"current\">$counter</span>";
				else
					$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
			}
		}
	}

	//next button
	if ($page < $counter - 1) 
		$pagination.= "<a href=\"$targetpage?page=$next\">next »</a>";
	else
		$pagination.= "<span class=\"disabled\">next »</span>";
	$pagination.= "</div>\n";		
}
?>

<?php 
    $result = mysql_query("SELECT * FROM $tbl_name LIMIT $start, $limit"); // rest of query 
    $i=0; 
    $num=mysql_numrows($result); 
    while($row = mysql_fetch_array($result)) { 
        if ($i < $num) { 
     
            $prod_name = $row['prod_name']; 
            $prod_line = $row['prod_line']; 
            $prod_description = $row['prod_description']; 
            $image = $row['image']; 
            $price = $row['price']; 

            echo ' 
        <span style="color: #000000;"> <u>'.$prod_name.'</u> 
            <br /><br /> '.$prod_line.'<br /><br /> 
        <img alt="product image" width="250" height="265" src="images/'.$image.'" /><br /> 
        <u>Product Description</u> 
            <br /><br /> '.$prod_description.'<br /><br /> 
            £'.$price.'<hr /><br /></span>'; 

    $i++; 
        } 
    } 
?>

<?=$pagination?>

Link to comment
https://forums.phpfreaks.com/topic/157063-pagination-help/#findComment-827505
Share on other sites

<?php
while($row = mysql_fetch_assoc($result)) {
    $product_name=$row['product_name'];
    $product_line=$row['product_line'];
    $product_description=$row['product_description'];
    $image=$row['image'];
    $price=$row['price'];

    echo "<font color=\"white\"><u>$product_name</u></b><br><br>$product_line<br><br></font>";
    echo "<img alt=\"product image\" width=\"250\" height=\"265\" src=\"images/".$image." \"/>";
    echo "<font color=\"white\"></br><u>Product Description</u><br><br>$product_description<br><br>£$price<hr><br></font>";
    
}

Link to comment
https://forums.phpfreaks.com/topic/157063-pagination-help/#findComment-827523
Share on other sites

Here is one of the errors:

<?php
$total_pages = $total_pages[num];
?>

 

It shoudl be

<?php
$total_pages = $total_pages['num'];
?>

 

And error on line 28:

<?php
$page = $_GET['page'];
?>

 

That means that your addressbar doesn't look like: pagination.php?page=blah

Make sure "page" is being passed.

Link to comment
https://forums.phpfreaks.com/topic/157063-pagination-help/#findComment-827535
Share on other sites

Here is one of the errors:

<?php
$total_pages = $total_pages[num];
?>

 

It shoudl be

<?php
$total_pages = $total_pages['num'];
?>

 

And error on line 28:

<?php
$page = $_GET['page'];
?>

 

That means that your addressbar doesn't look like: pagination.php?page=blah

Make sure "page" is being passed.

 

Thanks, i sorted the first error. I know this is gonna sound quite stupid, but how do I pass 'page'? :-[

Link to comment
https://forums.phpfreaks.com/topic/157063-pagination-help/#findComment-827552
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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