Jump to content

While loop break


ApocalypeX

Recommended Posts

So I'm writing some background code for a app I'm writing and due to stubborness I am in a situation.My php page is sent a string of usernames separated by , and then turned into an array. I then get all the rows in my database table and check against each name until that name is found in the database. Once found in the data the loop ends. The problem is that there is so many calls going on that when I break the loop checking 1 name against the database it breaks the forloop.Now I know I could do this the other way around but it would really be inefficient to loop every name in the database for 1 name. So I want to try and get this method to work.

//creates json objects for the clients friends list {'Username':'Offline'/NAN/Location}function createJSON($_username, $_status, $_location){	$status = time() - intVal($_status);	if($status < (60*5)){		$location = $_location;	}	else if($_status != 0){		$location = $_location;	}	else{		$location = "NAN";	}	return "'{$_username}':'{$location}'";}function checkFriends(&$_JSarr, $_Friend, $_row){	while($r = mysql_fetch_array($_row)){		if($_Friend == $r['username']){			array_push($_JSarr, createJSON($r['username'], $r['status'], $r['location']));			echo "a";			break;		}		else{			array_push($_JSarr, createJSON($_Friend, 0, 0));			echo "b";			break;		}	}}//find friends sent from the client in the database and convert them and their data to json objectsfunction getFriends($_Friends){	$Friends = explode(',',$_Friends);		$result = mysql_query("SELECT * FROM `clients`");	$JSarr = array();	foreach($Friends as $Friend){		checkFriends($JSarr, $Friend, $result);		echo "p";	}	$JSstr = join(",", $JSarr);	return "var FriendsArr = {"."{$JSstr}"."};";}

The echos are just there to test the loop.For example if I run random keysecho getFriends("ffX,ffeX,ffeX,ffeX,ApocalypeX");I get bpppppvar FriendsArr = {'ffX':'NAN'};When I should be gettingbpbpbpbpapvar FriendsArr = {'ffX':'NAN','ffeX':'NAN','ffeX':'NAN','ffeX':'NAN','ApocalypeX':'Online'};

Link to comment
Share on other sites

Using "break" means "get me out of this loop NOW!". From what I can tell from your code right away is that-

while($r = mysql_fetch_array($_row)){		if($_Friend == $r['username']){			array_push($_JSarr, createJSON($r['username'], $r['status'], $r['location']));			echo "a";			break;		}		else{			array_push($_JSarr, createJSON($_Friend, 0, 0));			echo "b";			break;		}	}

Isn't a loop. It can ONLY be the true or else conditions so you will always break the loop. Therefore, this isn't a loop but an if statement that will return on the first check (whether true or not)

Link to comment
Share on other sites

Isn't a loop. It can ONLY be the true or else conditions so you will always break the loop. Therefore, this isn't a loop but an if statement that will return on the first check (whether true or not)
OMG I can see that now. I'm not on my linux workspace so I'll test this out later but thanks. Its always good to get a fresh pair of eyes on a situation :)EDIT: Thanks for that I rearranged my code and it works.Anyone interested heres the new code
//creates json objects for the clients friends list {'Username':'Offline'/NAN/Location}function createJSON($_username, $_status, $_location){	$status = time() - intVal($_status);	if($status < (60*5)){		$location = $_location;	}	else if($_status != 0){		$location = $_location;	}	else{		$location = "NAN";	}	return "'{$_username}':'{$location}'";}//checks friend to see if they are in the databasefunction checkFriends(&$_JSarr, $_Friend){	$result = mysql_query("SELECT * FROM `clients`");	$friend = false;	$status = 0;	$location = 0;	while($r = mysql_fetch_array($result)){		if($_Friend == $r['username']){			$friend = true;			$status = $r['status'];			$location = $r['location'];			break;		}	}	array_push($_JSarr, createJSON($_Friend, $status, $location));	echo "a";}//find friends sent from the client in the database and convert them and their data to json objectsfunction getFriends($_Friends){	$Friends = explode(',',$_Friends);	$JSarr = array();	foreach($Friends as $Friend){		checkFriends($JSarr, $Friend);		echo "p";	}	$JSstr = join(",", $JSarr);	return "var FriendsArr = {"."{$JSstr}"."};";}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...