Yii2 - 列出每个 post - 编码错误
Yii2 - list every post - coding error
我是 Yii2 的新手,我创建了一个网站,我可以在其中编写 posts、查看它们、删除它们等等...
我想在一页上将数据库中的每个 post 作为 "timeline"
列出
我的代码没有列出任何错误,只是只显示第一条记录。
这是我的代码(浏览量)index.php
<?php
use yii\helpers\Html;
use yii\widgets\ListView;
/* @var $this yii\web\View */
/* @var $searchModel frontend\models\search\TweetSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'My tweets';
?>
<div class="tweet-model-index">
<h1 class="text-center"><?= Html::encode($this->title) ?></h1>
<p class="text-center">
<?= Html::a('New Tweet', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?php
$con = \Yii::$app->db;
$sql = $con->createCommand("SELECT * FROM tweet ORDER BY created_at DESC");
$tweets = $sql->queryAll();
if(!$tweets)
echo '<h2> This is empty </h2>';
else{
foreach($tweets as $tweet){
?>
<hr>
<div class="container-fluid">
<div class="col-md-8 col-md-offset-2">
<h2 class="text-left">
<?php echo $tweet['tweet_title']; ?>
<br><small>Author: <b><?php echo $tweet['author_id'];?> </b>Created at: <b><?php echo date(($tweet['created_at']));?></b>
<br>Updated at: <b><?php echo date(($tweet['updated_at']));?>
</small>
</h2>
<p class="text-left">
<?= Html::a('Update', ['update', 'id' => $tweet['tweet_id']], ['class' => 'btn btn-primary']) ?>
<?= Html::a('View', ['view', 'id' => $tweet['tweet_id']], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $tweet['tweet_id']], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
</div>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
请问各位大佬,怎么统计每个post的浏览量?
我在我的 TweetController 上试过了,但没有用。
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id));
}
更新自
$tweets = $sql->query();
到
$tweets = $sql->queryAll();
开始使用牙套!
else
foreach($tweets as $tweet);
?>
对此:
else {
foreach($tweets as $tweet) {
?>
甚至在 VIEWS 中更好:
else:
foreach($tweets as $tweet):
?>
.....
.....
<?php endforeach; ?>
<?php endif; ?>
我是 Yii2 的新手,我创建了一个网站,我可以在其中编写 posts、查看它们、删除它们等等...
我想在一页上将数据库中的每个 post 作为 "timeline"
列出我的代码没有列出任何错误,只是只显示第一条记录。
这是我的代码(浏览量)index.php
<?php
use yii\helpers\Html;
use yii\widgets\ListView;
/* @var $this yii\web\View */
/* @var $searchModel frontend\models\search\TweetSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'My tweets';
?>
<div class="tweet-model-index">
<h1 class="text-center"><?= Html::encode($this->title) ?></h1>
<p class="text-center">
<?= Html::a('New Tweet', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?php
$con = \Yii::$app->db;
$sql = $con->createCommand("SELECT * FROM tweet ORDER BY created_at DESC");
$tweets = $sql->queryAll();
if(!$tweets)
echo '<h2> This is empty </h2>';
else{
foreach($tweets as $tweet){
?>
<hr>
<div class="container-fluid">
<div class="col-md-8 col-md-offset-2">
<h2 class="text-left">
<?php echo $tweet['tweet_title']; ?>
<br><small>Author: <b><?php echo $tweet['author_id'];?> </b>Created at: <b><?php echo date(($tweet['created_at']));?></b>
<br>Updated at: <b><?php echo date(($tweet['updated_at']));?>
</small>
</h2>
<p class="text-left">
<?= Html::a('Update', ['update', 'id' => $tweet['tweet_id']], ['class' => 'btn btn-primary']) ?>
<?= Html::a('View', ['view', 'id' => $tweet['tweet_id']], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $tweet['tweet_id']], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
</div>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
请问各位大佬,怎么统计每个post的浏览量?
我在我的 TweetController 上试过了,但没有用。
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id));
}
更新自
$tweets = $sql->query();
到
$tweets = $sql->queryAll();
开始使用牙套!
else
foreach($tweets as $tweet);
?>
对此:
else {
foreach($tweets as $tweet) {
?>
甚至在 VIEWS 中更好:
else:
foreach($tweets as $tweet):
?>
.....
.....
<?php endforeach; ?>
<?php endif; ?>