Page 1 of 1
Displaying results in table :: Easiest way/how to?
Posted: Tue Jun 13, 2006 2:25 pm
by sharky
I'm new to php/mysql. I'm going to display results from my database. I do not know how to get them displayed in a table, in 1 table and fetch all the results to display in the next table. I'm looking for the easiest way to display them. Does anybody know where I can look for them? Thanks
Posted: Tue Jun 13, 2006 2:51 pm
by jayshields
Suprisingly, Google couldn't find me any half decent ones.
So I just wrote this now - tested.
Code: Select all
<?php
mysql_connect('xxx', 'xxx', 'xxx');
mysql_select_db('xxx');
$query = "SELECT * FROM `xxx`";
$result = mysql_query($query) or die(mysql_error());
echo '<table border="1">';
$first = TRUE;
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr>';
foreach($row as $key => $value) {
if($first == TRUE) echo '<td><strong>' . $key . '</strong></td>';
else echo '<td>' . $value . '</td>';
}
$first = FALSE;
echo '</tr>';
}
echo '</table>';
?>
EDIT
-------
Wow, this is harder than I thought. My example skips the first row of data to print the header row, I've been looking at it for 10 minutes now trying to fix it and I can't!
I'm going out now, someone fix this and enlighten me!
EDIT 2
----------
OK, it's working now, but it's bloated. Someone make a better version!
Code: Select all
<?php
mysql_connect('xxx', 'xxx', 'xxx');
mysql_select_db('xxx');
$query = "SELECT * FROM `xxx`";
$result = mysql_query($query) or die(mysql_error());
echo '<table border="1">';
$first = TRUE;
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if($first == TRUE) {
echo '<tr>';
foreach($row as $key => $value) {
echo '<td><strong>' . $key . '</strong></td>';
}
echo '</tr>';
}
echo '<tr>';
foreach($row as $key => $value) {
echo '<td>' . $value . '</td>';
}
$first = FALSE;
echo '</tr>';
}
echo '</table>';
?>
Posted: Tue Jun 13, 2006 8:23 pm
by sweatje
Here was a function I put together a while back to do a simple dump of an array of row hashes as a table.
Code: Select all
/**
* convert an array of associative arrays to a simple HTML table
* @param $a array the array to convert
* @return string the table
*/
function array_to_table($a) {
ob_start();
if (is_array($a)
&& is_array($a[min(array_keys($a))])) {
echo '<table border="1"><tr>';
foreach (array_keys($a[min(array_keys($a))]) as $field) {
echo '<th>'.htmlspecialchars($field).'</th>';
}
echo '</tr>';
foreach ($a as $row) {
echo '<tr>';
foreach ($row as $value) {
echo '<td>'.htmlspecialchars($value).'</td>';
}
echo '</tr>';
}
echo '</table>';
}
return ob_get_clean();
}
Posted: Wed Jun 14, 2006 9:35 am
by sharky
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I was thinking to build the table using the html/dreamweaver first. Then insert the php code before the table. This is how it will look like :: (well, not the code tho )
Code: Select all
<body>
<?php
$host="localhost";
$username="root";
$password="";
$db_name="test";
$tbl_name="cars";
// Connect to server and select databse
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM cars";
$result=mysql_query($sql);
?>
<?php
//--------------- php code starts here to insert the value from database----------
<table width="485" border="1">
<tr>
<td width="105" height="119">[display 'photo' from cars]</td>
<td width="247"><table width="280" border="1">
<tr>
<td width="321">[display 'brand' from cars]</td>
</tr>
<tr>
<td>[display 'model' from cars]</td>
</tr>
<tr>
<td>[display 'ID' from cars]</td>
</tr>
<tr>
<td>[display 'year' from cars]</td>
</tr>
<tr>
<td>[display 'engine' from cars]</td>
</tr>
</table></td>
</tr>
</table>
//---------------------- it will generate the same table for the next item/car-------------
?>
<?php
mysql_close();
?>
Can somebody help? Thanks
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Wed Jun 14, 2006 9:50 am
by sweatje
google for php templates, there are a bazillion of them out there.
Posted: Wed Jun 14, 2006 10:15 am
by lettie
Try this in place of the php code starts here bit. It will generate a new table for each car in the db. I usually use MYSQL_NUM in place of MYSQL_ASSOC as it cuts down the code but its personal preference. The row names should be the same as your database table names.
Code: Select all
<?php
// - php code starts here to insert the value from database -
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<table width="485" border="1">
<tr>
<td width="105" height="119"> <?php echo $row['photo']; ?> </td>
<td width="247"><table width="280" border="1">
<tr>
<td width="321"> <?php echo $row['brand']; ?> </td>
</tr>
<tr>
<td> <?php echo $row['model']; ?> </td>
</tr>
<tr>
<td> <?php echo $row['ID']; ?> </td>
</tr>
<tr>
<td> <?php echo $row['year']; ?> </td>
</tr>
<tr>
<td> <?php echo $row['engine']; ?> </td>
</tr>
</table></td>
</tr>
</table>
<?php
} // end of while loop
//---------------------- it will generate the same table for the next item/car-------------
?>
Posted: Wed Jun 14, 2006 11:52 am
by sharky
Thanks lettie. It works. That's just a basic stuff I need to build a bigger table with more displays. Thanks alot.
