Jump to content

Inner Joins, Arrays, For Each Loops Help


anaisamy

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:

[u]CATDESCRIPT for CAT1[/u]
[i]GENDESCRIPT for CAT1[/i]

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


[u]CATDESCRIPT for CAT2[/u]
[i]GENDESCRIPT for CAT2[/i]

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>" .
            "</p>";
    }
    ?> [/code]

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

For each CAT
If new CAT
Print CAT name
Else
For each LINK
Print LINK
Next 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!
Link to comment
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--]
see

[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]
[/quote]


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.
Link to comment
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>
                  <H4>$gendesc</H4>\n";
           $lastcat = $catdesc;
       }

       echo "$link<br>\n";

}[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.