Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2002
    Location
    United Kingdom
    Posts
    1,236

    need help on how to stop a loop

    ok then, i have built a webhost search script which searches through a mysql database, and displays the results on a page.

    The user selects what they want, i.e ftp, 200MB space, php, mysql etc etc.

    well i got the script to display the data but i have a problem

    If the script finds more than 20 webhosts, the script will stop, which is what i want. But if the script doesn't find 20 hosts, it will just keep on looping and looping.

    I need to know a way so that if the script comes to the end of all the rows in a database, the script stops.

    here is the code that i have done :

    PHP Code:
    $stop no;
    $max_results 0;
    $hostid 1;

    while (
    $stop == no)

    {

        
    $my_query "SELECT hostid, webhost_link, webhost_description FROM webhosts WHERE (
        hostid = '
    $hostid' && ftp = '$ftp' && mysql = '$mysql' && pre_installed_scripts =
        '
    $pre_scripts' && email = '$email' && browser_upload = '$browser_upload' &&
        show_cobrands = '
    $show_cobrands' && php = '$php' && cgi_bin = '$cgi_bin' &&
        sub_domain = '
    $subdomain' && domain_hosting = '$domain_hosting')";
        
    $result mysql_query($my_query);
        
    $data mysql_fetch_array($result);
        
    $webhost_link $data['webhost_link'];
        
    $webhost_description $data['webhost_description'];
        
    $host_id $data['hostid'];
        if(
    $webhost_link != "")

        {
            echo (
    $webhost_name);
            echo (
    '<BR>');
            echo (
    $webhost_link);
            echo (
    '<BR>');
            echo (
    $webhost_description);
            echo (
    '<BR>');
            echo (
    $host_id);
            echo (
    '<BR><BR>');
            
            
    $max_results ++;
            
        }

        if(
    $max_results == 21)

        {

            
    $stop == yes;

        }


        
    $hostid ++;



    so what i need is a way so that i can do this :

    if(i get to the last row in the table)

    {

    $stop == yes;
    echo ("No more Results");

    }

    how could i do this???

    i need to know once i have gone through all the rows in a table and have come to the end.

    Thanx

  2. #2
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    3,100
    why don't you try:

    $maxres = 20;
    $result = mysql_query ("select webhost_link, webhost_description from webhosts where '$conditions' LIMIT '$maxres' ");
    while ($data = mysql_fetch_array($result)){
    // do your printing
    }

    if (mysql_num_rows($result) < $maxRes) {
    // less then 20 hosts
    }

  3. #3
    Join Date
    Jul 2002
    Location
    Israel
    Posts
    351
    Or you could break out of the loop.
    C#/C++/TCL/Python/PHP developer.
    mark at mark org il

  4. #4
    Join Date
    May 2002
    Location
    UK
    Posts
    2,994
    Why not use the mySQL specific SQL function

    LIMIT 0, 20 (where 0 is your starting row... which you can increment for 'next results' pages and 20 is for how many rows).

    Just put it in your SQL statement at the very end.

  5. #5
    Join Date
    Jun 2002
    Location
    United Kingdom
    Posts
    1,236
    hi thanx, i realised that just after i did the post.

    for anyone who is wondering what i did i did this :

    PHP Code:
    $my_query "SELECT * FROM webhosts WHERE (ftp = '$ftp' &&
        mysql = '
    $mysql' && pre_installed_scripts = '$pre_scripts' &&
        email = '
    $email' && browser_upload = '$browser_upload' &&
        show_cobrands = '
    $show_cobrands' && php = '$php' && cgi_bin = '$cgi_bin' &&
        sub_domain = '
    $subdomain' && domain_hosting = '$domain_hosting') LIMIT $from$to"
    thanx for the help everyone

    thanx

Posting Permissions

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