Set empty variable to wildcard?
Posted: Thu May 21, 2015 10:18 am
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:
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 />";
}
}
?>