2

I have not worked with arrays much in PHP. I have a table of colors. I want to load it into a multidimensional associative array because I am going to be using that table a lot and don't want to do selects over and over again.

I did:

$result = mysql_query("select * FROM color") or die(mysql_error());
$colors = "";
 while($colorrec = mysql_fetch_array($result)){
 $colors[$colorrec['ID']][0] = $colorrec['Description'];
 $colors[$colorrec['ID']][1] = $colorrec['HexCode'];
}

then when I want to access a colors information, I can just do something like:

echo "color code WHT";
echo "description ".$colors['WHT'][0];
echo "Hex Code ".$colors['WHT'][1];

Is this the correct way/methodology to do this?

5 Answers 5

1
$colors = ""; //what?? this should be array()

This should be something like:

$result = mysql_query("select * FROM color") or die(mysql_error());
$colors = array();
while($colorrec = mysql_fetch_array($result)){
   $colors[$colorrec['ID']] = array();
   $colors[$colorrec['ID']]['Desc'] = $colorrec['Description'];
   $colors[$colorrec['ID']]['Hex'] = $colorrec['HexCode'];
}

Then you can do:

echo "color code WHT";
echo "description ".$colors['WHT']['Desc'];
echo "Hex Code ".$colors['WHT']['Hex'];
Sign up to request clarification or add additional context in comments.

Comments

0

Sure nothing wrong with going this way. Also if you get lost in your array, just do a quick print_r($array) and it will output the structure for you.

Comments

0

try with:

$result = mysql_query("select * FROM color") or die(mysql_error());
$colors = array();
 while($colorrec = mysql_fetch_array($result)){
 $colors[$colorrec['ID']] = array( 
              $colorrec['Description'] , 
              $colorrec['HexCode'] );
}

echo "color code WHT";
echo "description ".$colors['WHT'][0];
echo "Hex Code ".$colors['WHT'][1];

Comments

0
  • Neal you can do as he mentioned, if you want more clean go OOP (PHP5)
    class Color 
    {
      $description ="";
      $hexcode = "";
      $someOther="";
    }



    $colors = array();
        $result = mysql_query("select * FROM color") or die(mysql_error());

        while($colorrec = mysql_fetch_array($result))
        {
          $color = new Color();
          $id=$colorrec['HexCode'];
          $color->description = $colorrec['Description'];
          $color->hexCode = $colorrec['HexCode'];
          $colors[$id] = $color;
        }

        echo $color['ID']->description;
        echo $color['ID']->hexCode;

Comments

0

I use it like this:

$numfields = mysql_num_fields($result);
while($red = mysql_fetch_array($result)){
     for ($j=0; $j<$numfields; $j++ ) {
          $field_name = mysql_field_name($result, $j);
          //create Matrix
          $datapull[$i][$field_name] = $red[$field_name]; 
      }
      $i++;
 }
 mysql_free_result($result);
 var_dump($datapull);

This is more generic and harder to search, it should be used in class method or a function, so you pass the SQL and always expect the same format as a result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.