Jump to content

Looping and echoing calender maybe?


grahamb314

Recommended Posts

Hi all,

I have a booking system using php to write a name, date and time to a db

 

I have a DB with fields as such : id name day month year slot

id is just an auto increment int

name is a varchar

day is an int 0 for monday, 6 for sunday

month is an int 0 for jan, 11 for dec

year is a 4 digit int eg 2008

slot is an int (0 to whatever)

the slot 0 represents a time  24 HOUR CLOCK (09:00)

slot 1 represents a time (09:30) etc etc up untill slot 15 (17:30)

 

What I want to do is display all the entries in the DB in some reasonable order which someone can understand (Ie someone to see what is booked and what isnt)

 

Does someone have some code for me to look at that would do this?

 

I'm quite new to php, but eager to learn so any help would be fantastic!

 

Link to comment
Share on other sites

i think its easyer to not use integers for all the different dateparts, but just a datetime (thats one of the options in your database to store data) and than get it out of the database in the way you want with the option DATE_FORMAT.

 

but anyway, it works in your way too, although it takes a lot of code

 

to display it on your website, get it out of the database, change it to the info you want and put it in the order you want, for example:

 

$result = mysql_query("SELECT * FROM table" );

$row = mysql_fetch_array($result);

 

if($row['day'] == '0') {$day= 'monday';}

elseif($row['day'] == '1') {$day= 'tuesday';}

elseif($row['day'] == '2') {$day= 'wednesday';}

elseif($row['day'] == '3') {$day= 'thursday';}

etc

 

if($row['month'] == '0') {$month= 'jan';}

elseif($row['month'] == '1') {$month= 'feb';}

elseif($row['month'] == '2') {$month= 'mar';}

elseif($row['month'] == '3') {$month= 'apr';}

etc etc

 

echo 'you have booked on: ';

echo $day.' '.$month.' '.$year.' on '.$slot;

 

 

 

 

 

 

Link to comment
Share on other sites

Thanks that seems to be great for showing what you booked,

 

but what about looping through and displaying ALL booked days and times?

Say the booking is for hotel rooms, then you need to know in advance which to clean, so you need an overview ie echo all bookings to date

 

I dont know how to do this in a nice tidy way, possibly within a table?

Link to comment
Share on other sites

you can show all the bookings with the code:

 

while ($row = mysql_fetch_array($result))

{

 

}

 

this while loops through all the data stored in the database and shows it in the way that you put beween {}, so that would be the code i did write in the previous post. If you want to put it in a table, you can use the following:

 

echo '<table><tr><td>dag<td>month<td>year<td>time';

while ($row = mysql_fetch_array($result))

{

(all the code etc etc)

 

echo '<tr><td>'$day.'<td>'.$month.'<td>'.$year.'<td>'.$slot;

}

</table>

Link to comment
Share on other sites

I prefer a visual rather than a textual interface

 

<?php
include 'db.inc.php';       // db connection stuff

/**
*  Dates for query
*/
$wk_commence = isset($_GET['wk_commence']) ? strtotime($_GET['wk_commence']) : strtotime('next monday');
$dstart = date ('Y-m-d', $wk_commence);
$dend   = date ('Y-m-d', strtotime('+4 days', $wk_commence));

/**
*  Dates for current week output  
*/
$bookings = array();
for ($i=0; $i<5; $i++)
{
    $bookings[date('Y-m-d', strtotime("+$i days", $wk_commence))] = array_pad(array(),15,'');
}

/**
* timeslots headings
*/
$timeslots = array();
$firstslot = mktime(9,0,0);
for ($i=0; $i<16; $i++)
{
    $m = 30*$i;
    $timeslots[$i] = date ('H i', strtotime("+$m minutes", $firstslot));
}

/**
* Bookings data
*/
$sql = "SELECT name, date_booked, slot
        FROM booking
        WHERE date_booked BETWEEN '$dstart' AND '$dend'";
$res = mysql_query($sql);
while (list($name, $date, $slot) = mysql_fetch_row($res))
{
    $bookings[$date][$slot] = $name;
}

?>
<html>
<head>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta name="generator" content="PhpED Version 4.6.3 (Build 4633)">
<title>Bookings sample code</title>
<meta name="author" content="Barand">
<link rel="shortcut icon"  href="">
<meta name="creation-date" content="09/27/2008">
<style type='text/css'>
th  {
    background-color: #369;
    color: #FFF;
    font-family: verdana;
    font-size: 9pt;
    }
td  {
    background-color: #CCC;
    color: #000;
    font-family: verdana;
    font-size: 9pt;
    }
.booked {
    background-color: #FFC0C0;
    }
</style>
</head>
<body>
    <?php 
    /**
    * output bookings
    */
    echo "<table border='0' cellspacing='1' cellpadding='3'>\n";
        // heading
    echo "<tr><th>Date</th>\n";
    foreach ($timeslots as $ts)
    {
        echo "<th>$ts</th>\n";
    }
    echo "</tr>\n";

        // bookings
    foreach ($bookings as $d => $slotsdata)
    {
        echo '<tr><th>' . date ('D d M', strtotime($d)) . "</th>\n";
        for($i=0;$i<16;$i++)
        {
            $cl = $slotsdata[$i] == '' ? '' : "class='booked'";
            $t  = $slotsdata[$i] == '' ? '' : $slotsdata[$i];
            echo "<td $cl title='$t'>&nbsp</td>\n";
        }
        echo "</tr>\n";
    }
    echo "</table>\n";
    ?>
<hr/>
<form>
Week commencing 
<select name='wk_commence'>
         <option value='2008-09-29'>Mon Sep 29</option>
         <option value='2008-10-06'>Mon Oct 06</option> 
</select>
<br/>
<input type="submit" name="btn_submit" value="Submit">
</body>
</html>

 

The test data

DROP TABLE IF EXISTS `booking`;
CREATE TABLE `booking` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `name` varchar(45) NOT NULL,
  `date_booked` date NOT NULL,
  `slot` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`id`)
) ;


INSERT INTO `booking` VALUES 
(1, 'Peter', '2008-09-29', 0),
(2, 'Paul', '2008-09-29', 3),
(3, 'Paul', '2008-09-29', 4),
(4, 'Mary', '2008-09-30', 6),
(5, 'John', '2008-10-02', 15),
(6 ,'Mary', '2008-10-02', ;
(7 ,'Jane', '2008-10-08', ;

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.