SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: while

  1. #1
    SitePoint Addict noise's Avatar
    Join Date
    Sep 2002
    Location
    Sweden
    Posts
    251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    while

    I'm using this to display search results:
    PHP Code:
    $i 1;      
    while (
    $row mysql_fetch_array($search) and ($i <= 10))
    {
     print 
    $songs;
     
    $i++;

    And it's working as it should... But i can't figure out how to display rest of the results (11 to 20, 21 to 30, etc).


    Any ideas?

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2002
    Location
    Israel
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mmm...

    if the next code is exactly after this one all there is to do is to copy the exact loop. since the value of $i is 10 the next time this loop will occure its value will be increased by one. one you should change is the limit of the loop to be $i <= 20
    this is one way.
    if the 10-20 loop is not right after this loop then u can assign the value of 10 to the $i varible.
    what i would have done in a situation like this is a bit different. first of all i'd use a for loop instead of while loop. then if u want to show on the first page the 1-10 results and on the next page to show the 10-20 results i'd use a varible in the link to the next page. something like: http://www.someDomain.com/results.php?StartFrom=10
    then in the script itself in the "for" loop u can use the $_GET['StartFrom'] and run that loop as long as the $i varible is smaller/equall to $_GET['StartFrom']+10
    that is it.
    hope i was clear enough.
    lots of luck
    Shanor.
    You can't see your self in the mirror with your eyes closed!

  3. #3
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Better still ... use the LIMIT clause in mySQL to return a specific set of records ...
    PHP Code:
    // These values can be set dynamically, like the
    // post above suggests.
    $start 0;
    $limit 10;

    // Use them in a SQL command
    $sql "SELECT * FROM MyTable LIMIT $start$limit";
    $search mysql_query ($sql);

    // Loop through the records
    while ($row mysql_fetch_array($search))
    {
     
    $songs $row['Songs'];
     print 
    $songs;

    Ian Anderson
    www.siteguru.co.uk

  4. #4
    SitePoint Addict noise's Avatar
    Join Date
    Sep 2002
    Location
    Sweden
    Posts
    251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just woke up, having my morning cofee (well it's not morning but heh). Anyway, this is looking promissing - can't wait to start testing. And ya know that I HAVE to try both suggestions. Thank you very much for your ideas!!!

  5. #5
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    Alkmaar, Netherlands
    Posts
    710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    add to your list "I have to learn SQL".
    If you are developing a database application and if you do not know basics of the tool you have, then you will have lots of difficulties.
    There are few resources all around here that will make you go forward lots.

    MHO

  6. #6
    SitePoint Addict noise's Avatar
    Join Date
    Sep 2002
    Location
    Sweden
    Posts
    251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is so true, and i know that. Still tho this is how I learn best - attempt to do something that is beond my knowledge and bang my head aginst the wall untill it's done



    Anyway here is the first version:
    Lyrics database



    Once again, thank you for all your help!!!

    PS. I combined both SQL (LIMIT) and while() to make it work


Bookmarks

Posting Permissions

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