LOSTBOY Posted November 23, 2007 Share Posted November 23, 2007 Hi Experts Am an PHP Beginner here i give snap of my code ..... there how can i generate non repeating random nos...............this code didnt work properly what can i do for that do{ $qno=rand(1, $_SESSION['$max']); if (!in_array($qno,$tempArray)){ $_SESSION[$tempArray[$r]] = $qno; $i=0; $query="SELECT * FROM question WHERE qid='$qno' " ; $result=mysql_query($query); $id=mysql_result($result,$i,"qid"); $question=mysql_result($result,$i,"question"); mysql_result($result,$i,"question"); $query1="SELECT * from answer where qid=$id"; $result1=mysql_query($query1); $r++ ; } }while($r <= $n) thanks in advance Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 23, 2007 Share Posted November 23, 2007 >what can i do for that First, wrap code in code tags [ code]code here[ /code] (remove leading space after first bracket in each) Second, format your code so that it's readable and the logic is present. PhREEEk Quote Link to comment Share on other sites More sharing options...
LOSTBOY Posted November 23, 2007 Author Share Posted November 23, 2007 thanks for ur valuable suggestion ..sorry ......am an newbie to this forum.... $r=0 do{ $qno=rand(1, $_SESSION['$max']); if (!in_array($qno,$tempArray)){ $tempArray[$r] = $qno; $query="SELECT * FROM question WHERE qid='$qno' " ; $result=mysql_query($query); $id=mysql_result($result,$i,"qid"); $question=mysql_result($result,$i,"question"); mysql_result($result,$i,"question"); $query1="SELECT * from answer where qid=$id"; $result1=mysql_query($query1); $r++ ; } }while($r <= $n) Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 23, 2007 Share Posted November 23, 2007 >sorry ......am an new one to this forum.... As we all were at one time.. no worries = ) <?php do{ $qno=rand(1, $_SESSION['$max']); if (!in_array($qno,$tempArray)){ $tempArray[$r] = $qno; v$i=0; v$i is not a valid PHP variable. Was that a typo? Maybe you meant $i? or $vi? Fix that and see where you are. If it doesn't work, tell us what error you recieve or what result you recieve that isn't what you are expecting. PhREEEk Quote Link to comment Share on other sites More sharing options...
LOSTBOY Posted November 23, 2007 Author Share Posted November 23, 2007 ya ...i have already cleared that error but still .....it gerating the repeated no Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 23, 2007 Share Posted November 23, 2007 And what will be your $_SESSION['$max'] maximum value, if it is low then there is max chance of repeating. Try using mt_rand instead. Not too sure but give a try $qno=mt_rand(1, $_SESSION['$max']); It looks like you are trying to fetch a random question from DB, if it is right then you can try ORDER BY RAND() in your select. There is a sticky out there in MySQL board. Hope it helps Quote Link to comment Share on other sites More sharing options...
wsantos Posted November 23, 2007 Share Posted November 23, 2007 What do you mean repeating? This line would assure you that you have unique data in your $tempArray if (!in_array($qno,$tempArray)) Which data is repeating? Quote Link to comment Share on other sites More sharing options...
LOSTBOY Posted November 23, 2007 Author Share Posted November 23, 2007 $max is the Maximum question that user want to take.... Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 23, 2007 Share Posted November 23, 2007 I think wsantos is right What does your $tempArray contains can you show it ? Quote Link to comment Share on other sites More sharing options...
wsantos Posted November 23, 2007 Share Posted November 23, 2007 Anyway try this spaghetti code of mine do { $qno=rand(1, $_SESSION['max']); if (!in_array($qno,$tempArray)) $tempArray[] = $qno; }while(count($tempArray)<$_SESSION['max']); // This will ensure that all elements of tempArray is unique and has count equal to max for($i=0;$i<count($tempArray;$i++) { $query="SELECT q.qid,q.question,a.answer FROM question q JOIN answer a ON a.qid=a.qid WHERE qid=$tempArray[$i] " ; $result=mysql_query($query); $questionandanswer[]=mysql_fetch_row($result,MYSQL_ASSOC)) } // This will get all records of from your db with qid equal to the qids in your tempArray Quote Link to comment Share on other sites More sharing options...
LOSTBOY Posted November 23, 2007 Author Share Posted November 23, 2007 thanks let me try .... Quote Link to comment Share on other sites More sharing options...
LOSTBOY Posted November 23, 2007 Author Share Posted November 23, 2007 hi wsantos.... ur code works properly but it shows all the question & ans in single page..but i need to show one after another .....in the same page..wat can i do for that Quote Link to comment Share on other sites More sharing options...
wsantos Posted November 23, 2007 Share Posted November 23, 2007 What do you mean one after another? question 1 - answer 1 question 2 - answer 2 or question 1 question 2 then answer 1 answer 2 Paste your display snippet Quote Link to comment Share on other sites More sharing options...
LOSTBOY Posted November 23, 2007 Author Share Posted November 23, 2007 actually am trying this project as quiz system.so i need to show only one set at a time....... Quote Link to comment Share on other sites More sharing options...
wsantos Posted November 23, 2007 Share Posted November 23, 2007 In that case let's revise the last code I posted and since you are using sessions. Try this though I haven't tested this (working on imagination) $tempArray=$_SESSION['questionsandanswer']; // Make sure you get the value for the array $flag=0; do { $qno=rand(1, $_SESSION['max']); if (!in_array($qno,$tempArray)) { $tempArray[] = $qno; $flag = 1; // Used this to indicate that a record is added } }while(count($tempArray)<$_SESSION['max'] && $flag=0); // This will ensure that all elements of tempArray is unique and has count equal to max // This will only add one record to your tempArray every call to this page $id=$tempArray[count($tempArray)-1]; $query="SELECT q.qid,q.question,a.answer FROM question q JOIN answer a ON a.qid=a.qid WHERE qid=$tempArray[$id] " ; $result=mysql_query($query); $questionandanswer=mysql_fetch_row($result,MYSQL_ASSOC)) // This will get the last record of that tempArray $_SESSION['questionandanswer']=$tempArray; // Make sure you pass the updated tempArray Sorry for the spaghetti code guys... Quote Link to comment 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.