如何在树枝文件中显示行计数结果
How to display a row count result in twig file
我正在获取存储库中的行计数,结果该查询返回一个数组。我如何获取数字计数并将结果显示在我的树枝文件中?
这是获取行数的查询:
public function getTrailCount(){
$sql = "SELECT count(`id`) FROM `article` where `publish`='1' AND `catForFilter` like '%trail%' ";
$stmt = $this->connection->prepare($sql);
$stmt->execute();
$trailCount = $stmt->fetchAll(PDO::FETCH_ASSOC);
//$trailCount->getSingleScalarResult();
//echo '<pre>'; print_r($trailCount); exit;
return $trailCount;
}
在控制器中,我试图像这样获取它,但我知道这是一个错误的过程:
foreach($trailCount as $trail){
$data['trailCount'] = $trail->count; //throughing an error
}
如何修复此代码,非常感谢任何帮助。谢谢
在那种情况下,使用 PDO::FETCH_NUM
会简单得多:
$trailCount = $stmt->fetchAll(PDO::FETCH_ASSOC);
...
foreach($trailCount as $trail){
$data['trailCount'] = $trail[0]; // Using "0" instead of named key
}
但如果您仍然真的想使用 FETCH_ASSOC
,您需要:
$sql = "SELECT count(`id`) as cnt FROM `article ...... "
.....
foreach($trailCount as $trail){
$data['trailCount'] = $trail['cnt'];
}
请注意,我没有使用 ->cnt
,而是使用 ['cnt']
,因为从 PDO
返回的数据不是基于对象的,而是基于数组的。
希望对您有所帮助...
编辑:
鉴于缺少 Twig
部分,我只能假设您要执行的操作:
/**
* @Route("/foo")
* @Template()
*/
public function fooAction(){
...
... The code above
...
return array('data' => $data);
}
然后,在你的树枝上:
{{ data.trailCount }}
我在 Jovan Perovic 的帮助下找到了解决方案,我是这样做的:
查询部分:
public function getTrailCount(){
$sql = "SELECT count(`id`) as cnt FROM `article` where `publish`='1' AND `catForFilter` like '%trail%' ";
$stmt = $this->connection->prepare($sql);
$stmt->execute();
$trailCount = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $trailCount[0]['cnt']; //instead of an array i passed the single value
}
在控制器中:
$trailCount = $clicknblog->getTrailCount();
return $this->render('AdventureBiddingBundle:Organiser:editProfileOrganiser.html.twig', array(
'trails' => $trailCount
));
在 twig 文件中我直接显示了值:
<p>{{ trails }}</p>
希望这对遇到同样问题的人有所帮助
我正在获取存储库中的行计数,结果该查询返回一个数组。我如何获取数字计数并将结果显示在我的树枝文件中?
这是获取行数的查询:
public function getTrailCount(){
$sql = "SELECT count(`id`) FROM `article` where `publish`='1' AND `catForFilter` like '%trail%' ";
$stmt = $this->connection->prepare($sql);
$stmt->execute();
$trailCount = $stmt->fetchAll(PDO::FETCH_ASSOC);
//$trailCount->getSingleScalarResult();
//echo '<pre>'; print_r($trailCount); exit;
return $trailCount;
}
在控制器中,我试图像这样获取它,但我知道这是一个错误的过程:
foreach($trailCount as $trail){
$data['trailCount'] = $trail->count; //throughing an error
}
如何修复此代码,非常感谢任何帮助。谢谢
在那种情况下,使用 PDO::FETCH_NUM
会简单得多:
$trailCount = $stmt->fetchAll(PDO::FETCH_ASSOC);
...
foreach($trailCount as $trail){
$data['trailCount'] = $trail[0]; // Using "0" instead of named key
}
但如果您仍然真的想使用 FETCH_ASSOC
,您需要:
$sql = "SELECT count(`id`) as cnt FROM `article ...... "
.....
foreach($trailCount as $trail){
$data['trailCount'] = $trail['cnt'];
}
请注意,我没有使用 ->cnt
,而是使用 ['cnt']
,因为从 PDO
返回的数据不是基于对象的,而是基于数组的。
希望对您有所帮助...
编辑:
鉴于缺少 Twig
部分,我只能假设您要执行的操作:
/**
* @Route("/foo")
* @Template()
*/
public function fooAction(){
...
... The code above
...
return array('data' => $data);
}
然后,在你的树枝上:
{{ data.trailCount }}
我在 Jovan Perovic 的帮助下找到了解决方案,我是这样做的:
查询部分:
public function getTrailCount(){
$sql = "SELECT count(`id`) as cnt FROM `article` where `publish`='1' AND `catForFilter` like '%trail%' ";
$stmt = $this->connection->prepare($sql);
$stmt->execute();
$trailCount = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $trailCount[0]['cnt']; //instead of an array i passed the single value
}
在控制器中:
$trailCount = $clicknblog->getTrailCount();
return $this->render('AdventureBiddingBundle:Organiser:editProfileOrganiser.html.twig', array(
'trails' => $trailCount
));
在 twig 文件中我直接显示了值:
<p>{{ trails }}</p>
希望这对遇到同样问题的人有所帮助