沃梦达 / 编程问答 / php问题 / 正文

带超链接的 PHP/MySQL 表

PHP/MySQL Table with Hyperlinks(带超链接的 PHP/MySQL 表)

本文介绍了带超链接的 PHP/MySQL 表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个 PHP 表单.一个显示事件列表,另一个显示每个特定事件的结果.在包含我想要的事件列表的页面上,可以创建超链接以访问每个事件的结果.

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 hyperlink can be created to access each individual event's results.

例如,在事件"页面上,我单击第 2 行的超链接,然后将我带到包含该特定事件结果的结果"页面.

For example, on the Events page I click on row 2's hyperlink which will then take me to the Results page that has the results for that specific event.

任何帮助将不胜感激,因为我对 PHP 非常非常陌生.如果需要任何额外的细节,请随时询问.

Any help would be appreciated as I am very, very new to PHP. If any extra details are needed, please feel free to ask.

谢谢.

抱歉,我将向您展示到目前为止事件表单的样子:

Sorry I'll show you what the Events form looks like so far:

<?php
mysql_connect('localhost','root','');
mysql_select_db('clubresults') or die( "Unable to select database");
$sql = "SELECT *, DATE_FORMAT(EventDate, '%d/%m/%y') as newdate FROM Events";
$result = mysql_query ($sql);
?>
<table border = 1>
<tr>
<th>Event ID</th>
th>Event Name</th>
<th>Event Date</th>
<th>Location</th>
</tr>
<?php
 while ($row = mysql_fetch_array($result))
{
echo "</td><td>" . $row['EventID'] . "</td><td>" . $row['EventName'] . "</td><td>" . $row['newdate'] . "</td><td>" . $row['Location'] . "</td><tr>";
}
echo "</table>";
mysql_close();
?>

推荐答案

你不需要两个脚本,只需要一个:

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('<h2>Event: %s</h2>', htmlspecialchars($event->title));
        # ...
    }
}

/* show the list if requested (or show it always, whatever pleases you) */

if (isset($_GET['list']) {
    echo '<table>';
    foreach($db->getEventList() as $event) {
        printf('<tr><td><a href="?event=%d">%s</a></td></tr>'
               , $event->ID, htmlspecialchars($event->title));
    }
    echo '</table>';
}

<小时>

正如我在你更新的问题中看到的,你应该从那些 oldskool mysql_* 函数切换到我在我的例子中概述的类样式,因为它很多使用更简单.这是一个与您接近的代码示例:


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:

<?php
/**
 * 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();

?>
<table border=1>
    <tr>
        <th>Event ID</th>
        th>Event Name</th>
        <th>Event Date</th>
        <th>Location</th>
    </tr>
<?php
foreach($db->getEvents() as $event)
{
     echo "</td><td>" . $event->EventID . "</td><td>" . $event->EventName . "</td><td>" . $event->newdate . "</td><td>" . $event->Location . "</td><tr>";
}
?>
</table>

这篇关于带超链接的 PHP/MySQL 表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:带超链接的 PHP/MySQL 表

基础教程推荐