Jump to content

getting a case statement to work


ultraloveninja

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/158145-getting-a-case-statement-to-work/
Share on other sites

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)){

}
}
?>

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)) );

<?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)) );
   }
}
?>

???

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.