Jump to content

How to display a dynamic table from mySQl result.


wzcocoon

Recommended Posts

I want to display the results from mySQL in an HTML table.I have 2 tables one called "subCategories" and the other called "pages"this is how my tables look likesubCategories:

id	 int(11)catID	 int(11)subCatID	 int(11)subCatABV	 varchar(10)subCatName	 varchar(255)

pages:

pageID	  int(11)catID 	int(11)subCatID 	int(11)menuID 	int(11)pageTitle 	varchar(255)pageKeywords 	textpageDescription 	textpageFlash 	textpageContent 	text

my page is getting the the catID via Querystringwhat I need to optain must look like this:

+---------------+---------------+---------------+-----------------+| subCatName1   | subCatName2   | subCatName3   | subCatName4  |+---------------+---------------+---------------+-----------------+| subCat1 Item1 | subCat2 Item1   | subCat3 Item1 | subCat4 Item1 || subCat1 Item2 | subCat2 Item2   | subCat3 Item2 | subCat4 Item2 || subCat1 Item3 | subCat2 Item3   | subCat3 Item3 | subCat4 Item3 || subCat1 Item4 | subCat2 Item4   | subCat3 Item4 | subCat4 Item4 |+---------------+---------------+---------------+-----------------+

I need to get the subCatName from the subCategories table and the list of items from the pages tablethis is how my PHP codes look like:

<?php// Connect to the databaseinclude("_connect.php");$catID		  = 2;$sqlSub			 = "SELECT * FROM subCategories WHERE catID=$catID";$querySub	   = mysql_query($sqlSub,$connect) or die ("subcategories");  echo "<table border=\"1\" cellpadding=\"10\">";echo "  <tr>";	while ($r1 = mysql_fetch_array($querySub)) {		$subCatName	 = $r1['subCatName'];		$subCatID	   = $r1['subCatID']; // to be used in the second while		$sqlPages	   = "SELECT * FROM pages WHERE catID='$catID' AND subCatID='$subCatID'"; // the subCatID comes from the first while		 $queryPages	 = mysql_query($sqlPages,$connect) or die ("pages");		 echo "  <th>".$subCatName. "</th>";		 echo "  </tr><tr>";		 echo "	  <td>";		 while ($r2 = mysql_fetch_array($queryPages)){		 echo "		  <li><a href=\"pages.php?pageID=".$r2['pageID']."\">" $r2['pageTitle']. "</a></li>";		 }	 echo "	  </td>";	 }echo "  </tr>";echo "</table>";?>

I hope all this will make some sens to you because it does not to me...help

Link to comment
Share on other sites

From what i understand, You are trying to get the value of the SubCategory name from the table, and all pages from the pages table that match that subcategory. How i would do that would be something along the lines of this SQL Query:

 SELECT sc.subCatName,p.* FROM pages p, subCategories sc WHERE sc.id = p.subCatID;

I'm not sure what a GROUP BY statement would do, or if that would be what you want per se. That should work. Every row of the Query result will now have the subCatName value from the other table. This will allow you to group them in PHP and display them like that.Regards

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...