Displaying results in table :: Easiest way/how to?

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
sharky
Forum Newbie
Posts: 17
Joined: Sun Jun 04, 2006 12:31 pm

Displaying results in table :: Easiest way/how to?

Post 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
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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>';

?>
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post 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();
}
sharky
Forum Newbie
Posts: 17
Joined: Sun Jun 04, 2006 12:31 pm

Post by sharky »

feyd | Please use

Code: Select all

,

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

,

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]
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

google for php templates, there are a bazillion of them out there.
lettie
Forum Newbie
Posts: 15
Joined: Fri Jan 28, 2005 6:57 am

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

?>
sharky
Forum Newbie
Posts: 17
Joined: Sun Jun 04, 2006 12:31 pm

Post by sharky »

Thanks lettie. It works. That's just a basic stuff I need to build a bigger table with more displays. Thanks alot. :D
Post Reply