Table of contents with book subchapters. How to fetch data from Array

Hi,
I’m building a database for books to represent a Table of Contents like this:

[ol][li]A Study in Scarlet (ID=1)[/li]
[li]The Adventures of Sherlock Holmes (ID=2)
[list type=decimal]
[li]A Scandal in Bohemia (ID=3)[/li]
[li]The Final Problem (ID=4)[/li]
[/list]
[/li]
[li]The Hound of the Baskervilles (ID=5)[/li][/ol]

For this, I created a table matching BOOKS and WORKS (the adventures or collections in this case), which looks like this (I omit the Book_ID field, as we’ll refer to an only book)
Parent_ID | Order | Work_ID
0 | 1 | 1
0 | 2 | 2
2 | 1 | 3
2 | 2 | 4
0 | 3 | 5

The problem is that I cannot ORDER BY Parent_ID, Order ASC, because the 5th element would appear before the 3rd and 4th ones (which as you see, it’s not true). Nor I can order it by ID because these are examples.

I’ve imagined a solution but I don’t know how to implement it:

[php]foreach ($results as $item)
{
// Select the ones with Parent = 0
if ($item[‘parent_ID’] = 0)
{
echo $item[‘ID’]; // And show the rest of information about this chapter…

    // Now show subchapters which 'parent_ID' = $item['ID'], ¿¿¿with a while, with other foreach??? 
    // And so on (for each one, check if there's any whose Parent_ID matches current ID...)
} 

} [/php]

What do you think? Do you know how to fetch specific data from the Array???

Thanks in advance :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service