Jump to content

If Else problem


rocky48
Go to solution Solved by fastsol,

Recommended Posts

I am trying to check for the YEAR that I enter on a previous form and if the YEAR is 2015, do the first condition else if the YEAR is 2014 do the second condition.  THe reason for sorting on YEAR is to go to a diffierent table in the database.

 

When the YEAR satisfys the first condittion (2015), I get the correct data outputted, but when the second condition is entered (2014), no data is outputted.

 

In phpMyAdmin I have pasted the MYSQL query and substituted the Month and Year with correct data, it outputs data correctly for 2014.

Here is the code:

<?php
include("RelHol_connect.php");
doDB3();
$Year = $_POST["Year"];
echo $Year;
if ($Year= 2015){
$get_Event_sql  = "SELECT Events15.ID, Events15.Event_Date, Events15.Event_Description, Religion.ID, Religion.Religion
	FROM Events15
	LEFT JOIN Religion
	ON Religion.ID = Faith_ID
	WHERE MONTHNAME(Events15.Event_Date) = '".$_POST["Month"]."' And YEAR(Events15.Event_Date)= '".$_POST["Year"]."'
	ORDER BY Events15.Event_Date ASC";
$get_Event_res = mysqli_query($mysqli, $get_Event_sql) or die(mysqli_error($mysqli));
$array = array($get_Event_res);
print_r(array_values($array));

}
else{
$get_Event_sql  = "SELECT Events.ID, Events.Event_Date, Events.Event_Description, Religion.ID, Religion.Religion
	FROM Events
	LEFT JOIN Religion
	ON Religion.ID = Faith_ID
	WHERE MONTHNAME(Events.Event_Date) = '".$_POST["Month"]."' And YEAR(Events.Event_Date)= '".$_POST["Year"]."'
	ORDER BY Events.Event_Date ASC";
$get_Event_res = mysqli_query($mysqli, $get_Event_sql) or die(mysqli_error($mysqli));	
}
print_r($get_Event_res);

//create the display string
	$display_block = "<h2>
	<table width=\"100%\" cellpadding=\"3\" cellspacing=\"0\" border=\"1\" BGCOLOR=\"#87CEEB\" >
	<tr>
	<th>Date</th>
	<th>Event</th>
	<th>Faith</th>
	</tr>";

	while ($Event_info = mysqli_fetch_array($get_Event_res)) {
		$Event_Date = $Event_info['Event_Date'];
		$Event_text = nl2br(stripslashes($Event_info['Event_Description']));
		$Faith = $Event_info['Religion'];
		$Date = new DateTime(".$Event_Date.");
		$Date_frmt=$Date->format('d - F');
		
		//add to display
	 	$display_block .= "
		<tr>
		<td width=\"7%\" valign=\"top\">".$Date_frmt."</td>
		<td width=\"30%\" valign=\"top\">".$Event_text."</td>
		<td width=\"15%\" valign=\"top\">" .$Faith."</td>
		</tr>";
		
	}

	//free results
	mysqli_free_result($get_Event_res);


	//close connection to MySQL
	mysqli_close($mysqli);

	//close up the table
	$display_block .= "</table>";


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
	 <meta charset="utf-8">
    <meta name="generator" content="CoffeeCup HTML Editor (www.coffeecup.com)">
    <meta name="dcterms.created" content="Sat, 02 Mar 2013 20:21:23 GMT">
    <meta name="description" content="">
    <meta name="keywords" content="">
	<meta name="viewport" content="width=768px, minium-scale=1.0, maximum-scale=1.0" />
    <title>Religious Holidays & Festivals</title>
    <link rel="stylesheet" href="stylesheets/ipad.css" />
    <!--[if IE]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

</head>

<body>
<div id="wrap">
   <div id="header">
   		
  		<img src="images/Head!.png" width="100%" height="150px" alt="" title=" 1066 cards4U" border="0" />
		<h1>1066 cards4U</h1>
   </div>
   <div id="content">
      <div id="main">
	  <h1>Religious Holidays and Festivals for <? echo $_POST["Year"];?> </h1>
               </br> 
<?php echo $display_block; ?>
</div>
 <div id="side">
         <div id="sidea">
		 <h3>Main Menu</h3>
           <p><ul>
                <li><a href="index1.html">Home</a></li>
                <li><a href="iLinks.html">Links</a></li>
                <li><a href="iTechniques.html">Techniques</a></li>
				<li><a href="iVerse_Menu.html">Verse's</a></li>
                <li><a href="icontact.html">Contact Us</a></li>
				<li><a href="iNews.html">News</a></li>
				<li><a href="iAboutUs.html">About Us</a></li>
				<li><a href="iGallery.html">Gallery</a></li>
				<li><a href="iStats_Menu.html">Stats</a></li>
            </ul></p>
</br>
		 </div>
         <div id="sideb">
         </div>
 <div id="sidec">
		 
		 </div>
      </div>
   </div>
<div id="footer">
    <p>© 2012 Content:1066 Cards 4U. All rights reserved.   <br />
	With thanks to Matt Taylor - matthewjamestaylor.com</p>
		
   </div>
</div>   
</body>
</html>

Have I got the syntax wrong?  I do not get any error messages??

Your help would be appreciated.

Link to comment
Share on other sites

  • Solution

Couple things. First these lines

$Year = $_POST["Year"];

if ($Year= 2015)

Should be this

$Year = (int)$_POST["Year"]; // Cast to an integer for security and proper comparison below.

if ($Year == 2015) // Missing a =

But the bigger issue is that your DB structure looks very flawed.  You basically have 2 tables that are identical in what they store but you named the columns slightly different.  This is a huge waste of time and goes totally against proper db relationships.  It would be way way better to have just one table with a timestamp column that would have a value of whatever year you needed.  Then you only need one query that checks just the one column using the DATE() and possibly YEAR() in mysql to find the rows you need.

Link to comment
Share on other sites

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.