Page 1 of 1
Help with PHP/MySQL and Tables, my mind has gone blank...
Posted: Tue Jul 29, 2008 7:25 pm
by influenceuk
Hi there, i have not coded with PHP and MySQL for over a year now, and have come to find my mind has gone blank!
Please could someone help me out as i just cant seem to find a decent resource online to help.
Basically i want to connect to my MySQL DB, which is easy enough. However its displaying the data that's got me stumped!
Basically i want to display the data in a table like so...
Column 1 Column 2 Column 3 Column 4
Row 1
Row 2
Row 3
And so on. Its really just a basic table from the data off the MySQL DB, but i can't work it out

Obviously i want the SQL data to fill the rows itself Please help!!
Thanks in advance.
Re: Help with PHP/MySQL and Tables, my mind has gone blank...
Posted: Tue Jul 29, 2008 7:46 pm
by Chalks
Code: Select all
<?php //rough pseudo code
//assumed already connected
$sql = "SELECT * FROM tablename";
$result = mysql_query($sql) or die(mysql_error());
while($data = mysql_fetch_assoc($result))
{
echo "html formatting goes here" . $data['column1'] . "html formatting goes here\n";
echo "html formatting goes here" . $data['column2'] . "html formatting goes here\n";
echo "html formatting goes here" . $data['column3'] . "html formatting goes here\n"; //'\n' to keep view source looking nice
}
?>
Re: Help with PHP/MySQL and Tables, my mind has gone blank...
Posted: Tue Jul 29, 2008 7:48 pm
by allspiritseve
Something like this, maybe? (assuming you have an array of data)
Code: Select all
<table>
<tr>
<td>Column1</td>
<td>Column2</td>
<td>Column3</td>
</tr>
<? foreach ($array as $row): ?>
<tr>
<td><?=$row['column1']; ?></td>
<td><?=$row['column2']; ?></td>
<td><?=$row['column3']; ?></td>
</tr>
<? endforeach; ?>
</table>
Re: Help with PHP/MySQL and Tables, my mind has gone blank...
Posted: Tue Jul 29, 2008 7:55 pm
by Chalks
sort of. Don't use short tags, use <?php ?> instead. Like so:
Code: Select all
<table>
<tr>
<td>Column1</td>
<td>Column2</td>
<td>Column3</td>
</tr>
<?php
foreach($array as $row)
{
echo "<tr>
<td>" . $row['column1'] . "</td>
<td>" . $row['column2'] . "</td>
<td>" . $row['column3'] . "</td>
</tr>";
}
?>
</table>
I also recommend moving away from table based layout and using CSS instead. It's very easy to learn. The tutorial that got me started is from
w3schools.com
Re: Help with PHP/MySQL and Tables, my mind has gone blank...
Posted: Tue Jul 29, 2008 10:38 pm
by allspiritseve
Chalks wrote:I also recommend moving away from table based layout and using CSS instead. It's very easy to learn. The tutorial that got me started is from
w3schools.com
For layouts, sure, I'd agree with you... however, the OP wanted to display data from a database table, which is tabular in format. That's what html tables were meant for.
I also prefer to start with HTML, and break out for php when needed... its so much cleaner and easier to read.
Re: Help with PHP/MySQL and Tables, my mind has gone blank...
Posted: Wed Jul 30, 2008 2:45 am
by influenceuk
I was thinking of using CSS to style the table as i have am idea of how i want it to look.
How would i get it so the table displays the data like the following layout...
Code: Select all
<div><table width='100%' border='0' cellpadding='0' cellspacing='0' class='tpl_opentable'><tr><td>
<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>
<thead>
<tr>
<td width='40%'>Title</td>
<td width='30%'>Studio</td>
<td width='15%'>Release Date US</td>
<td width='15%'>Release Date UK</td>
</thead>
<tbody>
<tr>
<td class='std_clean'><font color=''><b>Product Name</b></font></td>
<td class='std_clean' nowrap>Studio A</td>
<td class='std_clean' nowrap>22-06-08</td>
<td class='std_clean' nowrap>22-06-08</td>
<tr>
<td class='std_hlight'><font color=''><b>Product Name</b></font></td>
<td class='std_hlight' nowrap>Studio A</td>
<td class='std_hlight' nowrap>22-06-08</td>
<td class='std_hlight' nowrap>22-06-08</td>
</tbody>
</table></td></table>
</div>
Basically I have 2 classes to alternate the row colour. And the other 2 style the header and main table.
Sorry if it seems i am trying to get you to write the code, i am not intentionally doing so, i really do mean it when i say my mind has gone blank lol

Re: Help with PHP/MySQL and Tables, my mind has gone blank...
Posted: Wed Jul 30, 2008 3:26 am
by Phoenixheart
influenceuk wrote:I was thinking of using CSS to style the table as i have am idea of how i want it to look.
No, as for tabular data, HTML tables are the right choice.
In the past, everything is table, which is incorrect. Layout should be CSS.
Now, everything tends to be pure CSS, which is bad also. Tabular data should use tables.

Re: Help with PHP/MySQL and Tables, my mind has gone blank...
Posted: Wed Jul 30, 2008 4:14 am
by influenceuk
So how are you suggesting i style the table? in pure HTML?
Re: Help with PHP/MySQL and Tables, my mind has gone blank...
Posted: Wed Jul 30, 2008 4:16 am
by Phoenixheart
It's totally OK and recommended if you style your tables using CSS, like you did in your previous example.
Re: Help with PHP/MySQL and Tables, my mind has gone blank...
Posted: Wed Jul 30, 2008 5:09 am
by influenceuk
cool,
but how about the code to use that code to show the listings
Thanks in advance.
Re: Help with PHP/MySQL and Tables, my mind has gone blank...
Posted: Wed Jul 30, 2008 8:50 am
by allspiritseve
influenceuk wrote:Basically I have 2 classes to alternate the row colour. And the other 2 style the header and main table.
Can I ask why you have two tables? The outer one doesn't appear to be doing much. It would make things much simpler, if you needed to style something outside the table, to wrap it in a <div>. You can style <tr> blocks, like <tr class="std_clean">, so you don't have to write it for every row. A way I use for alternate highlighting:
Code: Select all
<? foreach ($array as $key => $value): ?>
<tr class="<?=$key%2?'std_clean':'std_hlight'; ?>">
<td>
// ...
</tr>
<? endforeach; ?>
Where $array is 1 or more rows, $key is the result row number, and $value is the row data.