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
5
u/greg8872 Dec 05 '24 edited Dec 05 '24
Here is an example of the code with the logic separated, also SQL cleaned up and easier to read using aliases on table names, and a helper function to make sure any data being output is properly converted for use inside HTML.
Note, it is still HIGHLY recommended to switch from
mysql_*functions over tomysqli_*functions or usePDO, both of which allow prepared statements and placeholders for data, but I didn't go that far:As you can see, all your logic is in one place, you can uncomment 3 debugging lines of code to verify you have all teh data you were expecting, without the mess of HTML code all around it (and if you did have errors/warning output, it would be at the top of the page, more noticable), and then when you get to the output, how clean and easy it is to ready without a buch of logic code in the midde.
PS. Also note the loading up data here is designed for where data is unique to it's parrent (ie, Format data from one release will not also be used in another release, and track data from one format will be used only with that format.) If you have data that can be shared amoung parents, I would load up each layer in its own array, mapped by ID's, so if 50 Releases have the same "format", you are not loading that format 50 times. (though if this the case, you'd want to store your data differently)