Jump to content
MakeWebGames

Recommended Posts

Posted

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";
}
Posted (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 by sniko
Posted

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?

Posted (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 by newttster
Posted

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

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.';
   }
}
Posted

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.

  • Like 1
Posted
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.

  • Like 1
Posted

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

Posted
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.

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.   Paste as plain text instead

  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...