Help with PHP/MySQL and Tables, my mind has gone blank...

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
influenceuk
Forum Commoner
Posts: 42
Joined: Tue May 08, 2007 7:48 am
Location: London, UK

Help with PHP/MySQL and Tables, my mind has gone blank...

Post 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.
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Help with PHP/MySQL and Tables, my mind has gone blank...

Post 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
}
?>
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Help with PHP/MySQL and Tables, my mind has gone blank...

Post 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>
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Help with PHP/MySQL and Tables, my mind has gone blank...

Post 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
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Help with PHP/MySQL and Tables, my mind has gone blank...

Post 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.
influenceuk
Forum Commoner
Posts: 42
Joined: Tue May 08, 2007 7:48 am
Location: London, UK

Re: Help with PHP/MySQL and Tables, my mind has gone blank...

Post 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 :roll:
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Re: Help with PHP/MySQL and Tables, my mind has gone blank...

Post 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.
:D
influenceuk
Forum Commoner
Posts: 42
Joined: Tue May 08, 2007 7:48 am
Location: London, UK

Re: Help with PHP/MySQL and Tables, my mind has gone blank...

Post by influenceuk »

So how are you suggesting i style the table? in pure HTML?
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Re: Help with PHP/MySQL and Tables, my mind has gone blank...

Post by Phoenixheart »

It's totally OK and recommended if you style your tables using CSS, like you did in your previous example.
influenceuk
Forum Commoner
Posts: 42
Joined: Tue May 08, 2007 7:48 am
Location: London, UK

Re: Help with PHP/MySQL and Tables, my mind has gone blank...

Post by influenceuk »

cool,
but how about the code to use that code to show the listings :)

Thanks in advance.
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Help with PHP/MySQL and Tables, my mind has gone blank...

Post 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.
Post Reply