2

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.

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.

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

Thanks.

Edit: 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();
?>
1
  • What will the URL for a specific event look like? Commented May 16, 2012 at 15:49

1 Answer 1

2

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>';
}

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:

<?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>
Sign up to request clarification or add additional context in comments.

2 Comments

Are you sure I dont need something like echo '<a href="page2.php?ID='.$row['ID'].'">View Details</a>'; on one page and $query = "SELECT * FROM events WHERE ID=".intval($_REQUEST['ID']); on the results (This is something I've seen elsewhere online.)
you need to do that only if the filename differs. if the filename is the same, you can start with ?.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.