TapeGun007 Posted April 14, 2011 Share Posted April 14, 2011 $result = mysql_query("SELECT * FROM Chords_Loops_Lyrics ORDER BY MusicFiles_ID ASC"); while($row = @mysql_fetch_array($result)){ $Chords_Loops_Lyrics = array($row['CCL_ID'] => $row['MusicFiles_ID'], $row['Type'], $row['FileName']); } I know I'm doing this wrong, because of the results. How can I assign values to this array, when I do a print r on this, it only spits out the last value. I need to store like 100 of these "chord loops and lyrics" into an array with the values you see in the $row fields. I just can't seem to find how to properly write this in php. Quote Link to comment https://forums.phpfreaks.com/topic/233677-understanding-arrays/ Share on other sites More sharing options...
kenrbnsn Posted April 14, 2011 Share Posted April 14, 2011 You're always overwriting the value of $Chords_Loops_Lyrics in the loop. Change your code to <?php $Chords_Loops_Lyrics = array(); $result = mysql_query("SELECT * FROM Chords_Loops_Lyrics ORDER BY MusicFiles_ID ASC"); while($row = @mysql_fetch_array($result)){ $Chords_Loops_Lyrics[] = array($row['CCL_ID'] => $row['MusicFiles_ID'], $row['Type'], $row['FileName']); } ?> Or <?php $Chords_Loops_Lyrics = array(); $result = mysql_query("SELECT * FROM Chords_Loops_Lyrics ORDER BY MusicFiles_ID ASC"); while($row = @mysql_fetch_array($result)){ $Chords_Loops_Lyrics[$row['CCL_ID']] = array($row['MusicFiles_ID'], $row['Type'], $row['FileName']); } ?> Try it both ways and see which resultant array is easier to use. Ken Quote Link to comment https://forums.phpfreaks.com/topic/233677-understanding-arrays/#findComment-1201422 Share on other sites More sharing options...
TapeGun007 Posted April 14, 2011 Author Share Posted April 14, 2011 LOL. I knew it had to be some simple syntax, I was missing the "[]". I should've realized this from coding one dimensional arrays, but... oh well. In any case, the first example you gave me works perfectly! The second example you gave me simply does what my original code did, it's keeps overwriting and only shows the last entry on the print_r results. Thank you Ken. You've been a great help to me about 4-5 times on here. Quote Link to comment https://forums.phpfreaks.com/topic/233677-understanding-arrays/#findComment-1201432 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.