Forum Moderators: coopster

Message Too Old, No Replies

simple day of the week schedule

         

generic

1:02 pm on May 21, 2009 (gmt 0)

10+ Year Member



Hi all, I'm trying to put together a simple day of the week type schedule. I was thinking of using a select field to choose a day of the week and a textarea to add/edit the info for that day. I don't need to be time specific by event, just have the ability to add general info for a given day. If there is a simpler or more efficient way, I'm all ears... :)


if($submit){
$sql="INSERT INTO schedule (day, text) VALUES ('$_POST[day]','$_POST[text]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
} else {
echo '<form action="cms.php" method="post">
<p>Day: <select name="day">
<option value="monday">Monday
<option value="tuesday">Tuesday
<option value="wednesday">Wednesday
<option value="thursday">Thursday
<option value="friday">Friday
<option value="saturday">Saturday
<option value="sunday">Sunday
</select></p>
<p>Text: <textarea name="text" cols="10" rows="5"></textarea></p>
<p><input type="submit" value="submit" /></p>
</form>';
}

example output:
MONDAY:
9am class
1pm game

TUESDAY:
11am art show
5pm dinner

Thanks in advance.

generic

4:33 pm on May 21, 2009 (gmt 0)

10+ Year Member



Hi all,

I made some changes and have this sort of working, but it's outputting two sets of update forms. Any idea why it would do this or how I could optimize the whole setup?

mySQL


CREATE TABLE `schedule` (
`monday` varchar(255) NOT NULL default '',
`tuesday` varchar(255) NOT NULL default '',
`wednesday` varchar(255) NOT NULL default '',
`thursday` varchar(255) NOT NULL default '',
`friday` varchar(255) NOT NULL default '',
`saturday` varchar(255) NOT NULL default '',
`sunday` varchar(255) NOT NULL default ''
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

PHP


$monday = $_GET['monday'];
$tuesday = $_GET['tuesday'];
$wednesday = $_GET['wednesday'];
$thursday = $_GET['thursday'];
$friday = $_GET['friday'];
$saturday = $_GET['saturday'];
$sunday = $_GET['sunday'];
$submit = $_GET['submit'];
if(isset($submit)) {
// handle form
mysql_query ("INSERT INTO schedule (monday, tuesday, wednesday, thursday, friday, saturday, sunday) VALUES ('$monday','$tuesday','$wednesday','$thursday','$friday','$saturday','$sunday')");
echo '<p class="strong">Your schedule has been updated.</p>
<p><a href="cms.php">Click here</a> to make more updates.</p>';
} else {
// display form
$result = mysql_query("SELECT * FROM schedule") or die(mysql_error());
while($row = mysql_fetch_array($result)) {
?>
<form action="cms.php" method="get">
<p class="strong">Monday</p>
<textarea name="monday" style="width:99%;height:75px;"><? echo $row['monday']; ?></textarea>
<hr />
<p class="strong">Tuesday</p>
<textarea name="tuesday" style="width:99%;height:75px;"><? echo $row['tuesday']; ?></textarea>
<hr />
<p class="strong">Wednesday</p>
<textarea name="wednesday" style="width:99%;height:75px;"><? echo $row['wednesday']; ?></textarea>
<hr />
<p class="strong">Thursday</p>
<textarea name="thursday" style="width:99%;height:75px;"><? echo $row['thursday']; ?></textarea>
<hr />
<p class="strong">Friday</p>
<textarea name="friday" style="width:99%;height:75px;"><? echo $row['friday']; ?></textarea>
<hr />
<p class="strong">Saturday</p>
<textarea name="saturday" style="width:99%;height:75px;"><? echo $row['saturday']; ?></textarea>
<hr />
<p class="strong">Sunday</p>
<textarea name="sunday" style="width:99%;height:75px;"><? echo $row['sunday']; ?></textarea>
<hr />
<input name="submit" type="submit" value="submit" />
</form>
<br />
<?php
}
mysql_close($conn);
}
?>

CyBerAliEn

8:57 pm on May 21, 2009 (gmt 0)

10+ Year Member



Everytime you input data into your forms; you are creating a new record in the DB. This is why you are getting multiple forms (you a looping through the retrieved SQL results).

If you want to keep your basic SQL design, you could get by changing your SQL query (for changes) from being "INSERT INTO" into a "UPDATE" form, ie:

UPDATE table_name SET `column1`='value',`column2`='value2' WHERE (some_column='some_value')

However, this design means you will only have one set of data:
Monday, Tues, Wed, etc

If you change 'Monday', it will overwrite what is for Monday previously. But it seems like this is what you are going for?

generic

9:03 pm on May 21, 2009 (gmt 0)

10+ Year Member



Ok, awesome, that did the trick. I really only need one set of weekday fields as the information will be overwritten/resaved anyway.

Cheers and thanks!