您不需要两个脚本,只需一个:
events.php?list
events.php?event=1234
在那里你只需要检查一下:
$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 '
';}
编辑:正如我在更新的问题中看到的那样,您应该从那些oldskool mysql_ *函数切换到我在示例中概述的类样式,因为它使用起来要简单得多.这是一个接近你的代码示例:
/**
* My First PDO Databaseclass
*/
class Database extends PDO
{
public function __construct()
{
$host = 'localhost';
$name = 'clubresults';
$user = 'root';
$pass = NULL;
parent::__construct("mysql:host=$host;dbname=$name", $user, $pass);
$this->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 DateLocationforeach($db->getEvents() as $event)
{
echo "
" . $event->EventID . "" . $event->EventName . "" . $event->newdate . "" . $event->Location . "";}
?>