Jump to content

[SOLVED] query date from database..


Dragen

Recommended Posts

Okay, I've had a couple of topics about similar things to this and unfortunatly had no replies.

I've scripted a calendar, based on a tutorial. My calendar displays a whole year in one go, iunstead of just a single month.

Here is the code below:

<?php
//this function displays the calendar
function showYear($month, $year){
?>
    <table border="1" cellpadding="0" cellspacing="0" width="100%" id="calendar">
    	<tr>
		<th width="16%" height="48" align="center" valign="bottom"><?php echo $year; ?></th>
		<th align="center" valign="middle">Sunday</th>
		<th align="center" valign="middle">Monday</th>
		<th align="center" valign="middle">Tuesday</th>
		<th align="center" valign="middle">Wednesday</th>
		<th align="center" valign="middle">Thursday</th>
		<th align="center" valign="middle">Friday</th>
		<th align="center" valign="middle">Saturday</th>
	</tr>
<?php
//check if $month is above 0 but below 13. If so it writes out a month, and increments $month by one.
//once it reaches 12 it stops.
while($month > 0 && $month < 13)
{
showMonth($month, $year);
$month++;
}
?>
</table>
<?php
}

//This function writes the months
function showMonth($month, $year)
{
    $date = mktime(12, 0, 0, $month, 1, $year);
    $daysInMonth = date("t", $date);
$booked_day1 = 2;
$booked_month = date("n", $date);
$booked[$booked_month] = array(1 , 2 , 5 );
    // calculate the position of the first day in the calendar (sunday = 1st column, etc)
    $offset = date("w", $date);
    $rows = 1;

	echo "\t\t<tr>\n";
	//displays month
	echo "\t\t\t<td align=\"right\"><strong>". date(F, $date) . "</strong></td>\n";

    for($i = 1; $i <= $offset; $i++)
    {
	echo "\t\t\t<td width=\"12%\"></td>\n";
    }
    for($day = 1; $day <= $daysInMonth; $day++)
    {
        if( ($day + $offset - 1) % 7 == 0 && $day != 1)
        {
	echo "\t\t</tr>\n";
	echo "\t\t<tr>\n";
	echo "\t\t\t<td></td>\n";
            $rows++;
        }
	//checks if date is booked. If so sets it to class="booked"
	echo "\t\t\t<td align=\"center\" valign=\"middle\" width=\"12%\" height=\"48\">";
	echo $day . "</td>\n"; //this is the $day statement I need to verify!!
    }
    while( ($day + $offset) <= $rows * 7)
    {
	echo "\t\t\t<td></td>\n";
        $day++;
    }
	echo "\t\t</tr>\n";
}
?>

What I want to do is when the

echo $day . "</td>\n";

part is written on line 60, I need to query the date with a table in my database. I'm not sure how to do so, but someon has told me I should probably use an array.

I've got this code which turns my table into an array and then displays it:

$sql = "SELECT day, month, year FROM booked";
$res = mysql_query($sql);

$booked = array();
while ($row = mysql_fetch_assoc($res)) {
       $booked[] = $row;
}
echo '<pre>', print_r($booked, true), '</pre>';

I need help in using this to verify the date from the given array using an if statement. Basically if $day is equall to one of the dates listed, then it performs some code.

The problem is $day is just the day, but the info from the table is day month and year, so I need to check that $day is in the right month and year as well...

 

Hopefully I've explained it clearly..

Any help would be greatly welcome.. and needed!

Link to comment
https://forums.phpfreaks.com/topic/46334-solved-query-date-from-database/
Share on other sites

okay, I'm not too good at arrays,

but I think what I need to do is query the data with an if statement such as

		if ($year == year && $month == month && $day == day){ //getting year, month, day from my array, taken from my table
	// insert code here
	}
	echo $day . "</td>\n";

But as it's trying to get the information from an array, I'm not sure how to go about doing it.

Please can someone help. I've been trying to figure it out for a while and I need to get this sorted quickly for a client.

 

Thanks

<?php
//this function displays the calendar
function showYear($month, $year){
?>
    <table border="1" cellpadding="0" cellspacing="0" width="100%" id="calendar">
    	<tr>
		<th width="16%" height="48" align="center" valign="bottom"><?php echo $year; ?></th>
		<th align="center" valign="middle">Sunday</th>
		<th align="center" valign="middle">Monday</th>
		<th align="center" valign="middle">Tuesday</th>
		<th align="center" valign="middle">Wednesday</th>
		<th align="center" valign="middle">Thursday</th>
		<th align="center" valign="middle">Friday</th>
		<th align="center" valign="middle">Saturday</th>
	</tr>
<?php
//check if $month is above 0 but below 13. If so it writes out a month, and increments $month by one.
//once it reaches 12 it stops.
while($month > 0 && $month < 13)
{
showMonth($month, $year);
$month++;
}
?>
</table>
<?php
}

//This function writes the months
function showMonth($month, $year)
{
    $date = mktime(12, 0, 0, $month, 1, $year);
    $daysInMonth = date("t", $date);
$booked_day1 = 2;
$booked_month = date("n", $date);
$booked[$booked_month] = array(1 , 2 , 5 );
    // calculate the position of the first day in the calendar (sunday = 1st column, etc)
    $offset = date("w", $date);
    $rows = 1;

	echo "\t\t<tr>\n";
	//displays month
	echo "\t\t\t<td align=\"right\"><strong>". date(F, $date) . "</strong></td>\n";

    for($i = 1; $i <= $offset; $i++)
    {
	echo "\t\t\t<td width=\"12%\"></td>\n";
    }
    for($day = 1; $day <= $daysInMonth; $day++)
    {
        if( ($day + $offset - 1) % 7 == 0 && $day != 1)
        {
	echo "\t\t</tr>\n";
	echo "\t\t<tr>\n";
	echo "\t\t\t<td></td>\n";
            $rows++;
        }
	//checks if date is booked. If so sets it to class="booked"
	echo "\t\t\t<td align=\"center\" valign=\"middle\" width=\"12%\" height=\"48\">";
	echo $day . "</td>\n"; //this is the $day statement I need to verify!!

$sql = "SELECT day, month, year FROM booked";
$res = mysql_query($sql);

while ($row = mysql_fetch_assoc($res)) {

if( $year == $row[year] ) 
{
	if( $month == $row[month] ) 
	{
		if( $day == $row[day] )
		{
		echo "booked?";
		} 
	}
}


    }
    while( ($day + $offset) <= $rows * 7)
    {
	echo "\t\t\t<td></td>\n";
        $day++;
    }
	echo "\t\t</tr>\n";
}
?>

oh wow! oh wow!!!

I am so very happy right now! I've been trying for so long to get this to work and at last it does!

I did edit the code you gave me a bit for it to work though.. here's my final outcome:

<?php
require ('database.php');
//this function displays the calendar
function showYear($month, $year){
?>
    <table border="1" cellpadding="0" cellspacing="0" width="100%" id="calendar">
    	<tr>
		<th width="16%" height="48" align="center" valign="bottom"><?php echo $year; ?></th>
		<th align="center" valign="middle">Sunday</th>
		<th align="center" valign="middle">Monday</th>
		<th align="center" valign="middle">Tuesday</th>
		<th align="center" valign="middle">Wednesday</th>
		<th align="center" valign="middle">Thursday</th>
		<th align="center" valign="middle">Friday</th>
		<th align="center" valign="middle">Saturday</th>
	</tr>
<?php
//check if $month is above 0 but below 13. If so it writes out a month, and increments $month by one.
//once it reaches 12 it stops.
while($month > 0 && $month < 13)
{
showMonth($month, $year);
$month++;
}
?>
</table>
<?php
}

//This function writes the months
function showMonth($month, $year)
{
$sql = "SELECT day, month, year FROM booked";
$res = mysql_query($sql);

$booked = array();
while ($row = mysql_fetch_assoc($res)) {
      	 $booked[] = $row;
}


    $date = mktime(12, 0, 0, $month, 1, $year);
    $daysInMonth = date("t", $date);
    // calculate the position of the first day in the calendar (sunday = 1st column, etc)
    $offset = date("w", $date);
    $rows = 1;

	echo "\t\t<tr>\n";
	//displays month
	echo "\t\t\t<td align=\"right\"><strong>". date(F, $date) . "</strong></td>\n";

    for($i = 1; $i <= $offset; $i++)
    {
	echo "\t\t\t<td width=\"12%\"></td>\n";
    }
    for($day = 1; $day <= $daysInMonth; $day++)
    {
        if( ($day + $offset - 1) % 7 == 0 && $day != 1)
        {
	echo "\t\t</tr>\n";
	echo "\t\t<tr>\n";
	echo "\t\t\t<td></td>\n";
            $rows++;
        }
	//checks if date is booked. If so sets it to class="booked"
	echo "\t\t\t<td align=\"center\" valign=\"middle\" width=\"12%\" height=\"48\"";

$sql = "SELECT day, month, year FROM booked";
$res = mysql_query($sql);

while ($row = mysql_fetch_assoc($res)) {

if( $year == $row[year] ) 
{
	if( $month == $row[month] ) 
	{
		if( $day == $row[day] )
		{
	echo " class=\"booked\"";
		} 
	}
}
}


	echo ">" . $day . "</td>\n";
    }
    while( ($day + $offset) <= $rows * 7)
    {
	echo "\t\t\t<td></td>\n";
        $day++;
    }
	echo "\t\t</tr>\n";
}
?>

Thank you so much for the help!

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.