[英]PHP while loop output to variables
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 非常感谢
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. 遍历数组并打印/访问值。
$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'];
}
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);
). 之后,您将拥有一个很好的所有金属阵列( 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. 应该管用。
$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)
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: 然后,您可以访问$values
数组中的$values
:
echo '$silverColour = '. $values['silver']['colour'];
echo '$silverValue = '. $values['silver']['value'];
echo '$goldColour = '. $values['gold']['colour'];
echo '$goldValue = '. $values['gold']['value'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.