【问题标题】:Order displayed items by id descending?按 id 降序排列显示的项目?
【发布时间】:2013-03-19 03:28:07
【问题描述】:

我已经在这里和论坛上尝试过代码,但我似乎无法让它工作。我有一个索引页面,它显示我的最新帖子,只想按 id 降序显示它们。我还想知道,正如您在下面看到的那样,我在下面用字符来限制我的文本,并且我希望它受到字数的限制,但是当我尝试时,它不会显示任何文本。谢谢这是我的代码。

_view.php

<div class="view">

 <? $this->pageTitle = "Latest Music"?>

<h4><?php echo CHtml::encode($data->id); ?>.
<?php echo CHtml::encode($data->title); ?></h4>

<p><?php 
$charLimit = 680; 
$myText = CHtml::encode($data->text); 
$teaserText = substr($myText,0,$charLimit) . "..."; 
echo $teaserText; 
?></p>

<p><?php echo CHtml::link('View Article', array('view', 'id'=>$data->id)); ?>
&nbsp
<?php echo CHtml::link('Update Article', array('update', 'id'=>$data->id)); ?></p>     
 </div>

NewsController/actionIndex

public function actionIndex()
{
   $this->pageTitle = "$TitleP";
   $TitleP = print CHtml::encode($data->title);

    $dataProvider=new CActiveDataProvider('news_model');
$this->render('index',array(
        'dataProvider'=>$dataProvider,

    ));
}

news_model

class news_model extends CActiveRecord
{

public static function model($className=__CLASS__)
{
    return parent::model($className);
}

public function tableName()
{
    return '{{news}}';
}

public function rules()
{

    return array(
        array('title, text', 'required'),
       array('title', 'length', 'max'=>128),
           array('id', 'length', 'max'=>128),
           array('text', 'length', 'max'=>1500),
        array('title, text', 'safe', 'on'=>'search'),
    );
}

public function relations()
{
    return array(
    );
}


public function attributeLabels()
{

    return array(
        'id' => 'ID',
        'title' => 'Title',
        'slug' => 'Slug',
        'text' => 'Text',
    );
}

public function search()
{
    $criteria=new CDbCriteria;

    $criteria->compare('id',$this->id);
$criteria->compare('title',$this->title,true);
    $criteria->compare('text',$this->text,true);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}


}

【问题讨论】:

    标签: php sql yii web


    【解决方案1】:

    要将订单添加到您的dataProvider,请添加CDbCriteria 作为CActiveDataProvider 构造函数的第二个参数。

    public function index(){
        ...
        $dataProvider=new CActiveDataProvider('news_model',array('criteria'=>array('order'=>'id DESC')));//corrected answer
        ...
    }
    

    限制字数(来自original answer

    $word_limit=100;
    
    if (preg_match_all('/\s/', $text_to_shorten, $m, PREG_OFFSET_CAPTURE) && isset($m[0][$word_limit-1]) && $m[0][$word_limit-1][1]) 
            $shortened_text=substr($text_to_shorten, 0, $m[0][$word_limit-1][1]).'...';
    else $shortened_text=$text_to_shorten;
    echo $shortened_text;
    

    P.S 你在&lt;? $this-&gt;pageTitle = "Latest Music"?&gt; 有简短的开放式 php 标签

    不鼓励这样做,因为它们只有在使用 short_open_tag php.ini 配置文件指令启用,或者 PHP 使用 --enable-short-tags 选项配置时才可用。

    【讨论】:

    • 您好,感谢您的回答,但我收到的订单是Property "CActiveDataProvider.order" is not defined.
    • 还使用单词限制器显示整个文本而不是缩短它? &lt;?$text2 = CHtml::encode($data-&gt;text)?&gt; &lt;p&gt;&lt;?php $word_limit=50; if (preg_match('/\s/', $text_to_shorten, $m, PREG_OFFSET_CAPTURE) &amp;&amp; isset($m[0][$word_limit]) &amp;&amp; $m[0][$word_limit]) $shortened_text=substr($text2,$m[0][$word_limit]).'...'; else $shortened_text=$text2; echo $shortened_text; ?&gt;&lt;/p&gt; 有什么想法吗?谢谢。
    • 我设法通过它修复了订单,因为我必须在上面输入“标准”。
    • 我注意到 io 发布了 $text_to_shorten 之一,但它仍然没有修复它,它仍然显示全文。 &lt;?php $word_limit=50; if (preg_match('/\s/', $text2, $m, PREG_OFFSET_CAPTURE) &amp;&amp; isset($m[0][$word_limit]) &amp;&amp; $m[0][$word_limit]) $shortened_text=substr($text2,$m[0][$word_limit]).'...'; else $shortened_text=$text2; echo $shortened_text; ?&gt;
    【解决方案2】:

    顺便说一句,除了topher's correct answer,如果您总是希望在整个应用程序中按降序显示帖子,您可以为模型设置默认范围,如下所示: p>

    class news_model extends CActiveRecord
    {
        ...
        public function defaultScope()
        {
            $alias = $this->getTableAlias(false,false);
            return array(
                'order'=> "`$alias`.`id` DESC",
            );
        }
        ...
    }
    

    这将强制您的新闻模型的所有 Active Record 调用按 id 降序排列。

    当然,如果您不希望在整个站点上出现这种行为,那么我不会费心添加默认范围;只需在需要时应用 order 子句;)

    【讨论】:

    • 谢谢,但我确实只需要一页,但我收到错误 Property "CActiveDataProvider.order" is not defined 与 Topher 示例。
    【解决方案3】:

    使用下面的代码设法找出控制器中的订单:感谢@Topher

    public function actionIndex()
    {
       $this->pageTitle = "$TitleP";
       $TitleP = print CHtml::encode($data->title);
    
        $dataProvider=new CActiveDataProvider('news_model', 
    array('criteria'=>
                  array('order' => ('id DESC')
                  )));
    
        $this->render('index',array(
            'dataProvider'=>$dataProvider,
        ));
    }
    

    【讨论】:

    • 很抱歉。答案已更正以匹配此。
    • @topher 你有解决我在你的回答中提到的字数的问题吗,因为它似乎没有缩短段落
    • 再次抱歉。正确的方法是preg_match_all 而不是preg_match
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-14
    • 2014-04-30
    相关资源
    最近更新 更多