CodeIgniter Forums
Description length - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Description length (/showthread.php?tid=16066)

Pages: 1 2


Description length - El Forum - 02-23-2009

[eluser]NateL[/eluser]
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


Description length - El Forum - 02-23-2009

[eluser]JWarren[/eluser]
I would do something like this:

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

echo $short_desc.'...';



Description length - El Forum - 02-23-2009

[eluser]pistolPete[/eluser]
It's even possible using plain SQL:
Code:
SELECT LEFT(description, 50) from table_name



Description length - El Forum - 02-23-2009

[eluser]NateL[/eluser]
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:
Code:
$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:
Code:
<?php foreach($result as $row):?>
   <td>&lt;?=$row->status?&gt;</td>
   <td>&lt;?=$row->description?&gt;</td>
&lt;?php endforeach; ?&gt;



Description length - El Forum - 02-23-2009

[eluser]NateL[/eluser]
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


Description length - El Forum - 02-23-2009

[eluser]pistolPete[/eluser]
You can use the regular active record syntax:
Code:
$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.



Description length - El Forum - 02-23-2009

[eluser]NateL[/eluser]
ah! Thanks Pete Big Grin


Description length - El Forum - 02-23-2009

[eluser]NateL[/eluser]
errr I'm getting an error when I try this code..
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
Code:
$this->db->select('id, animal_id, status, visible, description', FALSE)->from('available')->order_by("id", "asc");



Description length - El Forum - 02-23-2009

[eluser]pistolPete[/eluser]
And the error message is?
Try it without method chaining.


Description length - El Forum - 02-23-2009

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