Simon
Simon

Reputation: 11

codeigniter database how to limit the output

Hello how can i like if i use substr(); do so i only get like 400 number of characters from the database out?

Upvotes: 0

Views: 3640

Answers (3)

Abhijith A C
Abhijith A C

Reputation: 530

It too late, but this is for someone like me looking for solution

public function getDetails(){
    // mytable(id,name,about,...,status)
    $this->db->select(array('id', 'name', 'SUBSTRING(about,1,180) AS about', 'status'));
    $result=$this->get('mytable');          
    return result_array();
}

Upvotes: 0

Brad
Brad

Reputation: 1691

You can use codeigniters limiter (test helper) to display only what you want

$string = "Here is a nice text string consisting of eleven words.";

$string = character_limiter($string, 400);

You can pull the entire string out of the database but only use the number of characters you need.

Or take a look at this tutorial using "left" in mysql http://net.tutsplus.com/tutorials/php/how-to-create-blog-excerpts-with-php/

Upvotes: 0

Alpesh
Alpesh

Reputation: 5405

You have to use core mysql function SUBSTRING to achieve this.

In codeigniter the query can be written as -

$this->db->select("SUBSTRING('COLUMN_NAME',5)");

$query = $this->db->get('TABLE_NAME');

foreach ($query->result() as $row)
{
    //process result here.
}

Upvotes: 3

Related Questions