Search the Community
Showing results for tags 'php arrays and loops'.
-
Hello I am working on a 3 field variable that is given in an array with A-Z and 1-26 and 26 words in total, being randomized and insert in database via mysql but it must checks to be sure there is no duplicate of any values at all. here is my code, I managed to make the first value to work but the 2nd and 3rd doesnt. <? // connection data mysql_connect("localhost", "user", "pass") or die("error ".mysql_error()); mysql_select_db("dbname") or die("error ".mysql_error()); function GenerateNumber(){ // selects random number $randomNumber = rand(1,26); return $randomNumber; } function GenerateLetter(){ // selects random position number in letters array to select an alphabet $LetterPosition = rand(0,25); $alphabet = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'); $randomLetter = $alphabet[$LetterPosition]; return $randomLetter; } function GenerateWord(){ // selects random word from an array -same method as letter $wordPosition = rand(0,25); $words = array('ah','book','cup','dice','elephant','flute','gum','house','idol','jumpingjacks','kite','lunchbox','matress','novel','olive','pumpkin','queen','rooster','stove','teacup','unicorn','violin','walurus','xerox','zebra'); $randomWord = $words[$wordPosition]; return $randomWord; } for($x=0; $x<=25;){ //generate letter,number,and word $randomLetter = GenerateLetter(); $randomNumber = GenerateNumber(); $randomWord = GenerateWord(); echo '<b>Generated Variables:</b>[ '. $randomLetter.' ][ '.$randomNumber.' ][ '.$randomWord.' ]<br />'; // there is data in the table, check if there is letter already assigned $first = mysql_query("SELECT * FROM test WHERE a_char='$randomLetter'"); if(mysql_num_rows($first) == 0){ //no letter is found, check if there is number already assigned $second = mysql_query("SELECT * FROM test WHERE aint='$randonNumber'"); if(mysql_num_rows($second) == 0){ //no number is found, check if there is word already assigned $thrid = mysql_query("SELECT * FROM test WHERE aword='$randomWord'"); if(mysql_num_rows($third) == 0){ //there is no letter,number, and word found in db, great! go ahead and insert! $fq = "INSERT INTO test (a_char,aint,aword) VALUES ('$randomLetter','$randomNumber','$randomWord')"; $final = mysql_query($fq); //increment the loop $x++; echo('[ '. $randomLetter.' ][ '.$randomNumber.' ][ '.$randomWord.' ] has been added in row '.$x.'<br /><br />'); }else{ echo("Repeated Word: ".$randomWord." generating another: "); //there is word already assigned, select another! $randomWord = GenerateWord(); echo($randomWord.'<br /><br />'); } }else{ echo("Repeated Number: ".$randomNumber." generating another: "); //there is number already assigned, select another! $randomNumber = GenerateNumber(); echo($randomNumber.'<br /><br />'); } }else{ echo("Repeated Letter: ".$randomLetter." generating another: "); //there is letter already assigned, select another! $randomLetter = GenerateLetter(); echo($randomLetter.'<br /><br />'); } } mysql_close(); ?>