Jump to content

Understanding Arrays


TapeGun007

Recommended Posts

$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.

 

Link to comment
https://forums.phpfreaks.com/topic/233677-understanding-arrays/
Share on other sites

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

 

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.

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.