Shadowing Posted February 12, 2012 Share Posted February 12, 2012 I hate asking help twice in the same day but im really stuck Im having problem with this loop im writing. I'm randomly grabing a name from an array then im checking to make sure it doesnt exist already in the database if it does then it loops back and grabs another name. I'm having problems with seeing whats going on while its looping I inserted a name in the data base to match "AGENA" so if AGENA is the end result i know its not working. right now its not even looping. $planet_names = " AGENA Deneb Zuben Zosma"; $explode_names = explode(" ", $planet_names); $random_key_name = (array_rand($explode_names, 1)); $planet_name = $explode_names[$random_key_name]; $loop_again = 0; while ($loop_again > 1) { $name_check3 = "SELECT address FROM planets WHERE name = '".($planet_name)."'"; $name_check2 = mysql_query($name_check3) or trigger_error("SQL", E_USER_ERROR); $name_check1 = mysql_num_rows($name_check2); if($name_check1 >= "1"){ // checks to see if name exists $loop_again = 1; }else{ ++$loop_again; $planet_name = $planet_name; } } echo "It ran $loop_again times<br />"; echo "$name_check1 match found equals 1. No match found equals 0<br />"; echo $planet_name . "<br />"; Quote Link to comment https://forums.phpfreaks.com/topic/256955-problem-with-a-loop/ Share on other sites More sharing options...
Pikachu2000 Posted February 12, 2012 Share Posted February 12, 2012 The array probably isn't structured as you think it should be. See what this gives you: echo '<pre>'; print_r($explode_names); echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/256955-problem-with-a-loop/#findComment-1317287 Share on other sites More sharing options...
Shadowing Posted February 12, 2012 Author Share Posted February 12, 2012 thanks for the reply again Pikachu what im doing is. I have atm 735 stars. im getting each star a differant name. i don thave 735 names so im adding a number with the name. "number generator not included in the code" but i need to make sure there is no duplicates. One idea i had was just add the id of the row to the name. but either way i need to learn how to do this for another future reason. im getting this. grabing the random value from the array is working fine. Thats all tested Array ( [0] => AGENA [1] => Deneb [2] => Zuben [3] => Zosma ) Quote Link to comment https://forums.phpfreaks.com/topic/256955-problem-with-a-loop/#findComment-1317291 Share on other sites More sharing options...
Pikachu2000 Posted February 12, 2012 Share Posted February 12, 2012 So then, seeing that output would indicate that there's a leading linefeed on each value, right? trim the values. You can use array_map to do it in one step. Quote Link to comment https://forums.phpfreaks.com/topic/256955-problem-with-a-loop/#findComment-1317296 Share on other sites More sharing options...
Shadowing Posted February 12, 2012 Author Share Posted February 12, 2012 each value has a space after it and each value is on a differant line You are right i have larger problems going on lol. I got rid of the loop and the if statement doesnt work. so what your saying is its because of the spaces before and after the value im pulling from the array. im confused on the part about array_map. not sure what to do with or what its going to do in this situation. but right now with this code when "AGENA 0" shows up it says no. I have only "AGENA 0" in the data base $planet_names = " AGENA Deneb Zuben Zosma"; $explode_names = explode(" ", $planet_names); $random_key_name = (array_rand($explode_names, 1)); $planet_name = $explode_names[$random_key_name]; $random_number = 0; //rand(1,999); using 0 for testing $name_plus_number = trim($planet_name . " $random_number", " "); $name_check3 = "SELECT address FROM planets WHERE name = '".($name_plus_number)."'"; $name_check2 = mysql_query($name_check3) or trigger_error("SQL", E_USER_ERROR); $name_check1 = mysql_num_rows($name_check2); IF ($name_check1 >= "1"){ echo "yes<br />"; }else{ echo "no<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/256955-problem-with-a-loop/#findComment-1317316 Share on other sites More sharing options...
Shadowing Posted February 12, 2012 Author Share Posted February 12, 2012 Alright using massive trial and error lol. This works when i set the string up like this before the explode $planet_names = "AGENA Deneb Zosma"; $explode_names = explode(" ", $planet_names); $random_key_name = (array_rand($explode_names, 1)); $name_plus_number = $explode_names[$random_key_name]; so im not triming it correctly. for what im reading in the manual says i need to use " " for blank spaces and \n for line feeds. how do I combine those though. cause i want to get rid of all spaces and line breaks. $name_plus_number = trim($name_plus_number, " \n"); I have 370 names so id like to keep them on their own line. cause i may add more names later like it to be easy to add more later on. Quote Link to comment https://forums.phpfreaks.com/topic/256955-problem-with-a-loop/#findComment-1317342 Share on other sites More sharing options...
Shadowing Posted February 12, 2012 Author Share Posted February 12, 2012 oh i guess i dont need to get rid of spaces just the line break like you mentioned. this does work $planet_names = "AGENA Deneb Zosma"; $name_plus_number = trim($name_plus_number, " \n"); this doesnt work though $planet_names = " AGENA Deneb Zosma"; EDIT: i just noticed that this works even with out the trim. So im not using trim correctly. what am I doing wrong? $planet_names = "AGENA Deneb Zosma"; Quote Link to comment https://forums.phpfreaks.com/topic/256955-problem-with-a-loop/#findComment-1317343 Share on other sites More sharing options...
Shadowing Posted February 12, 2012 Author Share Posted February 12, 2012 Alright I got everything working. Loop and all. it was cause i was adding a charlist on the trim that was messing me up. I wasnt doing it right i guess? if i just do this it wipes out the line feed $name_plus_number = trim($planet_name . " $random_number"); where i was trying to do this and it didnt do anything $name_plus_number = trim($planet_name . " $random_number", "\n"); Quote Link to comment https://forums.phpfreaks.com/topic/256955-problem-with-a-loop/#findComment-1317358 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.