Tree generation using php

hi all

I have 3 tables for generating tree.I need to display first and third table datas.
table1

Course


id name


1 A
2 B

table2

Certificates


id course_id certficate_name


1 1 abc
2 1 def
3 2 ghi

table3

CourseModules


id certificate_id modulename


1 3 hello
2 3 hello1
3 3 hello2
4 2 Hai
4 1 welcome

Each course has many certificates and each certificates have many modules.

when a user selecting a course (from tree) ,all modules which is related to that particular course should be displayed .

How can I do this in php? please help me

It’s more about mysql than php. I’m gonna guess (from your sample) that your model is wrong: what you have is backwards.

The smallest unit there is a module; certificates then have a number of modules, and courses a number of certificates. So for the above three tables, get rid of everything but id and name, and then create some relationships between them.

I think this can be done in a single query using joins.

It will go something like

SELECT * FROM course LEFT JOIN certificates ON id=course_id LEFT JOIN course_modules ON certificates.id=coursemodules.certificate_id

then the long query result can be conditionally displayed in a list.

I will probably get pulled up on my poor query writing but it goes something like that.

Hope that helps

Keith

Thanks,

my concern is how can I take a particular courss modules,when I am clicking on a tree.Actually I need to display entire courses on the page as a tree, while I am clicking on the course all the modules which is related to that should be displayed.I have written a query but in php when I am taking everything displayed as a separate array.

I have done for two tables.please check the following code.But no idea how to implement for three tables using my situation.

Please have look into my two table implementation.

<?
include(“db.php”);
?>
<html>
<head>
<script language=“javascript”>
function showHide(cid){
var img=document.getElementById(‘catimage’+cid);
var p=document.getElementById(‘p’+cid);
if(p.style.display==‘none’){
p.style.display=‘’;
img.src=‘images/-.gif’;
}
else{
p.style.display=‘none’;
img.src=‘images/+.gif’;
}
}
</script>
<style>
.categoryText{
font-family:tahoma;
font-weight:bold;
font-size:12px;
color:#006699;
}
.productText{
font-family:tahoma;
font-size:12px;
color:#0099CC;
}
</style>
</head>

<body>
<table border=“0”>
<?
$category_result=mysql_query(“select * from categories”);
while($category_row=mysql_fetch_array($category_result)){
$cid=$category_row[‘cid’];
?>
<tr>
<td>
<a href=“#” onclick=“showHide(<?=$cid?>)” class=“categoryText”><img src=“images/+.gif” id=“catimage<?=$cid?>” border=“0” /> <?=$category_row[‘cname’]?></a>
</td>
</tr>
<tr>
<td style=“padding-left:10px”>
<table border=“0” style=“display:none” id=“p<?=$cid?>” cellpadding=“0” cellspacing=“0”>
<?

						$product_result=mysql_query("select * from products where cid=$cid");
						while($product_row=mysql_fetch_array($product_result)){
					?&gt;
					&lt;tr&gt;
							&lt;td class="productText"&gt;&lt;img src="images/link.gif" /&gt;&lt;?=$product_row['pname']?&gt;&lt;/td&gt;
					&lt;/tr&gt;
					&lt;? } ?&gt;
				&lt;/table&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
&lt;? } ?&gt;

</body>
</html>