2014-02-23 70 views
0

我想创建一个用逗号分隔的整数的php字符串。字符串的值来自do {} while循环。我将如何做到这一点?从一个do/while循环创建一个值的字符串

//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; 
+1

'$字符串= $ ec_data [“ec_id”]。 “,”;'?这个? –

回答

2

使用此:

$string .= $ec_data['ec_id'] . ', '; 
2

您可以使用$string =+ $ec_data['ec_id'] . ", ";和循环后删除最后一个逗号与PHP substr方法:http://nl1.php.net/substr

$string = substr($string, 0, -1);

2

小心你的勘定串联运算符,在PHP中是.=。 一种简单的方式来处理的逗号分隔符是首先把整数数组中的 ,然后“粘合”在一起的循环后:

$string = ""; 
$integers = array();   
do { 
    $intgers[] = $ec_data['ec_id'];    

} while ($ec_data = mysql_fetch_assoc($query_ec_data)); 

if (count($integers)) { 
    $string = implode(",", $integers); 
}