0

I have the following loop to get data from a database...

$query = "SELECT id, metal, colour, value FROM data WHERE asset='gold' OR asset='silver' ORDER BY id DESC LIMIT 2";
$result = mysqli_query($connect, $query);
while ($row = mysqli_fetch_assoc($result)){
    echo $row['metal'] . " is " . $row["colour"] . "<br />";
    echo $row['metal'] . " is worth " . $row["value"] . "<br />";
}

which outputs the following:

silver is grey
silver is worth 50
gold is yellow
gold is worth 100

The following:

echo $row['metal'] . " is " . 

and...

echo $row['metal'] . " is worth " . 

...are simply there for testing and to help explain my issue. How would I go about putting the output into variables like so:

$silverColour = grey
$silverValue = 50
$goldColour = yellow
$goldValue = 100

Many Thanks

3
  • If I understand your question you are looking for something like $silverColour = $row['colour']; Is that correct? Commented Jul 9, 2016 at 20:28
  • I recomend you using objects for this kind of data movement... Commented Jul 9, 2016 at 20:33
  • mrpotocnik, yes correct :) Commented Jul 9, 2016 at 21:09

5 Answers 5

3

You can use {}, try this:

  ${$row['metal'].'Color'} = $row["colour"]; 
  ${$row['metal'].'Value'}= $row["value"];

Another easier way is to use keys and values of arrays. Keep the key as

 $array[row['metal'].'Color']= $row["colour"];
 $array[row['metal'].'Value']= $row["value"];

Iterate over the array and print/access the values.

Sign up to request clarification or add additional context in comments.

2 Comments

While this answer is flawless answering the question, in my honest opinion variable variables is not the way to go as it could create undesired variable names, meaning bugs in code.
@Xorifelse agreed. The approach needs to be changed, I answered him as per question.
1
$query = "SELECT id, metal, colour, value FROM data WHERE asset='gold' OR asset='silver' ORDER BY id DESC LIMIT 2";
$result = mysqli_query($connect, $query);
while ($row = mysqli_fetch_assoc($result)){
 ${$row['metal'].'color'} = $row['metal'];
 ${$row['metal'].'value'} = $row['value'];
}

Comments

0

Modify your code this way:

$values = [];
$result = mysqli_query($connect, $query);
while ($row = mysqli_fetch_assoc($result)){
    echo $row['metal'] . " is " . $row["colour"] . "<br />";
    echo $row['metal'] . " is worth " . $row["value"] . "<br />";
    $values[$row['metal']] = $row;
}

Then you can access the values in the $values array:

echo '$silverColour = '. $values['silver']['colour'];
echo '$silverValue = '. $values['silver']['value'];
echo '$goldColour = '. $values['gold']['colour'];
echo '$goldValue = '. $values['gold']['value'];

1 Comment

Thank you to all who answered, I chose this as the answer as it was the first one I tried and it worked perfectly. Thanks again.
0

I could show you a hackish way of doing it, but I'd like to show you a different approach using arrays:

$query = "SELECT id, metal, colour, value FROM data WHERE asset='gold' OR asset='silver' ORDER BY id DESC LIMIT 2";
$result = mysqli_query($connect, $query);
$metals = array();
while ($row = mysqli_fetch_assoc($result)){
    $metals[$row['metal']] = array('color' => $row['color'], 'value' => $row['value']);
}

After this, you'll have a nice array of all metals (var_dump($metals);).

Accessing it like this:

$metals['gold']['color'];
$metals['gold']['value'];

If you really insist on using your approach,

$query = "SELECT id, metal, colour, value FROM data WHERE asset='gold' OR asset='silver' ORDER BY id DESC LIMIT 2";
$result = mysqli_query($connect, $query);
while ($row = mysqli_fetch_assoc($result)){
    ${$row['metal'] . 'Color' } = $row['color'];
    ${$row['metal'] . 'Value' } = $row['value'];
}

should work.

Comments

0
$color= array();
$values= array();
while (($row = mysqli_fetch_assoc($result)) {
   $color[] = (string) $row["colour"];
   $color[] = (string) $row["value"];
}

// If you don't want an array, but a string instead, use implode:

$color= implode(' ', $color)
$values= implode(' ', $values)

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.