How do I call a nested foreach with this code?
Posted: Wed Feb 14, 2007 9:32 am
Greetings,
I have some php code that looks like this:
The output results in this:
The question is, How would I go about listing resources with more than one document only once with each document listed below it?
I am assuming I need to either create my array differently or create some type of nested loop. I'm a newbie, however, and can not figure this out with google/o'reilly.
Could somebody help? Thanks.
I have some php code that looks like this:
Code: Select all
mssql_select_db($database, $dbconnect);
$query = "SELECT TOP 10 Res.ResourceID, Res.Title AS Res_Title, Res.ShortBlurb, Res.Created, Cat.Title AS Cat_Title, Doc.Title AS Doc_Title, Doc.Location AS Doc_Location ";
$query .= "FROM DA_Resource Res, DA_Category Cat, DA_Document Doc ";
$query .= "WHERE Res.ResourceID = Doc.ResourceID ";
$query .= "AND Cat.CategoryID = Res.CategoryID ";
$result = mssql_query( $query, $dbconnect );
while ($line = mssql_fetch_assoc($result))
{
$return[] = $line;
}
$output = "<html><body><ul>";
foreach ($return as $line)
{ $output .= "<li>$line[Res_Title]<ul>
<li>Category: $line[Cat_Title]</li>
<li>Date Created: $line[Created]</li>
<li>$line[Doc_Location]</li>
</ul>
</li>";}
$output .="</ul></body></ul>";
echo $output;As you can see, Some Resources (like 'what a life') have more than one file associated with them in the database. When I loop through the array, my current code will repeat the resource name, category, and date created for every document associated with that resource.
- Hope In The Midst Of Suffering
- Category: 1 Peter
- Date Created: May 10 2004 8:50AM
- \Resource_16\Hope_In_The_Midst_Of_Suffering.pdf
- How Can I Give Glory To God?
- Category: 1 Peter
- Date Created: May 10 2004 9:03AM
- \Resource_17\How_Can_I_Give_Glory_To_God.pdf
- How To Smile When You Suffer For Jesus
- Category: 1 Peter
- Date Created: May 10 2004 9:05AM
- \Resource_18\How_To_Smile_When_You_Suffer_For_Jesus.pdf
- What A Life
- Category: 1 John
- Date Created: May 10 2004 9:29AM
- \Resource_20\what a life-outline.pdf
- What A Life
- Category: 1 John
- Date Created: May 10 2004 9:29AM
- \Resource_20\what a life-sermon.pdf
- When Men Call God A Liar
- Category: 1 John
- Date Created: May 10 2004 10:23AM
- \Resource_21\When Men Call God A Liar-sermon.pdf
- When Men Call God A Liar
- Category: 1 John
- Date Created: May 10 2004 10:23AM
- \Resource_21\When Men Call God A Liar-outline.pdf
- When Men Call God A Liar
- Category: 1 John
- Date Created: May 10 2004 10:23AM
- \Resource_21\Akin, Daniel - When Men Call God A Liar.mp3
- Know And Obey
- Category: 1 John
- Date Created: May 10 2004 10:31AM
- \Resource_22\Know And Obey-Sermon.pdf
- Know And Obey
- Category: 1 John
- Date Created: May 10 2004 10:31AM
- \Resource_22\Know And Obey-outline.pdf
The question is, How would I go about listing resources with more than one document only once with each document listed below it?
I am assuming I need to either create my array differently or create some type of nested loop. I'm a newbie, however, and can not figure this out with google/o'reilly.
Could somebody help? Thanks.