Poddy Posted February 25, 2008 Share Posted February 25, 2008 Hi, i am trying to get a loop to start over if a condition is true, is that possible? how do i instruct him to restart the loop? here is the concept of what i am trying to do: i am building an application for a social network which you fill information about yourself and answer questions about your friends i want to: display a question that is NOT 1. answered about that specific friend 2. same question twice in a row on different friends(still haven't figured that out) once i see a question WAS answered randomize the question again and start over... the questions must be random! Thanks in advance for all helpers here is the code: #defining an array will be replaced by real data by the network $friend = array ("1","2","3","4","5","6","7","8","9","10"); #counts the number of friends $fnum = COUNT($friend); #negative 1 to fix the 0 starting position in an array $fnum -= 1; #randomizing a friend $fr = rand(0,"$fnum"); #saving the friends id(numbers will not be 1, 2 etc they will be at least 10 char length and not in order) $fid = "$friend[$fr]"; $sql = " SELECT * FROM questions"; $result = mysql_query($sql) or die ('error fetching data); #counting the number of rows in mysql database $numrows = mysql_num_rows($result); #adjusting to 0 in array $numrows -= 1; #randomizing a row for a question id $qtmp = mt_rand(1,$numrows); #saving the question id in the correct format $qid = "q$qtmp"; #setting variables for looping $i = 0; $x = 0; #while number is smaller than sum of questions while ($i <= $numrows) { #select a question from friends values $sql = " select $qid FROM users WHERE user_id='$fid'"; $result = mysql_query($sql) or die ('cant select question'); $answer = mysql_fetch_assoc($result); if ($answer[$i] = null) { #this is where i want to change the question id, and start over the loop $qtmp = mt_rand(1,$numrows); $qid = "q$qtmp"; $i++; } else { #selecting the user accessing the page info in the database to see if he has answered the question about the randomized user $sql = " select * from users WHERE user_id='12345'"; $result = mysql_query($sql) or die ('error fetching data from web_user'); $row = mysql_fetch_assoc($result); #turning the data into an array $an = explode(',', $row["f$qid"]); break; } #counting the number of array objects $anum = count($an); #reference for me to see the array outputs correctly echo "<br> an ARRAY:"; print_r($an); echo "<br>"; #while x is smaller or equal to number of array objects while ($x <= $anum) { # checks if user exists in the array list if ($fid == $an[$x]) { #self reference echo "<br> friend is IN array <br>"; #this is also where i want to re start the loop with a new question $qtmp = mt_rand(1,$numrows); $qid = "q$qtmp"; break; } #if x is not equal to number of objects increase x and check another object elseif ($x != $anum) { $x++; } #when x is equal verify that the user is not in the questions answered database else { echo "question verified you have not answerd it"; break; } } } Link to comment https://forums.phpfreaks.com/topic/92918-restarting-a-loop/ Share on other sites More sharing options...
ucffool Posted February 25, 2008 Share Posted February 25, 2008 Instead of thinking about restarting a loop, why not put it in a loop and based on the results, EXITING the loop. That will give you the same results. $loopme = 0; while ($loopme == 0): // Code runs here if ($readytoexit): $loopme = 1; endif; endwhile; Link to comment https://forums.phpfreaks.com/topic/92918-restarting-a-loop/#findComment-476128 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.