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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    New Coder
    Join Date
    Mar 2008
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    next/previous links

    Hi there,

    I would like to add nex/previous buttons(links) to my current script and also a number showing wich page is displayed of the total (like: page 6/12). Not with a row of numbers like in many examples on internet.
    I am showing one record per page.

    My question: Should/could still use the script/structure like in the examples, to eventualy accomplish my goal?

    Tricky part is the fact that i want to navigate through a selected category (when selected) OR through all records (all assigments (ID)), depending on wich action the user

    My code:
    Code:
    <?
    $query = " 
        SELECT 
            id, 
            name, 
            label, 
            tekst, 
            category, 
            klant, 
            DATE_FORMAT(datum, '%d-%m-%Y') AS nl_datum 
        FROM 
            upload
    ";
    
    if(isset($_GET['id']))
    {
        $query .= " WHERE id = '".mysql_real_escape_string($_GET['id'])."'";
    
    }
    elseif(isset($_GET['category']))
    {
        $query .= " WHERE category = '".mysql_real_escape_string($_GET['category'])."'";
    }
    else
    {
        echo 'No id or category given';
    }
    
        
        $result = mysql_query($query) or die('Error : ' . mysql_error()); 
        $myrow = mysql_fetch_assoc($result); 
    
    
    
    ?>
    
    <!-- ### SHOW WORK ######-->
    
    <div >
        <div>
            <h3> <? echo $myrow['klant'] ?></h3>
            <p class="newsdate">Geplaatst op: <? echo $myrow['nl_datum'] ?></p>
                    <p class="newsdate">Middel: <? echo $myrow['category'] ?></p>
                    <p class="newsdate">Opdracht: <? echo $myrow['label'] ?></p>
    
    
            <? echo '<img class=plaatje src="image/' . $myrow['name'] . '" />' ; ?>
            <p class="newstext"> <? echo ($myrow['tekst']) ?> </p>   
        </div>
    </div> ?>
    I have no clue how to realize this within my two different querys..

    thnx in advance for any tips/help..!
    Last edited by myrok; 01-13-2009 at 04:43 PM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I'm sorry, I can't visualize your data or how you are displaying it. Could you explain that part or provide a link to a test page or a screenshot or something?

  • #3
    New Coder
    Join Date
    Mar 2008
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sure:
    http://blikvoer.com/temp/samtest/ind...gina=portfolio

    The first shown page is going to be blanc later on..

    so: two different nex/previous "situations". 1.: within category (after seleceted "middelen")
    2. within all (after a selected "klanten" id..)

  • #4
    New Coder
    Join Date
    Mar 2008
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    any idea?

  • #5
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    code below will do what you want change $page_rows = 4; to $page_rows = 1;

    then alter the way it displays and obviously your db details and tables etc

    PHP Code:
    <?php
    // Connects to your Database
    mysql_connect("your.hostaddress.com""username""password") or die(mysql_error());
    mysql_select_db("address") or die(mysql_error());

    //This checks to see if there is a page number. If not, it will set it to page 1
    if (!(isset($pagenum)))
    {
    $pagenum 1;
    }

    //Here we count the number of results
    //Edit $data to be your query
    $data mysql_query("SELECT * FROM topsites") or die(mysql_error());
    $rows mysql_num_rows($data);

    //This is the number of results displayed per page
    [B]$page_rows 4;[/B]

    //This tells us the page number of our last page
    $last ceil($rows/$page_rows);

    //this makes sure the page number isn't below one, or more than our maximum pages
    if ($pagenum 1)
    {
    $pagenum 1;
    }
    elseif (
    $pagenum $last)
    {
    $pagenum $last;
    }

    //This sets the range to display in our query
    $max 'limit ' .($pagenum 1) * $page_rows .',' .$page_rows

    //This is your query again, the same one... the only difference is we add $max into it
    $data_p mysql_query("SELECT * FROM topsites $max") or die(mysql_error());

    //This is where you display your query results
    while($info mysql_fetch_array$data_p ))
    {
    Print 
    $info['Name'];
    echo 
    "<br>";
    }
    echo 
    "<p>";

    // This shows the user what page they are on, and the total number of pages
    echo " --Page $pagenum of $last-- <p>";

    // First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
    if ($pagenum == 1)
    {
    }
    else
    {
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
    echo 
    " ";
    $previous $pagenum-1;
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
    }

    //just a spacer
    echo " ---- ";

    //This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
    if ($pagenum == $last)
    {
    }
    else {
    $next $pagenum+1;
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
    echo 
    " ";
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
    }
    ?>
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #6
    New Coder
    Join Date
    Mar 2008
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So you think that i can use youre code for my situation? :

    More detailed, for a complete image. In another page i get the category and client list.:
    grouped by category:
    Code:
     
    <a href="index.php?pagina=portfolio&category=<?php echo $category;?>"><? echo $category ?></a>
    and grouped by client (id):
    Code:
    <a  href="index.php?pagina=portfolio&id=<?php echo $id;?>"><? echo $klant ?></a>
    Wich i then get in another php page, with this code:
    PHP Code:
     <?php

    $query 

        SELECT 
            id, 
            name, 
            label, 
            tekst, 
            category, 
            klant, 
            DATE_FORMAT(datum, '%d-%m-%Y') AS nl_datum 
        FROM 
            upload
    "
    ;

    if(isset(
    $_GET['id']))
    {
        
    $query .= " WHERE id = '".mysql_real_escape_string($_GET['id'])."'";

    }
    elseif(isset(
    $_GET['category']))
    {
        
    $query .= " WHERE category = '".mysql_real_escape_string($_GET['category'])."'";
    }
    else
    {
        echo 
    'Geen id of categorie opgegeven';
    }

        
        
    $result mysql_query($query) or die('Error : ' mysql_error()); 
        
    $myrow mysql_fetch_assoc($result); 



    ?>

    <!-- ### SHOW WORK ######-->

    <div >
        <div>
            <h3> <? echo $myrow['klant'?></h3>
            <p class="newsdate">Geplaatst op: <? echo $myrow['nl_datum'?></p>
                    <p class="newsdate">Middel: <? echo $myrow['category'?></p>
                    <p class="newsdate">Opdracht: <? echo $myrow['label'?></p>


            <? echo '<img class=plaatje src="image/' $myrow['name'] . '" />' ?>
            <p class="newstext"> <? echo ($myrow['tekst']) ?> </p>   
        </div>
    </div> ?>
    And now i want to "skip" within category OR within all "id's". Based on the user choice in the other page (first codes)
    But i don't understand how i should do this with pagination.. wich is mostly based on one choice.
    preview: http://blikvoer.com/temp/samtest/ind...gina=portfolio
    Last edited by myrok; 01-19-2009 at 11:56 AM.

  • #7
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    yeah you just have to update the page count number within the script and customise the select within the script i posted to match your conditions
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #8
    New Coder
    Join Date
    Mar 2008
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I now have this: (im a noob at this things sry..)

    PHP Code:
     <?
    if*(!(isset($pagenum)))
    {
    $pagenum*=*1;
    }

    //Here*we*count*the*number*of*results
    //Edit*$data*to*be*your*query
    $data*=*mysql_query(
        SELECT 
            id, 
            name, 
            label, 
            tekst, 
            category, 
            klant, 
            DATE_FORMAT(datum, '%d-%m-%Y') AS nl_datum 
        FROM 
            upload
    "
    )*or*die(mysql_error());

    if(isset(
    $_GET['id']))
    {
        
    $data .= " WHERE id = '".mysql_real_escape_string($_GET['id'])."'";

    }
    elseif(isset(
    $_GET['category']))
    {
        
    $data .= " WHERE category = '".mysql_real_escape_string($_GET['category'])."'";
    }
    else
    {
        echo 
    'Geen id of categorie opgegeven';
    }

    $myrow*=*mysql_num_rows($data);

    //This*is*the*number*of*results*displayed*per*page
    $page_rows*=*1;

    //This*tells*us*the*page*number*of*our*last*page
    $last*=*ceil($myrow/$page_rows);

    //this*makes*sure*the*page*number*isn't*below*one,*or*more*than*our*maximum*pages
    if*($pagenum*<*1)
    {
    $pagenum*=*1;
    }
    elseif*(
    $pagenum*>*$last)
    {
    $pagenum*=*$last;
    }

    //This*sets*the*range*to*display*in*our*query
    $max*=*'limit*'*.($pagenum*-*1)***$page_rows*.','*.$page_rows;*

    //This*is*your*query*again,*the*same*one...*the*only*difference*is*we*add*$max*into*it
    $data_p*=*mysql_query("SELECT***FROM*topsites*$max")*or*die(mysql_error());

    //This*is*where*you*display*your*query*results
    while($info*=*mysql_fetch_array(*$data_p*))
    {
    Print*
    $info['klant'];
    Print*
    $info['category'];
    Print*
    $info['label'];

    echo*
    "<br>";
    }
    echo*
    "<p>";

    //*This*shows*the*user*what*page*they*are*on,*and*the*total*number*of*pages
    echo*"*--Page*$pagenum*of*$last--*<p>";

    //*First*we*check*if*we*are*on*page*one.*If*we*are*then*we*don't*need*a*link*to*the*previous*page*or*the*first*page*so*we*do*nothing.*If*we*aren't*then*we*generate*links*to*the*first*page,*and*to*the*previous*page.
    if*($pagenum*==*1)
    {
    }
    else
    {
    echo*
    "*<a*href='{$_SERVER['PHP_SELF']}?pagenum=1'>*<<-First</a>*";
    echo*
    "*";
    $previous*=*$pagenum-1;
    echo*
    "*<a*href='{$_SERVER['PHP_SELF']}?pagenum=$previous'>*<-Previous</a>*";
    }

    //just*a*spacer
    echo*"*----*";

    //This*does*the*same*as*above,*only*checking*if*we*are*on*the*last*page,*and*then*generating*the*Next*and*Last*links
    if*($pagenum*==*$last)
    {
    }
    else*{
    $next*=*$pagenum+1;
    echo*
    "*<a*href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next*-></a>*";
    echo*
    "*";
    echo*
    "*<a*href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last*->></a>*";
    }
    ?>
    But getting a Parse error: syntax error, unexpected '{' on
    PHP Code:
     if (!(isset($pagenum)))
    {
    $pagenum 1;

    Last edited by myrok; 01-19-2009 at 12:52 PM.

  • #9
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    well in what you've psted above you've got * in all over the place

    PHP Code:
    if*(!(isset($pagenum)))
    {
    $pagenum*=*1;

    if you remove the * it should work
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #10
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    you'll also want to change the 2nd query too

    //This*is*your*query*again,*the*same*one...*the*only*difference*is*we*add*$max*into*it
    $data_p*=*mysql_query("SELECT***FROM*topsites*$max")*or*die(mysql_error());
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #11
    New Coder
    Join Date
    Mar 2008
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok am back. I splitted my previous code to two different ones (to keep it a bit more orchanized for me)
    But still i have this "page" question in the url..

    My code now (for one of the pages (klanten.php ->clients)
    PHP Code:
    <?
    if (!isset($_GET['klant'])) {
        
    // kijken of klant is verkregen
        
    echo "klant niet gedefineerd";
    } else {

        
    // category pakken
         
    $query 
            SELECT 
                id, 
                name, 
                label, 
                tekst, 
                category, 
                klant, 
                DATE_FORMAT(datum, '%d-%m-%Y') AS nl_datum 
            FROM 
                upload  
            WHERE
            klant = '"
    .mysql_real_escape_string($_GET['klant'])."'
            ORDER BY datum DESC
        "

        
    $result mysql_query($query) or die('Error : ' mysql_error()); 
        
        while (
    $myrow mysql_fetch_assoc($result))

    {

    ?>
    <!-- ### WERK TONEN ######-->
    <div style="width:500px;background-color:#0CF; float:right">
    <div >
        <div>
            <h3> <? echo $myrow['klant'?></h3>
            <p class="newsdate">Geplaatst op: <? echo $myrow['nl_datum'?></p>
                    <p class="newsdate">Middel: <? echo $myrow['category'?></p>
                    <p class="newsdate">Opdracht: <? echo $myrow['label'?></p>


            <? echo '<img class=plaatje src="image/' $myrow['name'] . '" />' ?>
            <p class="newstext"> <? echo ($myrow['tekst']) ?> </p>   
        </div>
    </div>

    </div>

    <?php 
        
    // while afsluiten.
        
    // (else..) aflsuiten..
    ?>
    I've tried several examples concerning pagination. But i'm stuck at this part:
    PHP Code:
    if(!isset($_GET['page'])){ 
        
    $page 1
    } else { 
        
    $page $_GET['page']; 

    Because i'm already sending a variable (if (!isset($_GET['klant']))... )
    in the page before:
    Code:
    <a href="index.php?pagina=klanten&klant=<?php echo $klant;?>"><? echo $klant ?>
    I just don't get (understand) how to rebuilt this for my code..
    example: http://blikvoer.com/temp/samtest/ind...ten&klant=Anwb

  • #12
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    if(!isset($_GET['page'])){
    $page = 1;
    } else {
    $page = $_GET['page'];
    }

    is the page number for the sql queries its got nothing to do with your page you need to load
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #13
    New Coder
    Join Date
    Mar 2008
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    so there won't be a
    ../index.php?pagina=portfolio&category=Boeken&page=1 visible?.. or..

  • #14
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    yes it'll be visible what that if statement does is tell it that if the page=xxx header querystring doesn't exist then it needs to start at page 1 else start at the page as supplied by the querystring.
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #15
    New Coder
    Join Date
    Mar 2008
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It's almost working now. Only problem is the next (previous ed..) button.
    It's not giving the right url output.

    whole code:
    PHP Code:
    <? if (isset($_GET['pagina'])) {
       
    $pagina $_GET['pagina'];
    } else {
       
    $pagina 1;


    $query "SELECT count(*) 
            FROM 
                upload  
            WHERE
            klant = '"
    .mysql_real_escape_string($_GET['klant'])."'
    "
    ;
    $result mysql_query($query) or trigger_error("SQL"E_USER_ERROR);

    $query_data mysql_fetch_row($result);
    $numrows $query_data[0];

    $rows_per_page 1;
    $lastpage      ceil($numrows/$rows_per_page);

    $pagina = (int)$pagina;
    if (
    $pagina $lastpage) {
       
    $pagina $lastpage;

    if (
    $pagina 1) {
       
    $pagina 1;





    $limit 'LIMIT ' .($pagina 1) * $rows_per_page .',' .$rows_per_page;


    $query "
            SELECT 
                id, 
                name, 
                label, 
                tekst, 
                category, 
                klant, 
                DATE_FORMAT(datum, '%d-%m-%Y') AS nl_datum 
            FROM 
                upload  
                WHERE       klant = '"
    .mysql_real_escape_string($_GET['klant'])."'

        $limit"
    ;    
    $result mysql_query($query) or trigger_error("SQL"E_USER_ERROR);

        while (
    $myrow mysql_fetch_assoc($result))

    {


    ?>


    <!-- ### SHOW WORK ######-->
    <div style="width:500px;background-color:#0CF; float:right">
    <div >
        <div>
            <h3> <? echo $myrow['klant'?></h3>
            <p class="newsdate">Geplaatst op: <? echo $myrow['nl_datum'?></p>
                    <p class="newsdate">Middel: <? echo $myrow['category'?></p>
                    <p class="newsdate">Opdracht: <? echo $myrow['label'?></p>


            <? echo '<img class=plaatje src="image/' $myrow['name'] . '" />' ?>
            <p class="newstext"> <? echo ($myrow['tekst']) ?> </p>   
        </div>
    </div>

    </div>


    <?php 
        
    // (else..) close..
    ?>


    <?

    if ($pagina == 1) {
       echo 
    " EERSTE VORIGE";
    } else {
       echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagina=1'>EERSTE</a> ";
       
    $prevpage $pagina-1;
       echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagina=$prevpage'>VORIGE</a> ";


    echo 
    " ( pagina $pagina / $lastpage ) ";

    if (
    $pagina == $lastpage) {
       echo 
    " VOLGENDE LAATSTE ";
    } else {
       
    $nextpage $pagina+1;
       echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagina=$nextpage'>VOLGENDE</a> ";
       echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagina=$lastpage'>LAATSTE</a> ";


    ?>
    I think the problem is in {$_SERVER['PHP_SELF']}, because the url is: ./index.php?pagina=2 instead of index.php?pagina=klanten&klant=Anwb?pagina=2
    (or someting like that..)
    but i cannot find the exact problem..

    Any idea?


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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