r/PHPhelp • u/TheBigBlackMachine • Dec 05 '24
PHP5 - Issue Nesting Tables
Novice PHP5 coder here looking for advice. I'm sure all you smart people will solve this in 30 seconds.
I am building a music database. I have a page listing all the albums working. Inside that table I then added the following code to nest the formats available. I also have a separate query called releaseformatsdata.
<table>
<?php $query = mysql_query(sprintf("
SELECT releases.ReleaseID, releaseformats.ReleaseFormatID
FROM releases, releaseformats
WHERE releases.ReleaseID = %s
AND releases.ReleaseID = releaseformats.ReleaseIDLINK
", $row_releasedata['ReleaseID']), $database); ?>
<?php while ($row_releaseformatsdata = mysql_fetch_assoc($query)): ?>
<tr>
<td>
<?php $TFM_nest = $row_releaseformatsdata['ReleaseID']; ?>
<p>ReleaseFormatID: <?php echo $row_releaseformatsdata['ReleaseFormatID']; ?></p>
</td>
</tr>
<?php endwhile; ?>
</table>
This produces a list like this -
Release 1
- Format 1
- Format 2
Release 2
- Format 3
- Format 4
I then tried to add songs using by putting another table in the table. I have a separate query called releaseformattrackssdata.
<table>
<?php $query = mysql_query(sprintf("
SELECT releases.ReleaseID, releaseformats.ReleaseFormatID
FROM releases, releaseformats
WHERE releases.ReleaseID = %s
AND releases.ReleaseID = releaseformats.ReleaseIDLINK
", $row_releasedata['ReleaseID']), $database); ?>
<?php while ($row_releaseformatsdata = mysql_fetch_assoc($query)): ?>
<tr>
<td>
<?php $TFM_nest = $row_releaseformatsdata['ReleaseID']; ?>
<p>ReleaseFormatID: <?php echo $row_releaseformatsdata['ReleaseFormatID']; ?></p>
<table>
<?php $query = mysql_query(sprintf("
SELECT releaseformats.ReleaseFormatID, releaseformattracks.ReleaseFormatTrackID
FROM releaseformats, releaseformattracks
WHERE releaseformats.ReleaseFormatID = %s
AND releaseformats.ReleaseFormatID = releaseformattracks.ReleaseFormatIDLINK
", $row_releaseformatsdata['ReleaseFormatID']), $database); ?>
<?php while ($row_releaseformattrackssdata = mysql_fetch_assoc($query)): ?>
<tr>
<td>
<?php $TFM_nest = $row_releaseformattrackssdata['ReleaseFormatID']; ?>
<p>ReleaseFormatTrackID: <?php echo $row_releaseformattrackssdata['ReleaseFormatTrackID']; ?></p>
</td>
</tr>
<?php endwhile; ?>
</table>
</td>
</tr>
<?php endwhile; ?>
</table>
What I hoped to see was -
Release 1
- Format 1
--Track 1
--Track 2
- Format 2
-- Track 3
-- Track 4
Release 2
- Format 3
-- Track 5
-- Track 6
- Format 4
-- Track 7
-- Track 8
But instead I only get this -
Release 1
- Format 1
--Track 1
--Track 2
Release 2
- Format 3
-- Track 5
-- Track 6
Any help would be really appreciated!
View all comments
Show parent comment
1
u/greg8872 Dec 05 '24
For your actual error, use a different variable name for the inside loop. (also note, not sure what you are using $TFM_nest for, but you will have the same issue, when you get back to your formats loop, it will contain the last value it was assigned during the tracks loop. Hard to advise here without seeing all the code.
instead of using $query twice, do $formatsQuery and $tracksQuery.