Description length |
[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
[eluser]JWarren[/eluser]
I would do something like this: Code: $short_desc = substr($long_desc, 0, 50);
[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"); and then displaying it like this: Code: <?php foreach($result as $row):?>
[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
[eluser]pistolPete[/eluser]
You can use the regular active record syntax: Code: $this->db->select('LEFT(description, 50), some, more, fields', FALSE); 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.
[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"); 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");
[eluser]pistolPete[/eluser]
And the error message is? Try it without method chaining.
[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. |
Welcome Guest, Not a member yet? Register Sign In |