Jump to content


Photo

Inner Joins, Arrays, For Each Loops Help


  • Please log in to reply
3 replies to this topic

#1 anaisamy

anaisamy
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 22 April 2006 - 01:25 AM

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:

CATDESCRIPT for CAT1
GENDESCRIPT for CAT1

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


CATDESCRIPT for CAT2
GENDESCRIPT for CAT2

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


Here is the code I have so far:

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

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

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

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!

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 22 April 2006 - 08:38 PM

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]
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 anaisamy

anaisamy
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 22 April 2006 - 10:44 PM

[!--quoteo(post=367518:date=Apr 22 2006, 04:38 PM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Apr 22 2006, 04:38 PM) View Post[/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.


#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 22 April 2006 - 11:28 PM

Something like

$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";

}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users