godsent Posted February 18, 2009 Share Posted February 18, 2009 I remade i little ajax gethint system for learning purposes, i made that if typed name is exactly like in array it prints a message. But... <?php $a[]="Anna"; $a[]="Brittany"; $a[]="Cinderella"; $a[]="Diana"; $a[]="Eva"; $a[]="Fiona"; $a[]="Gunda"; $a[]="Hege"; $a[]="Inga"; $a[]="Johanna"; $a[]="Kitty"; $a[]="Linda"; $a[]="Nina"; $a[]="Ophelia"; $a[]="Petunia"; $a[]="Amanda"; $a[]="Raquel"; $a[]="Cindy"; $a[]="Doris"; $a[]="Eve"; $a[]="Evita"; $a[]="Sunniva"; $a[]="Tove"; $a[]="Unni"; $a[]="Violet"; $a[]="Liza"; $a[]="Elizabeth"; $a[]="Ellen"; $a[]="Wenche"; $a[]="Vicky"; $q=$_GET["q"]; if (strlen($q) > 0) { for($i=0; $i<count($a); $i++) { if ($a[$i] == $q) { $response = "Okay"; } else { $response = "not okay"; } } } echo $response; ?> The problem is that it always says "not okay", because it's continue looping even if they match. If after $response = "Okay"; i put "return;" its not gonna do printing. Any suggestions? Link to comment https://forums.phpfreaks.com/topic/145764-need-help-with-looping/ Share on other sites More sharing options...
Maq Posted February 18, 2009 Share Posted February 18, 2009 Add a break in after you find a match and it will exit the loop: if ($a[$i] == $q) { $response = "Okay"; break; Link to comment https://forums.phpfreaks.com/topic/145764-need-help-with-looping/#findComment-765304 Share on other sites More sharing options...
ILMV Posted February 18, 2009 Share Posted February 18, 2009 First, simplify your loop by using foreach... foreach($a as $key=>$value) { if ($a[$key] == $q) { $response = "Okay"; } else { $response = "not okay"; } } Try this, tell me if this helps... Link to comment https://forums.phpfreaks.com/topic/145764-need-help-with-looping/#findComment-765305 Share on other sites More sharing options...
godsent Posted February 18, 2009 Author Share Posted February 18, 2009 thanks, break; works perfect Link to comment https://forums.phpfreaks.com/topic/145764-need-help-with-looping/#findComment-765312 Share on other sites More sharing options...
Maq Posted February 18, 2009 Share Posted February 18, 2009 It is better practice to use a foreach for arrays because it's slightly faster and is pretty much the standard way of iterating through an array. Actually an array is the only thin you can use for a foreach. Anyway, even with the foreach ILMV, if it doesn't match the last name the $response will always be "not okay". Link to comment https://forums.phpfreaks.com/topic/145764-need-help-with-looping/#findComment-765332 Share on other sites More sharing options...
sasa Posted February 18, 2009 Share Posted February 18, 2009 you can use in_array() function Link to comment https://forums.phpfreaks.com/topic/145764-need-help-with-looping/#findComment-765446 Share on other sites More sharing options...
Maq Posted February 18, 2009 Share Posted February 18, 2009 you can use in_array() function Lol, that would make things a lot easier Do this: $response = (in_array($q, $a)) ? "Okay" : "Not Okay"; echo $response; Link to comment https://forums.phpfreaks.com/topic/145764-need-help-with-looping/#findComment-765460 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.