Displaying an Array within an Array

Let’s start with a web page that displays brief bios of people drawn from a database, like this:

[B]Muhammad Ali was a world champion boxing champion and one of the most vocal opponents of the war in Vietnam.

Che Guevara was perhaps the most famous Latin American revolutionary of all time.[/B]

To do this, I define the content ($Brief) in a while loop, like this…


$Brief[] = '<div class="Bio"><p><a name="'.$URL.'"></a>'.$row['Brief'].'</p></div>';

Then I put the following on my display page:


echo join( $Brief, '' );

It works fine, but I want to add a line of text below each bio that lists each person’s titles, or classifications. So the display will now look like this:

Muhammad Ali was a world champion boxing champion and one of the most vocal opponents of the war in Vietnam.
African American | activist

Che Guevara was perhaps the most famous Latin American revolutionary of all time.
Latin American | freedom fighter | intellectual

These classification values are stored in a table named people_1_class, which features a separate row for each classification…


URL | Class
Che_Guevara | Latin_American
Che_Guevara | freedom_fighter
Che_Guevara | intellectual

So I create another array…

$Class[] = $row['Class'];

But I don’t know how or where to insert it in my code. Do I change the value of $Brief in my while loop?..


$Brief[] = '<div class="Bio"><p><a name="'.$URL.'"></a>'.$row['Brief'].''.join( $Class, ' | ' ).'</p></div>';

Or do I somehow insert the $Class array on my display page? None of my experiments are working.

This is the entire query…


$res = mysql_query ("SELECT P.URL, P.Title, P.Subtitle, P.MetaTitle, P.MetaDesc, P.KW, P.Site, P.Live,
 PB.Common, PB.First, PB.Middle, PB.Last, PB.Prefix, PB.Suffix, PB.Alpha, PB.Born, PB.Died, PB.Birth_Place, PB.Death_Place, PB.Nationality, PC.URL, PC.Class, Brf.URL, Brf.Site, Brf.Brief, ART.Article PXA
 FROM people P
 LEFT JOIN people_1_bio PB ON PB.URL = P.URL
 LEFT JOIN people_1_class PC ON PC.URL = P.URL
 LEFT JOIN people_briefs Brf ON Brf.URL = P.URL
 LEFT JOIN people_articles_px ART ON ART.URL = P.URL
 WHERE P.Site = 'PX' AND Brf.Site  = 'PX' AND PB.Alpha LIKE '$MyURL%' AND Brf.Brief !='' AND PC.URL = P.URL AND P.Live = 1
 GROUP BY P.URL ORDER BY P.N") or die (mysql_error());

Someone suggested I replace PC.Class with group_concat(PC_Class), but that isn’t working, either.

Thanks for any tips.

shouldn’t


FROM people P
be
FROM people AS P
etc.. for all the rest

Depends on the db engine. Most engines accept both forms.

Okay. A couple of points here:
1.

AND PC.URL = P.URL

(from your WHERE clause) is redundant, as it is already done by the ON clause of your join.

  1. You’ve got an awful lot of joins there - if people only have 1 bio, that should be a field of your people table, not a seperate table. (1-1 matchings)

  2. This is an X*Y query; you’ve got potentially multiple articles and multiple classes. You should consider breaking this up into multiple queries or using grouping concatenation functions to return a single result if you’re not using it for anything other than a single echo.

Thanks for the tips. Yes, it is a complex, confusing query. I figured out how to combine at least two database tables into one. It will take me a while to get my mind wrapped around this project. :wink: