Jump to content


This topic is now archived and is closed to further replies.


Inner Joins, Arrays, For Each Loops Help

Recommended Posts

Hi, I am pretty new at PHP and need some help. I've been searching on Google for a while now and came across this site a few times in the results, but I couldn't find the exact answer I was looking for - so I decided to register and post the question directly here.

Here goes:

I have two tables: resourceLib and resourceLibLinks

In resourceLib, the columns are CAT, CATDESCRIPT, GENDESCRIPT and ORDERROW

In resourceLibLinks, the columns are CAT, LINK, ORDERROW

What I want to display to the end user are results like this:


LINK1 for CAT1
LINK2 for CAT1
LINK3 for CAT1
LINK4 for CAT1


LINK1 for CAT2
LINK2 for CAT2
LINK3 for CAT2
LINK4 for CAT2

Here is the code I have so far:

[code]$query  = "SELECT * FROM resourceLib INNER JOIN resourceLibLinks WHERE resourceLib.CAT=resourceLibLinks.CAT  ORDER BY resourceLib.ORDERROW";
$result = mysql_query($query) or die(mysql_error());[/code]

And then I have this to display the results, which is obviously not working and where I need your help!

[code]<?while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    echo "<span class=\"headerTextUL2\">" .
            "{$row['CATDESC']}</span>" .
            "<p class=\"contentText\">" .
            "{$row['DESCRIPT']}<br>" .
    ?> [/code]

I think what I need to do is something like this:

For each CAT
If new CAT
Print CAT name
For each LINK
Print LINK
Next CAT

But, I'm not real sure if that is correct and if it is, I'm not sure of the syntax.

I hope I've explained this well enough. I appreciate any of your help!

Share this post

Link to post
Share on other sites

[a href=\"http://www.phpfreaks.com/forums/index.php?s=&showtopic=91766&view=findpost&p=367515\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?...ndpost&p=367515[/a]

Share this post

Link to post
Share on other sites
[!--quoteo(post=367518:date=Apr 22 2006, 04:38 PM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Apr 22 2006, 04:38 PM) [snapback]367518[/snapback][/div][div class=\'quotemain\'][!--quotec--]

[a href=\"http://www.phpfreaks.com/forums/index.php?s=&showtopic=91766&view=findpost&p=367515\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?...ndpost&p=367515[/a]

Thanks for the reply.

I truly am a newbie, though, and I'm having a lot of trouble with the syntax. I think I understand the pseudo code you posted, but I'm having a hard time making it work.

Share this post

Link to post
Share on other sites
Something like

[code]$sql = "SELECT a.catdescript, a.gendescript, b.link
        FROM resourceLib a INNER JOIN resourceLibLinks b
        ON a.cat = b.cat
        ORDER BY a.cadtescript, b.cat";
$res = mysql_query($sql) or die (mysql_error());

$lastcat = '';
while (list($catdesc, $gendesc, $link) = mysql_fetch_row($res)) {

       if ($catdesc != lastcat) {
           echo "<H3>$catdesc</H3>
           $lastcat = $catdesc;

       echo "$link<br>\n";


Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.