Jump to content

Please help. Display data in tables

- - - - -

  • Please log in to reply
No replies to this topic

#1
boneysin

boneysin

    Newbie

  • Members
  • Pip
  • 1 posts
Hi, im a new developer. i have written my code in this post. Please help.

I want to display the data in a table with rows of 3 columns each.
Each column has the Name of the parent category in bold and the child categories should be mentioned right below the parent cat. All this should appear in the single column. The next parent cat and child cat should be displayed in another column. And so on..

im able to display the content but all in a straight one column. [Please view the output of the code mentioned below]

Please help. Thanks in advance.


Following is the sample code : 


//To create a table : 


<?php 

$con = mysql_connect("localhost", "root", "") 

or die (mysql_error()); 

mysql_select_db("mydb"); 

$table = "CREATE TABLE categories12 (`id_cat` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(255) NOT NULL, `id_catparent` INT NOT NULL, `friendlyname` VARCHAR(255) NOT NULL, `description` TEXT NOT NULL) ENGINE = MyISAM;"; 

$results = mysql_query($table) 

or die (mysql_error()); 

echo "<br>Successfully created the table!<br><br>"; 




//Data to be inserted in table : 


$insert = "INSERT INTO categories (id_cat, name, id_catparent, friendlyname, description) 

VALUES (1, 'Languages', 0, 'Languages', 'Sample description for Language Category'), 

(2,'People', 0, 'People', 'Sample description for People Category'), 

(3,'Games', 0, 'Games', 'Sample description for Games Category'), 

(4,'Animals', 0, 'Animals', 'Sample description for Animals Category'), 

(5,'Tree', 0, 'Tree', 'Sample description for Tree Category'), 

(6,'Planets', 0, 'History', 'Sample description for History Category'), 

(7,'Country', 0, 'Country', 'Sample description for Country Category'), 

(8,'Fruits', 0, 'Fruits', 'Sample description for Fruits Category'), 

(9,'Cocktails', 0, 'Cocktails', 'Sample description for Cocktails Category'), 

(10,'English', 1, 'English', 'Sample description for English Category'), 

(11,'French', 1, 'French', 'Sample description for French Category'), 

(12,'German', 1, 'German', 'Sample description for German Category'), 

(13,'Japanese', 1, 'Japanese', 'Sample description for Japanese Category'), 

(14,'Alexander', 2, 'People', 'Sample description for People Category'), 

(15,'Napolean', 2, 'Napolean', 'Sample description for Napolean Category'), 

(16,'Gandhi', 2, 'Gandhi', 'Sample description for Gandhi Category'), 

(17,'Churchill', 2, 'Churchill', 'Sample description for Churchill Category'), 

(18,'Shivaji', 2, 'Shivaji', 'Sample description for Category'), 

(19,'Steve Jobs', 2, 'Steve Jobs', 'Sample description for Steve Jobs Category'), 

(20,'Bill Clinton', 2, 'Bill Clinton', 'Sample description for Bill Clinton Category'), 

(21,'Robert DiNero', 2, 'Robert DiNero', 'Sample description for Robert DiNero Category'), 

(22,'Football', 3, 'Football', 'Sample description for Football Category'), 

(23,'Volleyball', 3, 'Volleyball', 'Sample description for Volleyball Category'), 

(24,'Basketball', 3, 'Basketball', 'Sample description for Basketball Category'), 

(25,'Chess', 3, 'Chess', 'Sample description for Chess Category'), 

(100,'Hockey', 3, 'Hockey', 'Sample description for Hockey Category'), 

(26,'Lion', 4, 'Lion', 'Sample description for Lion Category'), 

(27,'Tiger', 4, 'Tiger', 'Sample description for Tiger Category'), 

(28,'Beer', 4, 'Beer', 'Sample description for Beer Category'), 

(29,'Deer', 4, 'Deer', 'Sample description for Deer Category'), 

(30,'Rabbit', 4, 'Rabbit', 'Sample description for Rabbit Category'), 

(31,'Kangaroo', 4, 'Kangaroo', 'Sample description for Kangaroo Category'), 

(32,'Elephant', 4, 'Elephant', 'Sample description for Elephant Category'), 

(33,'Mouse', 4, 'Mouse', 'Sample description for Mouse Category'), 

(34,'Dog', 4, 'Dog', 'Sample description for Dog Category'), 

(35,'Cheetah', 4, 'Cheetah', 'Sample description for Cheetah Category'), 

(36,'Root', 5, 'Root', 'Sample description for Root Category'), 

(37,'Branches', 5, 'Branches', 'Sample description for Branches Category'), 

(38,'Leaves', 5, 'Leaves', 'Sample description for Leaves Category'), 

(39,'Seeds', 5, 'Seeds', 'Sample description for Seeds Category'), 

(40,'Mercury', 6, 'Mercury', 'Sample description for Mercury Category'), 

(41,'Venus', 6, 'Venus', 'Sample description for Venus Category'), 

(42,'Earth', 6, 'Earth', 'Sample description for Earth Category'), 

(43,'Mars', 6, 'Mars', 'Sample description for Mars Category'), 

(44,'Saturn', 6, 'Saturn', 'Sample description for Saturn Category'), 

(45,'America', 7, 'America', 'Sample description for America Category'), 

(46,'Germany', 7, 'Germany', 'Sample description for Germany Category'), 

(47,'Finland', 7, 'Finland', 'Sample description for Finland Category'), 

(48,'Russia', 7, 'Russia', 'Sample description for Russia Category'), 

(49,'India', 7, 'India', 'Sample description for India Category'), 

(50,'China', 7, 'China', 'Sample description for China Category'), 

(51,'Apple', 8, 'Apple', 'Sample description for Apple Category'), 

(52,'Mango', 8, 'Mango', 'Sample description for Mango Category'), 

(53,'Guava', 8, 'Guava', 'Sample description for Guava Category'), 

(54,'Grapes', 8, 'Grapes', 'Sample description for Grapes Category'), 

(55,'Kate Winslet', 9, 'Kate Winslet', 'Sample description for Kate Winslet Category'), 

(56,'Drew Barrymore', 9, 'Drew Barrymore', 'Sample description for Drew Barrymore Category'), 

(57,'Thandie Newton', 9, 'Thandie Newton', 'Sample description for Thandie Newton Category'), 

(58,'Julia Roberts', 9, 'Julia Roberts', 'Sample description for Julia Roberts Category'), 

(59,'Anna Farris', 9, 'Anna Farris', 'Sample description for Anna Farris Category'), 

(60,'Madonna', 9, 'Madonna', 'Sample description for Madonna Category'), 

(61,'Christina', 9, 'Christina', 'Sample description for Christina Category'), 

(62,'Britney', 9, 'Britney', 'Sample description for Britney Category');"; 


$result = mysql_query($insert) 

or die (mysql_error()); 


echo "<br>Successfully inserted the values in table : categories !"; 


?> 


// IM trying the following code in order to see the data in a table: 


<?php 

$con = mysql_connect("localhost", "root", "") 

or die("Unable to connect to Database!"); 


mysql_select_db("mydb") 

or die("Unable to select the database"); 


$limit = 3; 

$count = 0; 


$query_cat_0 = "SELECT id_cat, friendlyname FROM categories WHERE id_catparent = '0' ORDER BY id_catparent ASC"; 

$result_cat_0 = mysql_query($query_cat_0) or die("Error retrieving cat0: ". mysql_error()); 


echo "<table border='1'>"; 


while($row_cat_0 = mysql_fetch_assoc($result_cat_0)) 

{ 

echo "<tr>"; 

echo "<td height=75>"; 

echo $row_cat_0["friendlyname"] . "<br />\n"; 


//Selecting all childs of the parent 

$query_cat_1 = "SELECT id_cat, friendlyname FROM categories WHERE id_catparent = '".$row_cat_0["id_cat"]."' ORDER BY friendlyname ASC"; 

$result_cat_1 = mysql_query($query_cat_1) or die("Error retrieving cat1: ". mysql_error()); 


while($row_cat_1 = mysql_fetch_assoc($result_cat_1)) 

{ 

echo $row_cat_1["friendlyname"]."<br />\n"; 

} 

echo "<br />\n<br />\n"; 

} 

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


?>

Edited by Orjan, 24 October 2010 - 11:54 PM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users