php Yii2 显示每个循环使用的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28618043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Yii2 display data using for each loop
提问by con322
Using yii2 I have created a Model and CRUD using gii.
使用 yii2 我已经使用 gii 创建了一个模型和 CRUD。
I want to use a foreach or while loop in my VIEW to display the data in the following format
我想在我的 VIEW 中使用 foreach 或 while 循环以以下格式显示数据
For each row in database table
对于数据库表中的每一行
echo("addMarker($lat_field, $lon_field);\n");
I have an index page which is rendered using the following controller action.
我有一个使用以下控制器操作呈现的索引页。
public function actionIndex()
{
$this->layout = 'directory';
$searchModel = new ShopDirectorySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
I can use the following to display the data using listview which displays all the data/rows within database however it has html around it and obviously isn't outputted in the format I wish it to be.
我可以使用以下内容使用 listview 显示数据,该列表视图显示数据库中的所有数据/行,但是它周围有 html 并且显然没有以我希望的格式输出。
<?= ListView::widget([
'dataProvider' => $dataProvider,
'itemOptions' => ['class' => 'col-xs-6 col-sm-3'],
'itemView' => '_index',
]);?>
回答by soju
No need to use ListView here, you should simply try :
此处无需使用 ListView,您只需尝试:
foreach ($dataProvider->models as $model) {
echo "addMarker({$model->lat_field}, {$model->lon_field});";
}
If you really want to use ListView, you could simply edit _index
view file.
如果你真的想使用 ListView,你可以简单地编辑_index
视图文件。
回答by vishuB
if(!empty($st_data))
{
foreach($st_data as $row)
{
echo 'Country Name: '.$row['country_name'].'</br>';
echo 'State Name: '.$row['state_name'].'</br>';
echo 'City Name: '.$row['city_name'].'</br>';
echo '</br>';
}
exit;
}
回答by Timothée Planchais
$rows = ShopDirectory::findAll();
if(!empty($rows))
{
foreach($rows as $row)
{
$lat = $row->lat;
$lon = $row->lon;
$this->view->registerJs('addmarker("'.$lat.'", "'.$lon.'"."\n");', yii\web\View::POS_END);
...
}
}