Page 1 of 1

Displaying content of database

Posted: Thu Feb 18, 2010 9:11 am
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>

Re: Displaying content of database

Posted: Thu Feb 18, 2010 10:55 am
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

Re: Displaying content of database

Posted: Thu Feb 18, 2010 11:17 am
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

Re: Displaying content of database

Posted: Fri Feb 19, 2010 9:42 am
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);
?>