Jump to content

Problem with a loop


Shadowing

Recommended Posts

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 />"; 

Link to comment
Share on other sites

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
) 

 

 

Link to comment
Share on other sites

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 />";

}

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.