SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Dec 2004
    Location
    england
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    slight pagination problem....

    hey people im running into a few problems with a pagination script, its kinda working but not! It all depends on how many products i tell it to display ($display = 8). however i just want it working so that it displays 10 products (i realise i have it at 8 at present - thats because when i make it ten it breaks!) and all the links work correctly.....anyone tell me where im going wrong?

    PHP Code:
    <?php
    include('admin/db.php');

    $error = array();
    $results = array();
    $display 8/* changing this makes problems!*/

    $start = isset($_GET['s']) ? $_GET['s'] : 0;
    $end = isset($_GET['e']) ? $_GET['e'] : $display;
    $page = isset($_GET['page']) ? $_GET['page'] : 1;

    $query "SELECT * FROM maryproducts WHERE area='kitchenalia' ORDER BY `ID` DESC"

    /* run the query */
    $search mysql_query($query) or die(mysql_error());
    /* check for returned results */
    $num_rows mysql_num_rows($search);
    if(
    $num_rows == 0) {
    $error['no_products'] = 'Sorry, there were no products found in this category';
    } else {
    /* dump the results into an array */
    while($row mysql_fetch_assoc($search)) {
        
    $id $row['id'];
        
    $name $row['name'];
        
    $shortdescripton $row['shortdescription'];
        
    $descripton $row['description'];
        
    $price $row['price'];
        
    $ref $row['ref'];
        
    $img1 $row['img1'];
        
    $img2 $row['img2'];
        
    $img3 $row['img3'];
        
    $img4 $row['img4'];
        
    $img5 $row['img5'];
        
    $img6 $row['img6'];
        
    $area $row['area'];

        
    $results[] = '

        <strong>'
    .$name.'</strong><br>
        
        <a href="#" onclick="MM_openBrWindow(\'fullDes.php?id='
    .$id.'\',\''.$id.'\',\'scrollbars=yes,width=600,height=600\')">    
        <img src="'
    .$thumbnail_folder.''.$img1.'"  border="0"></a>
        
        '
    .$shortdescriptopn.'<br>
        
        <strong>Price: </strong>'
    .$price.'<br>
     
    <a href="#" onclick="MM_openBrWindow(\'fullDes.php?id='
    .$id.'\',\''.$id.'\',\'scrollbars=yes,width=600,height=600\')">further image and description</a><br><br>

        '
    ;
                }
              } 
    ?>

    <div class="pagination">
    <?php
    if($num_rows == 0) {
        echo 
    $error['no_products'];  
        } else {
    #echo '<pre>';
    #print_r($results);
    #echo '</pre>';
    echo '<strong>There are currently '.$num_rows .' products in the 1920\'s - 1950\'s kitchenalia range</strong> <br/><br/>';

    for(
    $i=$start$i<$end$i++) {
         echo 
    $results[$i] . '<br />';
         }

    /* array pagination */
        /* Check that the total number of returned rows is greater than the number to be displayed */
                    
    $total round(count($results)/$display);
                    
    //$qs = '&category='. $category .'&region='.$Sregion.'&priceTo='.$priceTo.'&priceFrom='.$priceFrom;
                // define number loop - basic
                        
    $t=isset($_GET['t']) ? $_GET['t'] : 1;
                if(
    $total >1) {
                        
    $link '<a href="?s=0&e='$display .'&page=1">First</a> - ';
                                                
                        for(
    $j=0$j<=($total-1); $j++) {
                    
    // check if page number is = to $j + 1
                        
    if($j+== $page) {
                            
    $link .= '['$page .']';
                        } else {
                            
    $link .= ' <a href="?s='$j*$display .'
                            &e='
    . (($j*$display)+$display) .'&t='. ($j+1) .'&page='. ($j+1) .'">';
                            
                            
    $link .= $j+1;
                            
    $link .= '</a> ';
                            }
                        }
                        
    #}
                        
    $link .= ' - <a href="?s='. (($total*$display)-$display) .'
                        &e=' 
    round($total*$display) .'&page='. ($total) .'
                        "> Last </a>'
    ;    
                        
                        
    // print the loop        
                        
    echo $link;
                    }
                }
    ?>
    heres the online page...

    http://www.hunterjohnstone-info.co.u...itchenalia.php

  2. #2
    SitePoint Evangelist ClickHeRe's Avatar
    Join Date
    Mar 2005
    Location
    Ottawa, Canada
    Posts
    580
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you should use the LIMIT clause in your SQL statement instead

    LIMIT M,N where M is the offset in rows (0 for the beginning) and N is the number of records to fetch.

    Then your loop can be a while ( $row = mysql_fetch_array( $result ) ) without worrying about the actual number of records since the SQL statement will limit it.

    I would suggest searching the forum for these pagination questions, as they are asked about every week and you may find a suitable code example on achieving this properly.
    David

  3. #3
    SitePoint Addict
    Join Date
    Dec 2004
    Location
    england
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    fixed! cheers!
    Last edited by deelunn; May 8, 2006 at 08:08.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •