Welcome Guest, Not a member yet? Register   Sign In
Description length

I'm trying to figure out how to strip a long description down to 50 or so characters... What would I do to pull a description from a database, and trim it down to ~50 characters so that you can get a general idea of what the long description is?

Thanks Smile

I would do something like this:

$short_desc = substr($long_desc, 0, 50);

echo $short_desc.'...';

It's even possible using plain SQL:
SELECT LEFT(description, 50) from table_name

thanks guys

Pete, I like the SQL method better - but I wonder how I would do that using CI's database helper?

My model has a function which looks like this:
$this->db->select('*')->from('available')->order_by("id", "asc");
$query = $this->db->get();
I'm selecting everything from a table,
and then displaying it like this:
<?php foreach($result as $row):?>
&lt;?php endforeach; ?&gt;

Hmm.. Looks like I (or someone) may have to write a hook for LEFT. In the mean time - I'm using JWarren's suggestion and it works just fine Smile

You can use the regular active record syntax:
$this->db->select('LEFT(description, 50), some, more, fields', FALSE);
Just be aware of the following:
Quote:$this->db->select() accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. This is useful if you need a compound select statement.

ah! Thanks Pete Big Grin

errr I'm getting an error when I try this code..
$this->db->select('id, animal_id, status, visible, LEFT(description,25)', FALSE)->from('available')->order_by("id", "asc");
$query = $this->db->get();

but it works fine when I just have
$this->db->select('id, animal_id, status, visible, description', FALSE)->from('available')->order_by("id", "asc");

And the error message is?
Try it without method chaining.

Try escaping `description` with back quotes. In theory, that should let MySQL know that it's a field name and not a string.

Theme © iAndrew 2016 - Forum software by © MyBB