Set empty variable to wildcard?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mairtin
Forum Newbie
Posts: 2
Joined: Thu May 21, 2015 9:04 am

Set empty variable to wildcard?

Post by Mairtin »

Hi,
Newbie to php and also newbie to forums. Have searched the forum but can't seem to get a solution to my particular problem. (Maybe I have phrased it wrong?)
I'm coding a website of historical events. The form queries a db with Year, Month and Day values and returns Events for selected dates.
Some Events however do not occur on a particular day (or I don't have the exact date to hand). I have set a 'required' field for Year and as every Event has a corresponding Year value, that works fine.
What I hope to achieve is that a user may enter only the Year value and the results for the whole year are returned, similarily the user may enter Year and Month values, but if the Day value is ommitted, (or there is no Event for that Day) the results of the whole month are returned (then at least the user will see some results).
I have been trying to use the 'if (empty($day))' code but cannot seem to find the right syntax to change the $day varaible (if empty) to something that will allow php to see the value as anything (i.e '01' -'31') and thus display all values within that Month. I have attempted the % wildcard, but to no avail.
Hope someone can help,
Best regards:

Máirtín
Attached code:

Code: Select all

<?php
					
		$link=mysqli_connect("localhost","root", "", "annals");
		if (mysqli_connect_error()) 
		{
			die ("Could not connect to database");
		}				
			$year= $_POST['year'];
			$Month= $_POST['month'];
			$month= ucwords($Month); //outputs Uppercase month//
			$day= $_POST['day'];
			
			
			if (empty ($year))
			{
			echo "you must enter a year";
			exit;
			}
			else
			{
					if (!is_numeric($year))
					{
					echo "The year must be numeric";
						exit;
					}
			}			 		
							
			$query = "SELECT * FROM annals WHERE Year='$year' AND Month = '$month' AND Day = '$day'";
			if (empty($month) && empty($day))
			{
				?>
            		<h4 class="green"> Results Found for <?php echo "$year";?>
            		</h4><p class="beag">        
				<?php
				}
            	else if (!empty($month) && empty($day))
				{ [color=#FF0040]try to change $day to some value[/color]
					?>
            		<h4 class="green">Results Found for <?php echo "$month" ." ". "$year"  ;?>
            		</h4><p class="beag">           
				<?php				
				}
					else if(!empty($year) &&!empty($month) && !empty($day))
			 		{
					?>
            		<h4 class="green">Results found for <?php echo "$day"." " ."$month" ." ". "$year" ;?>
           			</h4><p class="beag">           
				<?php
					}
					if ($result=mysqli_query($link, $query)) 
					{					
						while ($row = mysqli_fetch_array($result))																	
					{											
						echo"<br />" . $row['Event']."<br />"."<br />";
					}
					}
				?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Set empty variable to wildcard?

Post by Christopher »

First, using three different fields when databases have a DATE column type is crazy -- use a single DATE column!

What you really want is to check for events between two dates. I included start and end years, because eventually you'll want to search a date range that spans January 1st. To do that, just add a second set of form fields for the second data.

Or better, use a nice Javascript date picker that will return YYYY-MM-DD dates in start_date and end_date fields.

Here is some example code.

Code: Select all

                              
$year= intval($_POST['year']);
$month= intval($_POST['month']);
$day= intval($_POST['day']);
                       
$year_start = $year;
$year_end = $year;

if (empty($month)) {
    $month_start = $month;
    $month_end = $month;
} else {
    $month_start = 1;
    $month_end = 12;
}

if (empty($day)) {
    $day_start = $day;
    $day_end = $day;
} else {
    $day_start = 1;
    $day_end = 31;
}
                                                                            
$sql = "SELECT * FROM annals WHERE event_date>='$year_start-$month_start-$day_start' AND event_date<='$year_end-$month_end-$day_end'";
if ($result=mysqli_query($link, $query)) {                                      
     while ($row = mysqli_fetch_array($result)) {                                                                                      
          echo"<br />" . $row['Event']."<br />"."<br />";
    }
}
(#10850)
Mairtin
Forum Newbie
Posts: 2
Joined: Thu May 21, 2015 9:04 am

Re: Set empty variable to wildcard?

Post by Mairtin »

Excellent!
pure logic, Can't think of hy I couldn't see that!

Many thanks.

M
Post Reply