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 17
  1. #1
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    90
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Help with Pagination and holding the drop down form value for Next Page!

    Hi, I've been banging my head against a wall for days now, and i just cant solve my little problem.

    I have compiled this script below on 1 page called test.php: which pulls data from my DB. I have a drop down form that should only pull data from the DB by the value of the MONTH.

    I fould a tutorial to add pagination so that i can limit the data on the page to 15 rows, and then click > next to view the next 15 and so-on, this seems to work fine.

    But

    1. On page load the entire DB is loaded, i want nothing until a month is selected from the drop down form.

    2. When for e.g February is selected from drop down, it show the 1st 15 records/rows perfectly, but when you hit the > next page link it reverts back to showing the entire DB and not only the continued February records.

    Please stop me from smashing my head against this wall any longer.
    The biggest thankyou in-advance.
    P.S I'm am a PHP Novice
    Jenna

    Code:
    <?
    
    echo "
    <form method=post action=test.php>
    <select name=month>
    <option value='Select'>Select</option>
    <option value='January'>January</option>
    <option value='February'>February</option>
    <option value='March'>March</option>
    <option value='April'>April</option>
    <option value='May'>May</option>
    <option value='June'>June</option>
    <option value='July'>July</option>
    <option value='August'>August</option>
    <option value='September'>September</option>
    <option value='october'>October</option>
    <option value='November'>November</option>
    <option value='December'>December</option>
    </select>
    <input type=submit value=Select name=select>
    ";
    
    ?>
     
    
        <?php
    
    // How many entries you want to show per page
    $perpage = 15;
    
    // Find out what page of entries you are looking for
    // The reason why you subtract 1 is so that you can calculate which returned entry (for later when we query the database) to start at
    if (isset($_GET['page'])) {
        $page = max(intval($_GET['page'] - 1), 0);
        $page_original = intval($_GET['page']);
    }
    else {
        $page = 0;
        $page_original = 1;
    }
    
    $start_at = $page * $perpage;
    $start_at_public = $start_at + 1;
    
    mysql_connect('localhost', 'USER', 'PASS');
    mysql_select_db('DATABASE') or die( 'Unable to select database');
    
    // How many entries are there?
    $query = "SELECT COUNT(*) FROM payments WHERE date LIKE '%$month%' ";
    $result = mysql_query($query) or die('Error in query: ' . $query);
    $total_entries = mysql_result($result, 0);
    
    // You've asked for a page that is too high
    if ($total_entries < $start_at_public) {
        die('No Queries Found.');
    }
    
    // Grab the specified number of entries, starting at the appropriate record
    $query2 = "select * FROM payments WHERE date LIKE '%$month%' ORDER by id DESC LIMIT $start_at, $perpage";
    $result2 = mysql_query($query2);
    $num=mysql_numrows($result2);
    
    // Print those entries!
    while ($entries = mysql_fetch_object($result2)) {
        print '<div class="entry">' . "\n";
        print '<h2>' . $entries->title . '</h2>' . "\n";
        print '<p>' . $entries->content . '</p>' . "\n";
        print '</div>' . "\n";
    }
    
    mysql_close();
    
    $end_value_this_page = min($start_at + $perpage, $total_entries);
    
    if (($end_value_this_page == $total_entries) && ($start_at_public == $total_entries)) {
        print '<p>Entry ' . $end_value_this_page . ' of ' . $total_entries . '</p>' . "\n";
    }
    
    else {
        print '<p>Entries ' . ($start_at_public) . '-' . $end_value_this_page . ' of ' . $total_entries . '</p>' . "\n";
    }
    
    // How many pages of content are there?
    $total_pages = ceil($total_entries / $perpage);
    
    if ($total_pages != -1) {
    
        print '<div class="paginator">' . "\n";
    
        $next_link = '';
        $previous_link = '';
        $page_links = '';
    
        // Loop through each of the pages
        for ($page_count = 1; $page_count <= $total_pages; ++$page_count) {
    
            if ($page_original == $page_count) {
    
                // We're on this page, so no link needed
                $page_links .= $page_count;
    
                // Get the "previous" link if there is more than one page and if this isn't the first page
                if ($total_pages > 1 && $page_count != 1) {
                    $previous_link = '<a href="?page=' . ($page_count - 1) . '"><</a> | ';
                }
    
                // If there are more pages than this,  prepare the "next" link
                if ($page_count != $total_pages) {
                    $next_link = ' <a href="?page=' . ($page_count + 1) . '">></a>';
                }
            }
    
            else {
                $page_links .= '<a href="?page=' . $page_count . '">' . $page_count . '</a>';
            }
    
            if ($page_count != $total_pages) {
                // Print a separator for all pages but the last page
                $page_links .= ' | ';
            }
        }
        print $previous_link . $page_links . $next_link . '</div>';
    
    ?>
    
    <table ></p>
    <tr> 
       <th bgcolor="#9999FF"><div align="center"><font face="Arial, Helvetica, sans-serif">NAME</font></div></th>
       <th bgcolor="#9999FF"><div align="center"><font face="Arial, Helvetica, sans-serif">ALE</font></div></th>
       <th bgcolor="#9999FF"><div align="center"><font face="Arial, Helvetica, sans-serif">ALE</font><font face="Arial, Helvetica, sans-serif"> ID</font></div></th>
       <th bgcolor="#9999FF"><div align="center"><font face="Arial, Helvetica, sans-serif">REGION</font></div></th>
       <th bgcolor="#9999FF"><div align="center"><font face="Arial, Helvetica, sans-serif">NETWORK </font></div></th>
       <th bgcolor="#9999FF"><div align="center"><font face="Arial, Helvetica, sans-serif">UID </font></div></th>
       <th bgcolor="#9999FF"><div align="center"><font face="Arial, Helvetica, sans-serif">CID </font></div></th>
       <th bgcolor="#9999FF"><div align="center"><font face="Arial, Helvetica, sans-serif">DATE </font></div></th>
    </tr>
     
       <div align="center"></div>
         
         <font size="2">
           
         <?
    $i=0;
    while ($i < $num) { 
    $aff=mysql_result($result2,$i,"name");
    $sale=mysql_result($result2,$i,"ale");
    $saleID=mysql_result($result2,$i,"aleID");
    $region=mysql_result($result2,$i,"region");
    $network=mysql_result($result2,$i,"network");
    $uid=mysql_result($result2,$i,"uid");
    $cid=mysql_result($result2,$i,"cid");
    $date=mysql_result($result2,$i,"date"); 
    ?>
         </font>
       
    <tr> 
        <td><div align="center"><font size="1" face="Arial, Helvetica, sans-serif"><? echo "$name"; ?></font></div></td>
        <td><div align="center"><font size="1" face="Arial, Helvetica, sans-serif"><? echo "$ale"; ?></font></div></td>
        <td><div align="center"><font size="1" face="Arial, Helvetica, sans-serif"><? echo "$aleID"; ?></font></div></td>
        <td><div align="center"><font size="1" face="Arial, Helvetica, sans-serif"><? echo "$region"; ?></font></div></td>
        <td><div align="center"><font size="1" face="Arial, Helvetica, sans-serif"><? echo "$network"; ?></font></div></td>
        <td><div align="center"><font size="1" face="Arial, Helvetica, sans-serif"><? echo "$uid"; ?></font></div></td>
        <td><div align="center"><font size="1" face="Arial, Helvetica, sans-serif"><? echo "$cid"; ?></font></div></td>
        <td><div align="center"><font size="1" face="Arial, Helvetica, sans-serif"><? echo "$date"; ?></font></div></td>
    </tr>
    
       <div align="center"><font size="2">
       <?
    ++$i;
    } 
    echo "</table>";
    }
    
    ?>

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Simply wrap all of your code in an if statement
    if(isset($_GET['month'])) {..
    All of your query code here
    ... }
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #3
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    90
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Hi Jay,

    Thanks for the info, but i've put your mentioned if statement just about everywhere within the code and it stops working completely. I'm unsure where to put it to be honest.

    Thankyou
    Jenna

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    put it around this
    PHP Code:
    // Print those entries!
    while ($entries mysql_fetch_object($result2)) {
        print 
    '<div class="entry">' "\n";
        print 
    '<h2>' $entries->title '</h2>' "\n";
        print 
    '<p>' $entries->content '</p>' "\n";
        print 
    '</div>' "\n";

    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #5
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    90
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Hi Jay,

    Ok i have added it around that part of code, but it hasnt changed what happens...

    When February is selected from the drop down form it does show the 15 records/rows but when i hit the next page 2 ( 1|2 > ) link it doesnt show the rest of the february records/rows, it shows the next 15 records/rows of the entire DB.

    Thanks for your help so-far
    Jenna

  • #6
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    oh OK. Change this line
    PHP Code:
    $next_link ' <a href="?page=' . ($page_count 1) . '">></a>'
    to
    PHP Code:
    $next_link ' <a href="?page=' . ($page_count 1) .(isset($_GET['month']) ? '&amp;'.$_GET['month'] : '''">></a>'
    to begin with. Does that make the next button work?
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #7
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    90
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Afraid not, now i get a

    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING

  • #8
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Oops forgot the .
    This should work
    PHP Code:
    $next_link ' <a href="?page='.($page_count 1).(isset($_GET['month']) ? '&amp;'.$_GET['month'] : '').'">></a>'
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #9
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    90
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Hi,
    It fixed the syntax error, but still hasnt corected the next link for February.
    Jenna

  • #10
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    ah, this is a problem too
    <form method=post action=test.php>
    your method is post and it should be GET
    <form method="GET" action="test.php">
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #11
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    90
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Ok i have changed it to GET

    On the next link ( > ) i now get

    /test.php?page=2&February

    Which looks promising, but it doesnt use February in the

    WHERE date LIKE '%$month%'

    so it still shows all data from DB when the next link is clicked and not the rest of februarys data.

    It looks like we (YOU) might be close.

    Thanks-so-far-jay
    Jenna

  • #12
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    ok I am far too tired lol. It should have been this for the link
    PHP Code:
    $next_link ' <a href="?page='.($page_count 1).(isset($_GET['month']) ? '&amp;month='.$_GET['month'] : '').'">></a>'
    also change your prev_link to
    PHP Code:
    $previous_link '<a href="?page='.($page_count 1).(isset($_GET['month']) ? '&amp;month='.$_GET['month'] : '').'"><</a> | '
    See if that gets those two working
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • Users who have thanked JAY6390 for this post:

    mobimad (01-12-2010)

  • #13
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    90
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Excellent,

    It looks like its working for the > link

    Yipee

    How would i change it so that the 1|2|3|4 links, get the month aswell.

    Much Appreciated
    Jenna

  • #14
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Change this line
    PHP Code:
    $page_links .= '<a href="?page=' $page_count '">' $page_count '</a>'
    to
    PHP Code:
    $page_links .= '<a href="?page='.$page_count.(isset($_GET['month']) ? '&amp;month='.$_GET['month'] : '').'">'.$page_count.'</a>'
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • Users who have thanked JAY6390 for this post:

    mobimad (01-12-2010)

  • #15
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    90
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Thank you for help today Jay
    much appreciated
    All the best
    Jenna


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