[英]Create a string of values from a do/while loop
I am trying to create a php string with integers separated by commas. 我正在尝试创建一个用逗号分隔的整数的php字符串。 The values for the string are coming from a do { }while loop. 字符串的值来自do {} while循环。 How would I do this? 我该怎么做?
//GRAB EC DATA
$mysql_ec_data = "SELECT `ec_c_id` FROM `e_c` WHERE `ec_id` = '$e_id'";
$query_ec_data = mysql_query($mysql_ec_data) or die(mysql_error());
$ec_data = mysql_fetch_assoc($query_ec_data);
$string = "";
do {
//There will be values looping through here that I want to add to a single string.
$string =+ $ec_data['ec_id'];
} while ($ec_data = mysql_fetch_assoc($query_ec_data));
//this is how the values from the while loop should look in the string at the end
$string = 45,52,23;
用这个:
$string .= $ec_data['ec_id'] . ', ';
You could use $string =+ $ec_data['ec_id'] . ", ";
您可以使用$string =+ $ec_data['ec_id'] . ", ";
$string =+ $ec_data['ec_id'] . ", ";
and after the loop remove the last comma with PHP substr
method: http://nl1.php.net/substr 并在循环后使用PHP substr
方法删除最后一个逗号: http : //nl1.php.net/substr
$string = substr($string, 0, -1);
Be careful with your assignment-concatenation operator, in PHP it is .=
. 注意您的赋值-串联运算符,在PHP中为.=
。 A simple way to handle the comma delimiter is to first put the integers in an array and then "glue" them together after the loop: 处理逗号定界符的一种简单方法是,首先将整数放入数组中,然后在循环后将它们“粘合”在一起:
$string = "";
$integers = array();
do {
$intgers[] = $ec_data['ec_id'];
} while ($ec_data = mysql_fetch_assoc($query_ec_data));
if (count($integers)) {
$string = implode(",", $integers);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.