Web Hosting Talk







View Full Version : need help on how to stop a loop


matt2kjones
11-04-2002, 01:45 PM
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 :

$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

sasha
11-04-2002, 02:15 PM
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
}

MarkIL
11-04-2002, 02:44 PM
Or you could break out (http://www.php.net/manual/en/control-structures.break.php) of the loop.

Rich2k
11-05-2002, 05:39 AM
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.

matt2kjones
11-05-2002, 11:55 AM
hi thanx, i realised that just after i did the post.

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

$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