我需要创建一个类似‘Acme’
,‘Umbrella’
,‘Waymart’
的字符串,以便在java函数中使用。到目前为止,我有:
$info0 = "SELECT DISTINCT Company FROM CETracker";
$rs0=odbc_exec($conn1,$info0);
$count = 1;
while($row = odbc_fetch_array($rs0))
{
${'V'.$count++} = "" . $row['Company'] . "";
}
$categories = "'$V1', '$V2', '$V3'";
echo $categories;
然后我在函数需要公司列表的地方有$categories
个回音。这项工作只要有3家公司。但是当添加更多内容时,我将不得不添加‘$V4’
,‘$V5’
等等。有任何想法吗?感谢。
答案 0 :(得分:0)
使用数组而不是变量变量,然后可以implode
结果:
$info0 = "SELECT DISTINCT Company FROM CETracker";
$rs0 = odbc_exec($conn1,$info0);
$companies = array();
while($row = odbc_fetch_array($rs0)) {
$companies[] = "'" . $row['Company'] . "'";
}
$categories = implode(', ', $companies);
echo $categories;
...如果您想继续使用变量变量,请使用循环创建字符串:
$info0 = "SELECT DISTINCT Company FROM CETracker";
$rs0=odbc_exec($conn1,$info0);
$count = 1;
while($row = odbc_fetch_array($rs0)) {
${'V'.$count++} = "" . $row['Company'] . "";
}
$categories = '';
for($x = 1; $x <= $count; $x++) {
if($x > 1) $categories .= ', '; // add the comma
$categories .= "'" . ${'V'.$x} . "'"; // add the variable variable
}
echo $categories;
答案 1 :(得分:0)
请试试这个:
$info0 = "SELECT DISTINCT Company FROM CETracker";
$rs0=odbc_exec($conn1,$info0);
$count = 1;
$str = '';
while($row = odbc_fetch_array($rs0))
{
$str .= "'".$row['Company']."',";
}
$str = substr($str,0,strlen($str)-1);
echo $str;