Jump to content

Parsing arrays generated from distinct MySQL tables into one foreach loop


elliotpo

Recommended Posts

Hello,

I can't find anything about this subject here... if I missed a relevant post, I apologize.

 

I'm pulling search results from two MySQL tables.  I then need to parse the results into a foreach loop that's also outputting DIVs Javascript-controlled DIVs.

 

The searches are like so.  find_by_sql generates an object array that aligns db table fields as values with class attributes as keys.

 

<?php

$sql = "SELECT * FROM artists ORDER BY number ASC";

$musicians = artists::find_by_sql($sql);

$sql = "SELECT * FROM artistPics ORDER BY number ASC";

$mpics = artistPics::find_by_sql($sql);

?>

 

Then the output:

 

<div id="musicians">

        <p>Musician Profiles - click to learn more</p>

     

        <div id='Accordion1' class='Accordion' tabindex='0'>

          <?php

  foreach($musicians as $m) {

          echo "<div class='AccordionPanel'>";

            echo "<div class='AccordionPanelTab'>$m->artist_name - $m->artist_role</div>";

            echo "<div class='AccordionPanelContent'><img src='' />$m->artist_bio</div>";

        echo "</div>";

}

              ?>

</div>

 

I need to get the picture output into the img src spot.  But running a foreach loop inside another screws up everything, and I can't figure any other way.

 

I could do the processing up top and try to parse out the pics into variables I'd then just call below, but there are an indeterminate number of artists in the table at any time, and I can't figure of that's possible without knowing how many results would have to be parsed.

 

I hope this makes some sense--

 

I'd appreciate any advice.

 

Thanks so much -

 

elliotpo

 

For the artist_pic_array make it so:

key=artist name //  same as the artist name pulled from the other table

value=artists pic location

so in your example the artist name is $m

artist_pic_array[$m]

would give you the pics location

 

HTH

Teamatomic

Ah, I appreciate the feedback-- but the trouble is that the foreach output has to repeat X number of times.  and the pic changes each time.  so i'm not sure how to call items from a second array (the pics array) while the first array (the artists array) is outputting.  the DIVS wind up repeating multiple times and the whole thing jumbles.  the $m that's holding artist data just comes from the foreach loop instructions...

 

Unless I'm misunderstanding your advice?  Which is possible..

 

Archived

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

×
×
  • 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.