Displaying content of database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dave.hawksworth
Forum Newbie
Posts: 11
Joined: Thu Feb 18, 2010 8:45 am

Displaying content of database

Post by dave.hawksworth »

I am new to php and am trying to create an interface to allow users to view and update a database.
I have used the following code from a tutorial modified for my database, but the display obtained is a vertical table of each record rather than the spreadsheet view I was looking for.
The html section showing the column headers appears at the bottom of the display rather than the top.
I would be grateful for any advice on how to achieve the desired result.
Thanks
Dave

<?
$username="bnsquash";
$password="can1squash";
$database="bnsquash_courttime";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM courttimes";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";
$i=0;
while ($i < $num) {


$Day=mysql_result($result,$i,"Day");
$Date=mysql_result($result,$i,"Date");
$Time1=mysql_result($result,$i,"Time1");
$Player1=mysql_result($result,$i,"Player1");
$Player2=mysql_result($result,$i,"Player2");
$Time2=mysql_result($result,$i,"Time2");
$Player3=mysql_result($result,$i,"Player3");
$Player4=mysql_result($result,$i,"Player4");

echo "<b>Day: $Day</br><br> Date: %Date</b><br>Time1: $Time1<br>Player1: $Player1<br>Player2: $Player2<br>Time2: $Time2<br>Player3: $Player3<br>Player4:$Player4<br><hr><br>";

$i++;
}
?>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Day</font></th>
<th><font face="Arial, Helvetica, sans-serif">Date</font></th>
<th><font face="Arial, Helvetica, sans-serif">Time1</font></th>
<th><font face="Arial, Helvetica, sans-serif">Player1</font></th>
<th><font face="Arial, Helvetica, sans-serif">Player2</font></th>
<th><font face="Arial, Helvetica, sans-serif">Time2</font></th>
<th><font face="Arial, Helvetica, sans-serif">Player3</font></th>
<th><font face="Arial, Helvetica, sans-serif">Player4</font></th>

</tr>
QuantumTiger
Forum Newbie
Posts: 8
Joined: Thu Aug 27, 2009 11:17 am

Re: Displaying content of database

Post by QuantumTiger »

<br> is the HTML for line break, which is causing your data to appear on seperate lines. I think you're really trying to get it into a table. I suggest that you look at the HTML tutorials on W3 Shcools

http://www.w3schools.com/html/default.asp
http://www.w3schools.com/html/html_tables.asp

See also the PHP Manual for mysql_fetch_array for a more efficient way of referencing the data you get back from your SQL Query.

http://uk.php.net/manual/en/function.my ... -array.php

[EDIT:] W3 also have some PHP tutorials for getting data into tables
http://www.w3schools.com/php/php_mysql_select.asp
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Displaying content of database

Post by AbraCadaver »

You need to echo the table start and the th tags first, then loop through and echo a tr and then each of your vars inside td tags. Simple HTML table stuff.

http://www.w3schools.com/html/html_tables.asp
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
dave.hawksworth
Forum Newbie
Posts: 11
Joined: Thu Feb 18, 2010 8:45 am

Re: Displaying content of database

Post by dave.hawksworth »

Thanks for the help.
The following code works OK but I now need to select one of the records in the table to allow the user to add data.
Can anyone point me in the right direction to do this.
Thanks

<?
$username="bnsquash";
$password="can1squash";
$database="bnsquash_courttime";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM courttimes";
$result=mysql_query($query);

$num=mysql_numrows($result);



echo "<b><center>Database Output</center></b><br><br>";



echo "<table border='1'>
<tr>
<th>Day</th>
<th>Date</th>
<th>Time1</th>
<th>Player1</th>
<th>Player2</th>
<th>Time2</th>
<th>Player3</th>
<th>Player4</th>




</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Day'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "<td>" . $row['Time1'] . "</td>";
echo "<td>" . $row['Player1'] . "</td>";
echo "<td>" . $row['Player2'] . "</td>";
echo "<td>" . $row['Time2'] . "</td>";
echo "<td>" . $row['Player3'] . "</td>";
echo "<td>" . $row['Player4'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);
?>
Post Reply