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

[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
#2

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

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

echo $short_desc.'...';
#3

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

[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;
#5

[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
#6

[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.
#7

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

[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");
#9

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

[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.




Theme © iAndrew 2016 - Forum software by © MyBB