Php 如何在codeigniter上仅显示模型的前100个字符
这是我的型号Php 如何在codeigniter上仅显示模型的前100个字符,php,mysql,codeigniter,Php,Mysql,Codeigniter,这是我的型号 function get_news(){ $this->db->select('*'); $this->db->from('articles'); $this->db->where('status' , 0); $this->db->limit(6); $this->db->order_by('created', 'desc');
function get_news(){
$this->db->select('*');
$this->db->from('articles');
$this->db->where('status' , 0);
$this->db->limit(6);
$this->db->order_by('created', 'desc');
return $this->db->get()->result();
}
我的桌子arcticles
id----title----body----status---created
现在,在专栏body中,我只想显示100个字符,我应该在视图、控制器上编辑还是在up上编辑此模型
提前问候。使用substr
$content = substr($str, 0, 100);
当我需要在某个字符处剪切字符串而不剪切最后一个单词时,我总是使用
函数。如果需要,此函数将通过减少字符数来解决此问题
function strSelect( $myString, $maxLength ) {
$out = "";
$s = explode( " ",$myString );
for( $i = 0, $cs = count( $s ); $i < $cs; $i++ ) {
$out .= $s[$i]." ";
if( isSet( $s[$i+1] ) && ( strlen( $out ) + strlen( $s[$i+1] ) ) > $maxLength ) {
break;
}
}
return rtrim( $out );
或者,您可以简单地使用函数在给定的数字处剪切
使用
您需要将正文拆分为100个字符,或者您想获取只有100个字符的数据?是的,我只想获取100个字符,正文上有更多但只有100个字符,我不希望所有SUBSTR在您的tpl中使用。在查询fetchecho substr($result->body,01100)中不需要使用它;只显示100个字符,这是正确的@Sibiraj pr我应该在模型或视图上使用它,你可以在任何地方使用。如果您要从模型中传递一个最终数组,那么您可以在那里合成它本身,或者视图是您可以添加此逻辑的最终位置。
return strSelect(($this->db->get()->result()), 100);
substr(($this->db->get()->result()), 0, 100);
function get_news() {
$this->db->select('*');
$this->db->from('articles');
$this->db->where('status' , 0);
$this->db->limit(6);
$this->db->order_by('created', 'desc');
return substr($this->db->get()->result(), 0, 100);
}