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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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