ultraloveninja Posted May 14, 2009 Share Posted May 14, 2009 I am trying to get something to display from a mysql query using a case statement, but I am having trouble trying to get it to work right. basically, i am trying to get it to display one thing if there is a result that comes back true. but if it doesn't i want it to use a different mysql query and display something else and i am just confused on how to get it to work. here is my code: <?php include 'dbconn.php'; $sql = "select * from picks where expiredate = curdate() and curtime() < expiretime and starttime < curtime();"; $result = mysql_query($sql,$conn); $anymatches=mysql_num_rows($result); if ($anymatches == 0) { $sql = "select * from picks where expiredate > curdate() order by expiredate asc limit 1;"; $result = mysql_query($sql,$conn); echo <<<END <div align="center">There is no pick available at this time $edate</div> END; } while ($row = mysql_fetch_array($result)){ $pick = $row['pick']; $event = $row['event']; $starttime = $row['starttime']; $endtime = $row['expiretime']; $enddate = $row['expiredate']; $etime = ( date("g:i a", strtotime($endtime)) ); $stime = ( date("g:i a", strtotime($starttime)) ); $edate = ( date("m/d/Y", strtotime($enddate)) ); echo <<<END <div align="center">This current pick is available:<br /> $event<br /> END; } ?> more or less this is the part that i am having trouble with: if ($anymatches == 0) { $sql = "select * from picks where expiredate > curdate() order by expiredate asc limit 1;"; $result = mysql_query($sql,$conn); echo <<<END <div align="center">There is no pick available at this time $edate</div> END; } I am not sure what needs to happen there, I guess. I thought that I had this working, but I guess not. Quote Link to comment https://forums.phpfreaks.com/topic/158145-getting-a-case-statement-to-work/ Share on other sites More sharing options...
JonnoTheDev Posted May 14, 2009 Share Posted May 14, 2009 I am trying to get something to display from a mysql query using a case statement You mean a conditional statement. A case is something completely different Check your logic carefully <?php include 'dbconn.php'; $result = mysql_query("SELECT * FROM picks WHERE expiredate = CURDATE() AND CURTIME() < expiretime AND starttime < CURTIME()" ,$conn); $showResults = true; if(!mysql_num_rows($result)) { unset($result); // there are no matches echo "<div align=\"center\">There is no pick available at this time ".$edate."</div>"; // run a second query $result = mysql_query("SELECT * FROM picks WHERE expiredate > CURDATE() ORDER BY expiredate ASC LIMIT 1",$conn); // check results if(!mysql_num_rows($result)) { $showResults = false; } } // display the results from either the first or second query if($showResults) { while ($row = mysql_fetch_assoc($result)){ } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/158145-getting-a-case-statement-to-work/#findComment-834197 Share on other sites More sharing options...
ultraloveninja Posted May 14, 2009 Author Share Posted May 14, 2009 Cool. Thanks for clearing that up. Just need to figure out where to put the array: while ($row = mysql_fetch_array($result)){ $pick = $row['pick']; $event = $row['event']; $starttime = $row['starttime']; $endtime = $row['expiretime']; $enddate = $row['expiredate']; $etime = ( date("g:i a", strtotime($endtime)) ); $stime = ( date("g:i a", strtotime($starttime)) ); $edate = ( date("m/d/Y", strtotime($enddate)) ); Quote Link to comment https://forums.phpfreaks.com/topic/158145-getting-a-case-statement-to-work/#findComment-834231 Share on other sites More sharing options...
ultraloveninja Posted May 14, 2009 Author Share Posted May 14, 2009 Yeah...I am offically confused now. I am not sure where the variables need to go. Quote Link to comment https://forums.phpfreaks.com/topic/158145-getting-a-case-statement-to-work/#findComment-834276 Share on other sites More sharing options...
Brian W Posted May 14, 2009 Share Posted May 14, 2009 <?php include 'dbconn.php'; $result = mysql_query("SELECT * FROM picks WHERE expiredate = CURDATE() AND CURTIME() < expiretime AND starttime < CURTIME()" ,$conn); $showResults = true; if(!mysql_num_rows($result)) { unset($result); // there are no matches echo "<div align=\"center\">There is no pick available at this time ".$edate."</div>"; // run a second query $result = mysql_query("SELECT * FROM picks WHERE expiredate > CURDATE() ORDER BY expiredate ASC LIMIT 1",$conn); // check results if(!mysql_num_rows($result)) { $showResults = false; } } // display the results from either the first or second query if($showResults) { while ($row = mysql_fetch_assoc($result)){ $pick = $row['pick']; $event = $row['event']; $starttime = $row['starttime']; $endtime = $row['expiretime']; $enddate = $row['expiredate']; $etime = ( date("g:i a", strtotime($endtime)) ); $stime = ( date("g:i a", strtotime($starttime)) ); $edate = ( date("m/d/Y", strtotime($enddate)) ); } } ?> ??? Quote Link to comment https://forums.phpfreaks.com/topic/158145-getting-a-case-statement-to-work/#findComment-834278 Share on other sites More sharing options...
ultraloveninja Posted May 14, 2009 Author Share Posted May 14, 2009 yeah, that's what i thought at first, but then it will look like it's true regardless and pull the next query and adds it in there when it's not supposed to. meh. i am not that well versed in PHP to understand most what needs to be done for the most part. i am just going to try something else and see if that works. Quote Link to comment https://forums.phpfreaks.com/topic/158145-getting-a-case-statement-to-work/#findComment-834309 Share on other sites More sharing options...
JonnoTheDev Posted May 15, 2009 Share Posted May 15, 2009 You only learn from your mistakes Quote Link to comment https://forums.phpfreaks.com/topic/158145-getting-a-case-statement-to-work/#findComment-834608 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.