查询使用php codeigniter从数据库表字段中获取字符串的n个字符

时间:2015-04-29 08:08:12

标签: php mysql codeigniter

// ---查询使用php codeigniter从数据库表字段中获取字符串的n个字符

// - 查看页面

<?php  foreach ($query as $row)

{ 

$content = $row->blogger_content;

?>

<p> <?php echo $content; ?>

(here i want to list the content but only needed first 25 character) </p>

<?php  }  ?>

// what query want to use here..?

6 个答案:

答案 0 :(得分:1)

在模型类中尝试此操作。

$this->db->select(SUBSTRING(columnName, 0, 25))
         ->from(tablename)
         ->get();

答案 1 :(得分:0)

使用它并添加第二个参数FALSE,如:

$this->db->select('SUBSTRING(columnName, 0, 25)', FALSE)
        ->from('table_name'); 

将第二个参数设置为FALSE,CodeIgniter不会尝试使用反引号来保护您的字段或表名。

答案 2 :(得分:0)

如果你使用utf-8字符语言 - 多字节 - 你必须使用mb_substr而不是substr

答案 3 :(得分:0)

更改此

<?php echo $content; ?>

进入这个

<?php echo strlen($content) > 25 ? substr($content, 0, 25).'...' : $content; ?>

将检查$ content是否超过25个字符,并且最后只显示25个字符...或者如果不超过25个字符则显示$ content

答案 4 :(得分:0)

检查text helper中的word_limitercharacter_limiter功能,以便:

$content = character_limiter($row->blogger_content, 25);

这将是CodeIgniter方式。不要忘记自动加载或加载文本助手。

答案 5 :(得分:0)

<?php

$content_overview = (strlen($content) > 25) ? substr($content,0,25).'... <a href="#">Read More</a>' : $content;

echo $content_overview;

?>