Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts

    Pagination for beginners

    Easy pagination script i want to share with rest of the world is maybe a first script i wrote that works from start till end

    So here's the code, i explaind in the comments.

    Can't wait to hear your comments!

    PHP Code:
    <?php

    //TEST ARRAY $pagination We use for this script
    //Can be generated with mysql_fetch_row, or some sort of explode() functions
    //even can be flat file
    $pagination = array(=> "Page1"=> "Page2"=> "Page3"=> "Page4"=> "Page5");


    //simple "preloader" although it has nothing to do with the
    //preloading, i just love to use that name, that and ZeroCool.. 
    if (isset($_GET['id'])) {
        
    $id =$_GET['id'];
        if (
    $id count($pagination)) {
        
    $id 1;
        }
    }
    else {
        
    $id 1;
    }

    ///
    ///Some random HTML file used to generate output..
    ///we could call that pagination.html

    $page file_get_contents("paginationtest.html");

    ### paginationtest.html ###
    /*
    <style type="text/css">
    #site {
        width: 400px;
    }
    #content {
        font-size: 20px;
        width: 380px;
        margin: 0 auto;
        background: #cccccc;
    }
    #links {
        text-align: center;
    }
    #links p.left {
        width: 190px;
        float: left;
        text-align: right;
        text-indent: 10px;    
    }
    #links p.right {
        width: 190px;
        float: left;
        text-align: left;
        text-indent: 10px;
    }
    </style>
    <div id="site">
    <div id="content">
    %content%

    <div id="links">
    <p class="left">%left%</p><p class="right">%right%</p>
    </div>
    </div>
    </div> */
    #############################

    //just a variable to MATCH content with
    $MATCHcontent "/%content%/";
    //real content from VALUE of pagination array
    $VALUEcontent $pagination[$id];
    //preg_replace to switch MATCH with real VALUES for CONTENT
    $page preg_replace($MATCHcontent$VALUEcontent$page);

    //preloader function as above, just this one is used to
    //generade $_GET['id'] variable
    $left $id-1;
    if (
    $left<=0) {
        
    $left "";
    }
    else {
        
    //and to asign some fine text so we could
        //generate links
        
    $left '<a href="?id='.$left.'">LEFT</a>';;
    }
    //preloader function as above but for oposite
    //getting next button
    $right $id+1;
    $max count($pagination);
    $right $id+1;
    if (
    $right>$max) {
        
    $right "";
    }
    else {
        
    $right '<a href="?id='.$right.'">RIGHT</a>';;
    }
    //and similar to above just this time array to spare some typing
    $MATCHleft     = ("/%left%/");
    $MATCHright     = ("/%right%/");

    $page preg_replace (array($MATCHleft$MATCHright), array($left$right), $page);
    ##page variable should have some paggination
    print $page;


    ?>
    Last edited by funnymoney; 03-17-2009 at 11:45 PM. Reason: simplify the HTML file


 

Posting Permissions

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