I have 2 PHP forms. One displays a list of events and the other displays the results of each specific event. On the page with the list of events I would like it so that a hy
You don't need two scripts, but just one:
events.php?list
events.php?event=1234
in there you only need to check for things:
$db = new Database(); # simplified
/* show event details if requested */
if (isset($_GET['event']) {
if ($event = $db->getEventByID($_GET['event'])) {
printf('Event: %s
', htmlspecialchars($event->title));
# ...
}
}
/* show the list if requested (or show it always, whatever pleases you) */
if (isset($_GET['list']) {
echo '';
foreach($db->getEventList() as $event) {
printf('%s '
, $event->ID, htmlspecialchars($event->title));
}
echo '
';
}
Edit: As I saw in your updated question, you should switch from those oldskool mysql_*
functions to the class style I outlined in my example, because it is much simpler to use. Here is a code-example that is close to yours:
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// $this->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
public function getEvents()
{
$sql = "SELECT *, DATE_FORMAT(EventDate, '%d/%m/%y') as newdate FROM Events";
return $this->query($sql, PDO::FETCH_OBJ );
}
public function getEventByID($id)
{
$sql = sprintf("SELECT * FROM Events WHERE EventID = %d;", $id);
return $this->query($sql)->fetchObject();
}
}
$db = new Database();
?>
Event ID
th>Event Name
Event Date
Location
getEvents() as $event)
{
echo "" . $event->EventID . " " . $event->EventName . " " . $event->newdate . " " . $event->Location . " ";
}
?>