#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    10
    Rep Power
    0

    A word of advise please.


    I am all new with PHP so bare with me here.

    I am writing my first script (well... more application), but I can not seem to figure out how to display the data as I wish. Even after searching google and diving into a lot of online tutorials I am not able to find the answer to my question.

    I am able to display all my data on one page. My question is. How would I edit my code so that it display's 10 results and then underneath the results it gives me the option to go to a next page.

    Example:
    previous page 8,9,20....17 next page
    The connection to the database is done with the use of an included file. This because I want to store some variables when the connection is made which need to be used in different pages.
    PHP Code:
    $sql "SELECT naam, datum_toevoeging, inleg_totaal FROM medewerker_poster";
            
    $resultaat mysql_query($sql);
            
    $aantal_rijen mysql_numrows($resultaat);
            
    mysql_close(); 
    The following is used to display the data:
    PHP Code:
                            $teller 0;
                            while(
    $teller $aantal_rijen){
                            
                                
    $naam mysql_result($resultaat$teller"naam");
                                
    $datum_toevoeging mysql_result($resultaat$teller"datum_toevoeging");
                                
    $inleg_totaal mysql_result($resultaat$teller"inleg_totaal");
                            
                                print 
    "<tr>
                                            <td>" 
    $naam "</td>
                                            <td>" 
    $datum_toevoeging "</td>
                                            <td>" 
    $inleg_totaal "</td>
                                        </tr>"
    ;
                                
    $teller++;
                            } 
    Thanks in advance.
  2. #2
  3. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    I achieved this with reference to a book: PHP and MySql by Larry Ullman - excellent book.

    But there are numerous references to PHP Pagination on the internet.

    Essentially you need to store the total number of records, divide this into pages, and use url-get parameters to jump from page to page.

    Comments on this post

    • Storm2012 agrees : Thank you for the help.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    10
    Rep Power
    0
    Originally Posted by AndrewSW
    I achieved this with reference to a book: PHP and MySql by Larry Ullman - excellent book.

    But there are numerous references to ****on the internet.

    Essentially you need to store the total number of records, divide this into pages, and use url-get parameters to jump from page to page.
    Thank you.

    Now I at least have a term for to search.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    before you read through the whole code (which doesn't look very good to me):

    If you have n entries per page and you want to select the entries for a specific page p, you first skip all entries of the previous pages, which are given by (p - 1) * n. And then you select n rows.

    MySQL has a LIMIT clause for this:

    Code:
    SELECT
    ...
    LIMIT (number of rows to skip), (number of rows to select)
    That's all the "magic".

    Comments on this post

    • Storm2012 agrees : Thank you for your help.
    • npl disagrees
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    10
    Rep Power
    0
    Originally Posted by Jacques1
    Hi,

    before you read through the whole code (which doesn't look very good to me):

    If you have n entries per page and you want to select the entries for a specific page p, you first skip all entries of the previous pages, which are given by (p - 1) * n. And then you select n rows.

    MySQL has a LIMIT clause for this:

    Code:
    SELECT
    ...
    LIMIT (number of rows to skip), (number of rows to select)
    That's all the "magic".
    If you mean error checking when connecting to the MySQL database that is done seperatly.

    I presume there are way better methods of coding what I did. However as I am just beginning this method allows me to keep good track of my variables and see exactly what I am doing.

    I do not really understand what you mean with using the LIMIT clause as I am not familiar with it. However I will do some research on that as well.
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by Storm2012
    I presume there are way better methods of coding what I did. However as I am just beginning this method allows me to keep good track of my variables and see exactly what I am doing.
    I was refering to the code in AndrewSW's link. It does contain good explanations, but the code itself isn't exactly best practices.

    You should generall be aware that the mysql_... functions are absolete. See this link for explanations and alternatives:

    http://php.net/manual/en/mysqlinfo.api.choosing.php

    You don't have to rewrite your whole code now, but you should switch to one of the more modern libraries in the long term.



    Originally Posted by Storm2012
    I do not really understand what you mean with using the LIMIT clause as I am not familiar with it. However I will do some research on that as well.
    See the MySQL online manual:

    http://dev.mysql.com/doc/refman/5.6/en/select.html

    Comments on this post

    • npl disagrees

IMN logo majestic logo threadwatch logo seochat tools logo