newttster Posted August 26, 2012 Share Posted August 26, 2012 I'm sure there is a much better way to do this ... but I can't find it. No matter how I write it out ... it just does not work even though all the statements are correct. What am I doing wrong? $r2=($db->fetch_row($db->query("SELECT * FROM coursesdone WHERE userid = '{$ir['userid']}'"))); if (($r2['courseid']==1) AND ($r2['courseid']==2) AND ($r2['courseid']==3) AND ($r2['courseid']==4) AND ($r2['courseid']==5) AND ($r2['courseid']==6) AND ($r2['courseid']==7) AND ($r2['courseid']==8) AND ($r2['courseid']==9) AND ($r2['courseid']==10) AND ($r2['courseid']==11) AND ($r2['courseid']==12) AND ($r2['courseid']==13) AND ($r2['courseid']==14) AND ($r2['courseid']==15) AND ($r2['courseid']==16) AND ($r2['courseid']==17) AND ($r2['courseid']==18) AND ($r2['courseid']==19) AND ($r2['courseid']==20) AND ($r2['courseid']==21) AND ($r2['courseid']==22) AND ($r2['courseid']==23) AND ($r2['courseid']==24) AND ($r2['courseid']==25) AND ($r2['courseid']==26) AND ($r2['courseid']==27) AND ($r2['courseid']==28) AND ($r2['courseid']==29) AND ($r2['courseid']==30) AND ($r2['courseid']==31) AND ($r2['courseid']==32) AND ($r2['courseid']==33) AND ($r2['courseid']==34) AND ($r2['courseid']==35) AND ($r2['courseid']==36) AND ($r2['courseid']==37) AND ($r2['courseid']==38) AND ($r2['courseid']==39) AND ($r2['courseid']==40) AND ($r2['courseid']==41) AND ($r2['courseid']==42) AND ($r2['courseid']==43) AND ($r2['courseid']==44) AND ($r2['courseid']==45) AND ($r2['courseid']==46) AND ($r2['courseid']==47) AND ($r2['courseid']==48) AND ($r2['courseid']==49) AND ($r2['courseid']==50) AND ($r2['courseid']==51) AND ($r2['courseid']==52) AND ($r2['courseid']==53) AND ($r2['courseid']==54) AND ($r2['courseid']==55) AND ($r2['courseid']==56) AND ($r2['courseid']==57) AND ($r2['courseid']==58) AND ($r2['courseid']==59) AND ($r2['courseid']==60) AND ($r2['courseid']==61) AND ($r2['courseid']==62) AND ($r2['courseid']==63) AND ($r2['courseid']==64) AND ($r2['courseid']==65) AND ($r2['courseid']==66) AND ($r2['courseid']==67) AND ($r2['courseid']==68) AND ($r2['courseid']==69) AND ($r2['courseid']==70) AND ($r2['courseid']==71) AND ($r2['courseid']==72)) { echo"this"; } else { echo"this"; } Quote Link to comment Share on other sites More sharing options...
sniko Posted August 26, 2012 Share Posted August 26, 2012 (edited) Woah, that's a lot of same-gates being used! <?php //Create an array $courses = array(); //Populate for($i = 1; $i<=72; $i++) { $courses[] = $i; } //Check if( in_array($r2['courseid'], $courses) ) { echo 'This'; } else { echo 'This'; } ?> Try that? Here is it being executed: http://codepad.org/LkZScRXm Sorry Djk Edit - This basically acts as an OR gate. - If their is an occurrence in the array, it will display 'Yep'. I assumed that is what you're after, if not, I'll make amendments. Edit2 - Closer look, my original code isn't what you're after. - Not tested, I think it should work :) //Required Courses $reqCourses = array(); //Populate for($i = 1;$i<= 72; $i++) { $reqCourses[] = $i; } $verdict = null; $get = $db->query("SELECT * FROM coursesdone WHERE userid = '{$ir['userid']}'"); while( $verdict == null ) { while($r2 = $db->fetch_row($get) ) { if( !in_array($r['courseid'], $reqCourses) ) { $verdict = FALSE; } } elseif($verdict == null) { $verdict = TRUE; } else { $verdict = $verdict; } } echo ($verdict ? "You can do it!" : "You can't do it!"); Edited August 26, 2012 by sniko Quote Link to comment Share on other sites More sharing options...
newttster Posted August 26, 2012 Author Share Posted August 26, 2012 Thank you for the quick response, sniko. It does work ... but it is working in the sense that it meets the condition if only one of the courses is done. I want it so that it only executes when ALL the courses are done ... which is why I had the multitude of "Ands" . Any ideas? Quote Link to comment Share on other sites More sharing options...
sniko Posted August 26, 2012 Share Posted August 26, 2012 Have you had a look at at edit 2? Quote Link to comment Share on other sites More sharing options...
newttster Posted August 26, 2012 Author Share Posted August 26, 2012 (edited) Just saw it ... testing it now. Will let you know shortly. :) Getting ... Parse error: syntax error, unexpected T_ELSEIF in /home/danteawa/public_html/index.php on line 145 ... which is line 15 in your code. Edited August 26, 2012 by newttster Quote Link to comment Share on other sites More sharing options...
sniko Posted August 26, 2012 Share Posted August 26, 2012 Oooops, sorry //Required Courses $reqCourses = array(); //Populate for($i = 1;$i<= 72; $i++) { $reqCourses[] = $i; } $verdict = null; $get = $db->query("SELECT * FROM coursesdone WHERE userid = '{$ir['userid']}'"); while( $verdict == null ) { while($r2 = $db->fetch_row($get) ) { if( !in_array($r['courseid'], $reqCourses) ) { $verdict = FALSE; } } if($verdict == null) { $verdict = TRUE; } } echo ($verdict ? "You can do it!" : "You can't do it!"); Quote Link to comment Share on other sites More sharing options...
newttster Posted August 26, 2012 Author Share Posted August 26, 2012 *nods* Not getting the error any more. However ... it's still executing where only one of the courses is taken ... when it should only do that when all of them are taken. Quote Link to comment Share on other sites More sharing options...
Djkanna Posted August 26, 2012 Share Posted August 26, 2012 It's untested, but should provide what you're after. <?php $res = $db->query (' SELECT COUNT(`courseid`) AS `count` FROM `coursesdone` WHERE (`userid` = '.$ir['userid'].' && `courseid` BETWEEN 1 AND 72) '); if ($db->num_rows($res) == 1) { $count = $db->fetch_single($res); if ($count == 72) { echo 'You can do'; } else { echo 'You need to do all 72 courses first.'; } } Quote Link to comment Share on other sites More sharing options...
newttster Posted August 26, 2012 Author Share Posted August 26, 2012 Woot!! And we have a winner!! Thank you so much for that Dj ... works like a dream. sniko ... thank you for all of your time and effort on helping me with this. It is very much appreciated. 1 Quote Link to comment Share on other sites More sharing options...
Spudinski Posted August 26, 2012 Share Posted August 26, 2012 Woot!! And we have a winner!! Thank you so much for that Dj ... works like a dream. sniko ... thank you for all of your time and effort on helping me with this. It is very much appreciated. Just a tip for people having similar problem... Try to make MySQL do the most work, it will be much faster and efficient at the end of the day. +1 Djk. 1 Quote Link to comment Share on other sites More sharing options...
Djkanna Posted August 26, 2012 Share Posted August 26, 2012 You're welcome Newttster, glad it worked for you. @What Spud said: I had a wise fella say pretty much the exact same thing to me, it's good advice. Just don't do what I tried doing, and attempt to make MySQL do *everything*, doesn't end well. :P Quote Link to comment Share on other sites More sharing options...
Spudinski Posted August 26, 2012 Share Posted August 26, 2012 You're welcome Newttster, glad it worked for you. @What Spud said: I had a wise fella say pretty much the exact same thing to me, it's good advice. Just don't do what I tried doing, and attempt to make MySQL do *everything*, doesn't end well. :P GROUP_CONCAT. Haha. Quote Link to comment Share on other sites More sharing options...
sniko Posted August 27, 2012 Share Posted August 27, 2012 Thanks DJK and Spud - You learn something new everyday ;) No worries newttster 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.