Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

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

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

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.