按ID降序显示项目?

时间:2013-03-19 03:28:08

标签: php sql yii web

我在这里和论坛上尝试过这些代码,似乎无法让它运行起来。我有一个索引页面,显示我的最新帖子,只想通过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,
    ));
}


}

3 个答案:

答案 0 :(得分: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你在<? $this->pageTitle = "Latest Music"?>

有短暂的开放式php标签
  

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

答案 1 :(得分:0)

除了topher's correct answer之外,如果您总是想要在整个应用程序中按降序显示帖子,您可以为您的模型设置默认范围,如下所示:

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

这将强制您的新闻模型的所有Active Record调用按ID进行降序。

当然,如果你不想在整个网站上出现这种行为,那么我就不会在默认范围内添加;只需在需要时应用订单子句;)

答案 2 :(得分:0)

使用以下代码管理控制器中的订单:@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,
    ));
}
相关问题