我已经在这里和论坛上尝试过代码,但我似乎无法让它工作。我有一个索引页面,它显示我的最新帖子,只想按 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)); ?>
 
<?php echo CHtml::link('Update Article', array('update', 'id'=>$data->id)); ?></p>
</div>
新闻控制器/actionIndex
public function actionIndex()
{
$this->pageTitle = "$TitleP";
$TitleP = print CHtml::encode($data->title);
$dataProvider=new CActiveDataProvider('news_model');
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
新闻模型
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,
));
}
}