Php pagination of database queries..

Discussion in 'PHP' started by khan11, Jan 12, 2009.

  1. #1
    Hello,

    I'm creating a script which firstly fetches columns from database and then save them to an array, then i call them using while loop.

    What i want is to show page links at bottom like, if the database has 30 items, it should show page 1 - 2 - 3 which means 10 items on single page.

    I downloaded one script but it didn't work for me.
    The thing i want to do is like this:
    ID|NAME|Place
    1 | ABC | earth
    ..... up to 30 ids

    i save it in $data = mysql_fetch_array($result);
    then i use this
    while ($row = $data) {
    echo $row['id']." ".$row['name']; // and so on...
    }

    i want to display links after this thing.
    i know i can do it using if, elseif commands but thats kinda annoying, i want the links to be dynamic so it saves the headache..

    Any solution??

    Thanks in advance..
     
    khan11, Jan 12, 2009 IP
  2. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #2
    how are you telling php which set to show?

    say if you have, 3 pages, you can do

    www.domain.com/index.php?show=2 , which would signify page 2, but then in your code you could do something like '$_GET['show'] * 10 - 9', and then start your array at whatever that value is, so even if you have 100 total id's, and you did show=9, it would show 81-90

    If I don't understand your question correctly, please let me know :p
     
    crath, Jan 12, 2009 IP
    khan11 likes this.
  3. mikey1090

    mikey1090 Moderator Staff

    Messages:
    15,869
    Likes Received:
    1,055
    Best Answers:
    0
    Trophy Points:
    445
    Digital Goods:
    2
    #3
    mikey1090, Jan 12, 2009 IP
  4. webrickco

    webrickco Active Member

    Messages:
    268
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #4
    webrickco, Jan 13, 2009 IP
  5. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #5
    @mikey1090, i also tried on google before asking, i tried the first one but it didn't meet my requirement.
    @webrickco, thank you for sharing link. I will surely contact you if i face any problems.

    However, i have sort it out using crath's idea, it worked fine. I first counted the database rows and then used loop to create its url and then link them to get command of ['page'] and after that i fetched the $_GET['page'] multiplied it with 10 and it worked fine. I did a few more tweaks to make it work perfect. So far, its working fine, if i get into some trouble, i will share here.

    Thanks anyways for your replies.
     
    khan11, Jan 13, 2009 IP
  6. mikey1090

    mikey1090 Moderator Staff

    Messages:
    15,869
    Likes Received:
    1,055
    Best Answers:
    0
    Trophy Points:
    445
    Digital Goods:
    2
    #6
    Its basically a logical battle. Once you get your head around it its fine. Quite simple when you look back :)

    Just make sure you do some validation on the _GET vars before entering them into your DB queury.
     
    mikey1090, Jan 13, 2009 IP
  7. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #7
    true.^

    yea, i try something like if(isset($var)) { // do this } where $var gets the get command. i have created an array $errors = array(); whatever has chances of going wrong, i set $errors = $field etc... therefore, it works quite fine.
     
    khan11, Jan 13, 2009 IP
  8. linkexchange1984

    linkexchange1984 Peon

    Messages:
    73
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    basic and simple funda is using limit at your sql query. Simple.
    *search for script by the key words " pagination php ".


    -------------------------------------------------
    Download Free Ebooks
    Smart Banking Tips
     
    linkexchange1984, Jan 13, 2009 IP