代码之家  ›  专栏  ›  技术社区  ›  user65748

Yii2-列出每个后编码错误

  •  0
  • user65748  · 技术社区  · 8 年前

    我是Yii2的新手,我做了一个网站,在那里我可以写帖子,查看帖子,删除帖子等等。。。

    我想在一个页面上列出数据库中的每个帖子,作为“时间线”

    我的代码没有列出任何错误,只显示第一条记录。

    这是我在(视图)索引中的代码。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; ?>
    

    我可以问你们,我如何计算每个帖子的浏览量吗?

    我在我的TweetController上试过这个,但不起作用。

    public function actionView($id)
    {
        $this->render('view',array(
            'model'=>$this->loadModel($id));
    }
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   Yupik    8 年前

    开始使用大括号!

    else
        foreach($tweets as $tweet);
    ?>
    

    为此:

    else {
        foreach($tweets as $tweet) {
    ?>
    

    甚至在视图中更好:

    else:
        foreach($tweets as $tweet):
    ?>
    
    .....
    .....
    
    <?php endforeach; ?>
    <?php endif; ?>
    
        2
  •  0
  •   K.B    8 年前

    更新自

    $tweets = $sql->query();
    

    到

    $tweets = $sql->queryAll();
    
    推荐文章