我想这样回答
["巴丁","巴哈瓦尔纳加尔","巴哈瓦尔 bool "]
但是当我运行查询时
if ($result->num_rows() > 0)
{
echo '[';
foreach($result->result() as $listing)
{
echo '"'.$listing->city_name.'"';
if ($listing->city_name!='')
{
echo ',';
}
}
echo ']';
}
然后我在最后一次有额外的昏迷请帮我把它去掉
我想删除最后的昏迷 ["Badin","Bahawalnagar","Bahawalpur",]
最佳答案
你的代码应该是这样的:
if ($result->num_rows() > 0)
{
echo '[';
foreach($result->result() as $key => $listing)
{
$row = $key+1;
if ($listing->city_name != '')
{
echo '"'.$listing->city_name.'"';
// If it's not last item, then add a comma
if ($row < $result->num_rows())
{
echo ',';
}
}
}
echo ']';
}
我还假设,如果城市名称为空,您不想回显城市名称,否则您最终会得到空的 ""
。
关于php codeigniter数据库查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14795974/