Jump to content

If Else if Statement Issue


cupaball

Recommended Posts

Hi ALL:

 

I am trying to pull the weekend dates based on the current day and pull some information based on the weekend. When $daynum = 4 the code works but when I change $daynum = 5 the I get nuthin. Can anyone help?

 

<?php

require("connection.php");

$daynum = '5';


if ($daynum == '4') {
$date1 = date("Y-m-d", strtotime("+1 days"));
$date2 = date("Y-m-d", strtotime("+3 days"));


$q = " SELECT `tb_events`.`date`, `tb_events`.`event_title`, `tb_events`.`info`
FROM `tb_events`
WHERE (`tb_events`.`date` >= DATE_FORMAT('$date1' , '%Y-%m-%d'))
AND (`tb_events`.`date` <= DATE_FORMAT('$date2' , '%Y-%m-%d'))
";

$result = $mysqli->query($q) or die($mysqli_error($mysqli));

if($result) {
        while($row = $result->fetch_object()) {
	$event = $row->event_title;
	$date = $row->date;
        $info = $row->info;

        print'<h3>Event: '.$event.'</h3>';
        print'<p>Date: '.$date.'</p>';
        print'<p>Event Info: '.$info.'</p>';

        }

   }
else if ($daynum == '5') {
$date1 = date("Y-m-d");
$date2 = date("Y-m-d", strtotime("+2 days"));

$q = " SELECT `tb_events`.`date`, `tb_events`.`event_title`, `tb_events`.`info`
FROM `tb_events`
WHERE (`tb_events`.`date` >= DATE_FORMAT('$date1' , '%Y-%m-%d'))
AND (`tb_events`.`date` <= DATE_FORMAT('$date2' , '%Y-%m-%d'))
";

$result = $mysqli->query($q) or die($mysqli_error($mysqli));

if($result) {
        while($row = $result->fetch_object()) {
	$event = $row->event_title;
	$date = $row->date;
        $info = $row->info;

        print'<h3>Event: '.$event.'</h3>';
        print'<p>Date: '.$date.'</p>';
        print'<p>Event Info: '.$info.'</p>';

        }

     }
   }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/145003-if-else-if-statement-issue/
Share on other sites

You should break it up into functions to make it cleaner.

 

The problem is you put this:


else if ($daynum == '5') {

 

else if won't execute how you wrote it (you put a space in between). This is how it needs to be done:


elseif ($daynum == '5') {

 

Also, you don't need to put ' around your 5:


elseif ($daynum == '5') {

 

Since its a numeral, you can just leave them off:


elseif ($daynum == 5) {

 

 

hope this helps! :D

 

You should break it up into functions to make it cleaner.

 

The problem is you put this:


else if ($daynum == '5') {

 

else if won't execute how you wrote it (you put a space in between). This is how it needs to be done:


elseif ($daynum == '5') {

 

Also, you don't need to put ' around your 5:


elseif ($daynum == '5') {

 

Since its a numeral, you can just leave them off:


elseif ($daynum == 5) {

 

 

hope this helps! :D

 

 

 

 

else if will work with a space

 

The problem is with his placement of curly brackets

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.