Jump to content

Nested loop help


Ty44ler

Recommended Posts

I'm trying to create a table of all the software we have. This is easy enough as I have them listed in a MySQL database and just loop through them.

 

Now I'd like to also loop through to show the licenses for each software once clicked, but I don't exactly know how to do it.

 

Here was my attempt along with a picture...

$row = mysql_fetch_assoc($result2);

while($row=mysql_fetch_assoc($result2))
{
$newarray[]=$row['software'];
}

foreach ($newarray as $k ) {

					<!-- ##############  TABLES LISTING THE NAMES OF THE SOFTWARE      ###################   -->
						<div id="<? echo $k; ?>" >

								//select the table
								$result3 = mysql_query("select DISTINCT software from licenses WHERE software = '$k'");

								//get field names to display as table headers
								$fields_num = mysql_num_fields($result);

								//count number of records
								$num_rows = mysql_num_rows($result);

			### LICENSE TABLE					
						echo "<table name='licenses' border='1' id='customers' class='sortable'>";

						//grab all the content
						while($r=mysql_fetch_array($result3))
								{
								   //the format is $variable = $r["column"];
								   $username = $r["username"];
								   $license = $r["license"];
								   $software = $r["software"];
								   
								   echo "<th colspan='2'>$k:</th>";

								   //display the table
								   echo "<tr><td>$username</td><td>$license</td></tr>";
								}
}
?> 

 

Software is in its own table. (attached picture to help)

 

Licenses is in its own table. (attached picture to help)

 

Each table has software

 

I attached my output... first one shows the software and second shows what happens when view licenses is clicked.

 

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/196354-nested-loop-help/
Share on other sites

Just use 1 loop with the JOIN keyword. Make both of your tables be php string variables. Like this

 

SELECT * FROM software AS s INNER JOIN licenses AS l ON s.name=l.software

 

Then you would have 2 variables to hold the data so you would put all of ur echo statements from the first loop into 1 variable and all of your echo statements from the second loop in your 2nd variable and then echo both of the variables.

Link to comment
https://forums.phpfreaks.com/topic/196354-nested-loop-help/#findComment-1031016
Share on other sites

Would I only use the while loop or the foreach loop?

 

I kind of follow, but still not exactly sure quite what you mean.

 

You would use the while loop so you can read in each row until you have no more rows to fetch. What the join does is join two tables by a keyword. So you will be able to access all of them

Link to comment
https://forums.phpfreaks.com/topic/196354-nested-loop-help/#findComment-1031042
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.