Current location: Hot Scripts Forums » Programming Languages » PHP » Paging listed results


Paging listed results

Reply
  #1 (permalink)  
Old 09-03-04, 06:12 AM
djnaf's Avatar
djnaf djnaf is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Question Paging listed results

Hello;
i wrote a php code that list all the products in my database table, and i want to divide the results (every 3 products listed in a page), the table contains 5 fields (prodname, specs, price, picname, id (Auto Increment)). The code below is not working fine on phpdev, there is no error, just logic error.
when i use it to display results, for example i have 5 products in my table. it displays 3 products in the first page (prod5,prod4,prod3) , and page 2, it displays them again. not (prod2, prod1).
i think the problem is in $row = mysql_fetch_array($result);
i thought of using if statement but didnt manage to get it!

what confuses me, that i uploaded the code in my website, and it worked fine!!! so i need help to make it work on my website and phpdev please

PHP Code:

if ($step == NULL)

{
$step=0;
$step2=$step+3;
}else{
$step2=$step+3;
}

$count=0;

$query1 "SELECT * FROM prod ORDER BY id DESC";
$result1 mysql_query($query1);
while (
$row mysql_fetch_array($result1))
{
$count++;
}
//63
$query "SELECT * FROM prod ORDER BY id DESC LIMIT ".addslashes("$step").",4";
$result mysql_query($query);

if (
$count == 0)
{
echo
"No results found";
}


if (
$count 4)
{
$min 1;
}else{
$min 0;
}
//$b1=0;
$ccc=0;
for (
$c 1$c <= 3$c++)
{
$ccc++;
//$b1++;
//$row=$count-$row[0];
$row mysql_fetch_array($result);
$prodname$row["prodname"];
$price$row["price"];
$specs$row["specs"];
$picname$row["picname"];
if (
$picname == NULL)
break;
echo 
"$ccc-  $prodname$specs$price$picname<BR>";
}

echo 
"<center>";
for (
$l=0;$l<=$count;$l=$l+3)
{
if (
$l == $step)
{
echo 
"[".($l/3+1)."]";
echo 
" ";
}else{
echo 
"<a href=\"blabla.php?step=$l\">[".($l/3+1)."]</a>";
echo 
" ";
}

Reply With Quote
  #2 (permalink)  
Old 09-04-04, 09:25 AM
djnaf's Avatar
djnaf djnaf is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
OR if anyone have another code to display products from table, and divide products to be viewed 5 items per page for example
THANKS
Reply With Quote
  #3 (permalink)  
Old 09-04-04, 10:21 AM
PromptLogic PromptLogic is offline
Newbie Coder
 
Join Date: Sep 2004
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
There was alot of unnessecary things being done in your code. Instead of writing something from scratch, this is from a tutorial from the great people at PHPNoise. I modified it to work for what you need. Let me know of this works for you.

PHP Code:

<?

class Pager 

   function 
getPagerData($numHits$limit$page)  { 
        
$numHits = (int) $numHits
        
$limit max((int) $limit1); 
        
$page = (int) $page;  
        
$numPages ceil($numHits $limit); 

        
$page max($page1); 
        
$page min($page$numPages); 

        
$offset = ($page 1) * $limit

        
$ret = new stdClass

        
$ret->offset $offset
        
$ret->limit $limit
        
$ret->numPages $numPages
        
$ret->page $page

        return 
$ret
  } 
}

    
// get the pager input values 
    
$page $_GET['page'];  
    
$limit 3;  
    
$result mysql_query("select count(*) from prod");  
    
$total mysql_result($result00);  

    
// work out the pager values 
    
$pager  Pager::getPagerData($total$limit$page);  
    
$offset $pager->offset;  
    
$limit  $pager->limit;  
    
$page   $pager->page;  

    
// use pager values to fetch data 
    
$query "select * from prod order by id DESC limit $offset$limit";  
    
$result mysql_query($query);  

    
// use $result here to output page content 

    // output paging system (could also do it before we output the page content) 
    
if ($page == 1// this is the first page - there is no previous page 
        
echo "Previous";  
    else            
// not the first page, link to the previous page 
        
echo "<a href=\"blabla.php?page=" . ($page 1) . "\">Previous</a>";  

    for (
$i 1$i <= $pager->numPages$i++) {  
        echo 
" | ";  
        if (
$i == $pager->page)  
            echo 
"Page $i";  
        else  
            echo 
"<a href=\"blabla.php?page=$i\">Page $i</a>";  
    }  

    if (
$page == $pager->numPages// this is the last page - there is no next page 
        
echo "Next";  
    else            
// not the last page, link to the next page 
        
echo "<a href=\"blabla.php?page=" . ($page 1) . "\">Next</a>";
?>

A couple tricks you should pick up is that if you need a record count, do it within MySQL as opposed to looping and counting in PHP. Another thing is your naming conventions for variables. I got lost pretty quick trying to remember what variable was for what. Don't worry, I do the same thing all the time but it's good practice, and easier for you in the long run with good names.
Reply With Quote
  #4 (permalink)  
Old 09-06-04, 08:44 AM
djnaf's Avatar
djnaf djnaf is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks alot
it worked fine cool
but i wonder whats wrong in my code!
__________________
Code Talking Forum
Programming & Scripts Forum
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP, MySQL Beginner... Help Needed Displaying Results db3db3 PHP 1 05-12-04 01:24 AM
posting of results script Sir-ihlatrebon Script Requests 0 02-17-04 11:46 AM
results page to display prev/next php-learner PHP 11 01-12-04 09:40 AM
Which way to display results from mysql Peter_web PHP 2 09-13-03 05:39 PM
Database results paging class jv2222 PHP 0 08-03-03 04:49 AM


All times are GMT -5. The time now is 04:25 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.