Hey guys I have a mysql database that has an events table. The events table consists of the fields Title, Date, Place and description. I am trying to display the table on the browser using php coding. it should list important events first according to the date. so as follows

title: xxx
date: xxx
Place:xxx
Description:xxxx

Here is my code so far but it does not seem to work the way I want it to to print the multiple events that may exists on screen in sequence of the date. Any help or advice is appreciated. Thanks

<?php

$con = mysql_connect("localhost", "root", "root") or die ("MYSQL connection error");

mysql_select_db (enetta, $con) or die("Unable to connect to the database");

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Assignment1</title>
</head>

<body>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Assignment 1</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<meta name="generator" content="HAPedit 3.1">
<link rel="stylesheet" type="text/css" href="../styles/defaultpage.css"/>
</head>

<body>

<table width = 600 border = 1>
<tr> <td>
<table border = 1 align = "right">
<tr> <td> <a href = "addevents.php"> Add Events </td> </tr>
</table>
</td> </tr>
<tr> <td>
<table width = 200>
<?php

$sql = "SELECT Title, Date, Place, Description FROM Events";
$data = mysql_query($sql);
?>
<tr> <td> Title: </td>
<?php $row = mysql_fetch_array($data);

echo "<td>" .$row["Title"]. "</td>";
?>
</tr>
<tr> <td> Date: </td>
<?php $row = mysql_fetch_array($data);
$newDate = date("d-m-Y", strtotime($row["Date"]));
echo "<td>" .$newDate. "</td>";
?>
</tr>

<tr> <td> Place </td>
<?php $row2 = mysql_fetch_array($data);

echo "<td>" .$row["Place"]. "</td>";
?>
</tr>

<tr> <td> Description </td>
<?php $row2 = mysql_fetch_array($data);

echo "<td>" .$row["Description"]. "</td>";
?>
</tr>
</table>
</td> </tr>

</table>

</body>
</html>

Member Avatar for LastMitch

@lizetta

In the future try to highlight the code and click "Code" on the text editor so it will appear light blue. So people can read it.

Here is my code so far but it does not seem to work the way I want it to to print the multiple events that may exists on screen in sequence of the date.

1) Are you connected to the database?
2) Can you select anything from the table?

You need to order the results in your query:

$sql = "SELECT Title, Date, Place, Description FROM Events ORDER BY Date ASC";

Either ASC or DESC depending which order you want them in

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.