...

View Full Version : Formatting PHP output in HTML table



bumbar
04-05-2007, 12:02 PM
Hallo!

I have two tables in database "Category" and "SubCategory".
I need some formatting output in HTML table.

Here I show design on MySQL tables:



CREATE TABLE `category` (
`id` int(4) NOT NULL auto_increment,
`name` text NOT NULL,
PRIMARY KEY (`id`)
);

INSERT INTO `category` (`id`, `name`) VALUES
(1, 'Web Programming'),
(2, 'Database');

CREATE TABLE `subcategory` (
`id` int(4) NOT NULL auto_increment,
`cat_id` int(4) NOT NULL default '0',
`name` text NOT NULL,
`description` text NOT NULL,
PRIMARY KEY (`id`)
) ;

INSERT INTO `subcategory` (`id`, `cat_id`, `name`, `description`) VALUES
(1, 1, 'PHP', 'Personal Home Page'),
(2, 1, 'JavaScript', 'Client-Side Java Script Programming'),
(3, 2, 'MySQL', 'Structured Query Language'),
(4, 2, 'PostgreSQL', 'Beauty SQL'),
(5, 2, 'Microsoft SQL', 'ghjghjgh ghghjghj');


I have use this PHP code to display output:



$select = "SELECT id, name FROM category";
$query = mysql_query ("$select") or die (mysql_error());

echo "<table border=1>";

while ($row = mysql_fetch_array ($query)) {

$select_sub = "SELECT name FROM subcategory WHERE cat_id=$row[0]";
$query_sub = mysql_query ("$select_sub") or die (mysql_error());

echo "<td>" . "<b>" . $row['name']. "</b>" . "</td>";

echo "<tr><td bgcolor=\"#CCFF99\">";

while ($row_sub = mysql_fetch_array ($query_sub))
{
echo "<u>" . $row_sub['name'] . "</u>" . "&nbsp;&nbsp;";
}

echo "</tr></td>";
}
echo "</table>";


But this output in 1 column

Web Programming
PHP JavaScript CSS VB
Database
MySQL PostgreSQL Microsoft SQL

But I want get output in row like:

Web Programming-----Database
PHP JavaScript CSS VB----MySQL PostgreSQL Microsoft SQL

Any suggestion?
Thanks!

rafiki
04-05-2007, 02:06 PM
dont insert the id into the database table mysql will do that for you
also i seen

(1, 1, 'PHP', 'Personal Home Page'),
PHP stands for
Hypertext Pre Processor

iLLin
04-05-2007, 04:07 PM
while ($row = mysql_fetch_array ($query)) {
//store id in array
$id_array[] = $row[0];

echo "<td>" . "<b>" . $row['name']. "</b>" . "</td>";
}
echo "</tr><tr>";
foreach($id_array as $id) {
$select_sub = "SELECT name FROM subcategory WHERE cat_id=$id";
$query_sub = mysql_query ("$select_sub") or die (mysql_error());

echo "<td bgcolor=\"#CCFF99\">";

while ($row_sub = mysql_fetch_array ($query_sub))
{
echo "<u>" . $row_sub['name'] . "</u>" . "&nbsp;&nbsp;";
}

echo "</td>";
}
echo "</tr></table>";


This is one way although might not be proficient if you have a ton of values.

aedrin
04-05-2007, 04:51 PM
PHP stands for
Hypertext Pre Processor

Actually, that would make HPP.

http://www.php.net/manual/en/faq.general.php#faq.general.acronym

aedrin
04-05-2007, 04:59 PM
The code posted is already horribly inefficient. A query for each category?


SELECT * FROM category c, subcategory sc WHERE sc.cat_id = c.id ORDER BY c.id ASC

Now you'll get rows like this:



1 Web Programming 1 1 PHP Personal Message
...


Loop through these rows. Everytime the cat_id changes, echo out the header.

This will get you what you had in a more efficient manner.

Your example is a little confusing, so I'm not sure what you are trying to achieve.



<table>
<tr>
<?php
$row_count = 0;
$columns = 3;
while ($row = mysql_fetch_assoc($result)) {
if ($row_count == $columns) {
echo "</tr><tr>";
$row_count = 0;
}
echo "<td> ....... </td>";
$row_count++;
}
?>
</tr>
</table>

rafiki
04-05-2007, 06:36 PM
Actually, that would make HPP.

http://www.php.net/manual/en/faq.general.php#faq.general.acronym

Hypertext Preprocessor
see..

aedrin
04-05-2007, 06:37 PM
You didn't include the PHP part. And your capitalization suggested you meant Pre to be a seperate part. ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum