Hi,
I am trying to create a database of sightings of whales.
This database will need to contain the date of the sighting, and what was seen; what I will also need to do is put them in date order and I'll need to have a title for each month that shows automatically before the listings for each month.
What I can't work out is how to put the date listing in (I can't use basic timestamp I don't think, because I may not put the sightings in on the day that they're seen, perhaps a few days later).
What I also want to record is who adds the sighting, but just for reference, not to be shown in the list.
Also, I don't know how to dynamically put the month title before that month's listings.
Here's what I have so far to add data to the database (obviously, current_date isn't what I would want to display when I output):
<?php
// include the database configuration and
// open connection to database
include 'library/config.php';
include 'library/opendb.php';
// check if the form is submitted
if(isset($_POST['btnSign']))
{
// get the input from $_POST variable
// trim all input to remove extra spaces
$name = trim($_POST['txtName']);
$message = trim($_POST['mtxMessage']);
// escape the message ( if it's not already escaped )
if(!get_magic_quotes_gpc())
{
$name = addslashes($name);
$message = addslashes($message);
}
// prepare the query string
$query = "INSERT INTO sightings (name, message, entry_date) " .
"VALUES ('$name', '$message', current_date)";
// execute the query to insert the input to database
// if query fail the script will terminate
mysql_query($query) or die('Error, query failed. ' . mysql_error());
// redirect to current page so if we click the refresh button
// the form won't be resubmitted ( as that would make duplicate entries )
header('Location: ' . $_SERVER['REQUEST_URI']);
// force to quite the script. if we don't call exit the script may
// continue before the page is redirected
exit;
}
?>
<html>
<head>
<title>Add Sightings</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="styles/styles.css">
<script language="JavaScript">
/*
This function is called when
the 'Add Sighting' button is pressed
Output : true if all input are correct, false otherwise
*/
function checkForm()
{
// the variables below are assigned to each
// form input
var gname, gmessage;
with(window.document.guestform)
{
gname = txtName;
gmessage = mtxMessage;
}
// if name is empty alert the visitor
if(trim(gname.value) == '')
{
alert('Please enter your name');
gname.focus();
return false;
}
// alert the visitor if sighting details are empty
else if(trim(gmessage.value) == '')
{
alert('Please enter details of the sighting');
gmessage.focus();
return false;
}
else
{
// when all input are correct
// return true so the form will submit
return true;
}
}
/*
Strip whitespace from the beginning and end of a string
Input : a string
Output : the trimmed string
*/
function trim(str)
{
return str.replace(/^\s+|\s+$/g,'');
}
</script>
</head>
<body>
<form method="post" name="guestform">
<table width="550" border="0" cellpadding="2" cellspacing="1">
<tr>
<td width="100">Name *</td> <td>
<input name="txtName" type="text" id="txtName" size="30" maxlength="30"></td>
</tr>
<tr>
<td width="100">Message *</td> <td>
<textarea name="mtxMessage" cols="80" rows="5" id="mtxMessage"></textarea></td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="btnSign" type="submit" id="btnSign" value="Add Sighting" onClick="return checkForm();"></td>
</tr>
</table>
</form>
<br>
<br>
<?php
// close the database connection since
// we no longer need it
include 'library/closedb.php';
?>
</body>
</html>
And here is what I have to display the results so far:
<?php
$query = "SELECT message FROM sightings";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo " \"...{$row['message']}...\" <br>";
}
include 'closedb.php';
?>
As I said, I don't know how to input the date properly and then how to output it into the listing. I also don't know how to sort my data by date or how to put the headings above each month's listings. I've just been doing this in HTML before, but as it's got bigger, it's become increasingly more unsightly for the HTML.
Any help would be much appreciated, bearing in mind that I don't really understand arrays or in depth php.
Thanks,
M@