skeezmo Posted January 4, 2008 Share Posted January 4, 2008 The first part of this code (lines 1-19) are fetching album names from a list of tracks from a mysql database. The classes and functions shown are of the joomla CMS. This is all working fine and I can get the list of albums displayed. $artist is a variable in the URL. What I am then trying to do (line 20 to close tag #1) is to find the songs that correlate to that specific album and artist. Using the example of the database finding two albums or more albums is where we run into the problem. I can get the song lists for both and that is all successful. The thing is that (i dont know if this makes sense) but the arrays aren't differentiating between the different albums. So when I list the songs and albums on a later page (second php tag set) I get the same song listing for both albums. That is the problem. The DEBUG I have set up shows me that I am getting the song and album data correctly from the db, it just shows that both of the arrays are identified by the number zero and are then being lumped together. The album that is fetched second is the one whose songs are shown for both albums. <?php if(isset($artist)) { $query = 'SELECT DISTINCT album, picture FROM `songlist` WHERE artist = "'.rawurldecode($artist).'" ORDER BY album ASC'; $database->setQuery($query); $album_name = $database->LoadResultArray(0); $album_picture = $database->LoadResultArray(1); print_r($album_name); $this->assignRef( 'album_name', $album_name ); $this->assignRef( 'album_picture', $album_picture ); //Cycle through albums and get songlist //Find number of albums $query = 'SELECT COUNT(DISTINCT album) as num FROM `songlist` WHERE artist = "'.rawurldecode($artist).'" ORDER BY album ASC'; $database->setQuery($query); $numalbums = $database->LoadObject(); for ($x = 0, $n = $numalbums->num; $x < $n; $x++) { $query = 'SELECT title, ID FROM songlist WHERE artist = "'.rawurldecode($artist).'" AND album = "'.$this->album_name[$x].'" ORDER BY title ASC'; $database->setQuery($query); $songs = $database->LoadResultArray(0); $songIDs = $database->LoadResultArray(1); //DEBUG print_r($songs); echo '<br /><br />'; $this->assignRef( 'songs', $songs ); $this->assignRef( 'songIDs', $songIDs ); } } ?> <?php $h = 0; for ($j=0, $y=count( $this->album_name ); $j < $y; $j++) { $row =& $this->album_name[$j]; $row2 =& $this->album_picture[$j]; $script = 'window.addEvent(\'domready\', function(){ '; $script .= 'var mySlide'.$j.' = new Fx.Slide(\'slide'.$j.'\'); '; $script .= 'mySlide'.$j.'.hide(); '; $script .= ' $(\'toggle'.$j.'\').addEvent(\'mousedown\', function(e){ e = new Event(e); mySlide'.$j.'.toggle(); e.stop(); }); }); '; $document->addScriptDeclaration( $script ); //Find tracklist for album echo ' <tr> <td width="9%" valign="center">'; if(file_exists('components/com_radio/albumcovers/'.$dj.'/'.$row2)) { echo '<img src="components/com_radio/includes/phpThumb/phpThumb.php?src=../../albumcovers/'.$dj.'/'.$row2.'&w=35" />'; } else { echo '<img width="35px" height="35px">'; } echo '</td><td width="85%" valign="middle"> '.$row.'</td><td width="1%"></td><td width="5%" valign="middle"><a id="toggle'.$j.'" name="toggle'.$j.'" class="slideTitleLink"><span class="slideTitle">[+/-]</span></a> <tr><td colspan="3"> <div id="slide'.$j.'" class="slideBox" style="margin-left:50px;"> <table border="0"> '; for ($r=0, $q=count( $this->songs ); $r < $q; $r++) { echo '<tr><td width="100%"><a href="index.php?option=com_radio&view=songinfo&dj='.$dj.'&songID='.$this->songIDs[$r].'">'.$this->songs[$r].'</a></td><!--<td width="1%"></td><td width="3%">»</td>--></tr>'; } echo ' </table> </div> </td></tr> </td> </tr>'; } ?> <?php } ?> This is what I get from the DEBUG part of my script: Array ( [0] => A Change Of Pace [1] => An Offer You Can't Refuse ) Array ( [0] => A Song the World Can Sing Out Loud [1] => How to Rape a Country [2] => I Wanna Be Your Rock & Roll [3] => I'm Alive [4] => Prepare the Masses [5] => Recipe for Disaster [6] => Safe and Sound In Phone Lines [7] => Shoot from the Hip [8] => Take Care [9] => War In Your Bedroom [10] => Weekend Warriors [11] => White Lines and Lipstick ) Array ( [0] => A Farewell To Friendship [1] => Asleep At The Wheel [2] => Chippie [3] => Death Do Us Part [4] => December [5] => Every Second [6] => Goodbye For Now [7] => Home Is Where The Heart Is [8] => Know One Knows [9] => Loose Lips Sink Ships [10] => Queen Of Hearts ) I think that the fact that both arrays are Array [0] is the problem. Would that make sense? Can anyone identify what the problem is and how I might be able to fix it? Thank you very much. Hess Smith Quote Link to comment https://forums.phpfreaks.com/topic/84435-arrays-not-working-properly-with-db-fetch/ Share on other sites More sharing options...
skeezmo Posted January 4, 2008 Author Share Posted January 4, 2008 *bump* Quote Link to comment https://forums.phpfreaks.com/topic/84435-arrays-not-working-properly-with-db-fetch/#findComment-430553 Share on other sites More sharing options...
hitman6003 Posted January 5, 2008 Share Posted January 5, 2008 You can get the album and song data at the same time, then put it into a multidimensional array: $query = "SELECT ID, album, picture, title FROM `songlist` WHERE artist = '" . urldecode($artist) . "' ORDER BY album ASC, title ASC" $database->setQuery($query); while ($row = $database->loadRow()) { $data[$row->album]['tracks'][$row->ID] = $row->title; if (!$data[$row->album]['picture']) { $data[$row->album]['picture'] = $row->picture; } } print_r($data); Then you should be able to loop through the array and format your data for display: echo ' <table>'; foreach ($data as $album => $info) { echo ' <tr> <td><img src="' . $info['picture'] . '" /></td> <td>'; foreach ($info['tracks'] as $track_id => $title) { echo '<a href="somelink.php?track_id=' . $track_id . '">' . $title . '</a><br />' . "\n"; } echo ' </td> </tr>'; } echo ' </table>'; Hopefully you can use this to accomplish what you are wanting. Quote Link to comment https://forums.phpfreaks.com/topic/84435-arrays-not-working-properly-with-db-fetch/#findComment-430856 Share on other sites More sharing options...
skeezmo Posted January 5, 2008 Author Share Posted January 5, 2008 Thank you very much for your help. I will try this as soon as I can and I will update here. Hess Quote Link to comment https://forums.phpfreaks.com/topic/84435-arrays-not-working-properly-with-db-fetch/#findComment-431034 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.