Welcome Guest, Not a member yet? Register   Sign In
Getting only a limited number of characters from model?
#1

[eluser]JamieBarton[/eluser]
Hi guys,

I want to show a preview of a personal message in the Inbox area of my site, I want to show the first 180 characters of the message or so, how it this possible?

Regards,

Jamie
#2

[eluser]eoinmcg[/eluser]
Hi,

Check http://ellislab.com/codeigniter/user-gui...elper.html

Specifically the character_limiter() function
#3

[eluser]JamieBarton[/eluser]
I searched high and low, thanks mate!
#4

[eluser]JamieBarton[/eluser]
Actually, I might need some help here.

I have a model pulling out all of the messages, in my view i have the foreach loop, it's here inside the view loop I want to be using the limiter to limit the message characters. Would/could I use the character_limiter here in the VIEW or is it best to do it in the CONTROLLER. I just don't know how to do it in the CONTROLLER.


Thanks,

Jamie
#5

[eluser]überfuzz[/eluser]
Sound like a case for substr().
#6

[eluser]rogierb[/eluser]
I'd go for substr()or character_limiter in the view. That is the fastest way.

If you are set on doing it in the controller, create another foreach in the controller, substr()/character_limiter the value and create an array with all the right values.

In your view loop through the array instead of the result object.
#7

[eluser]jedd[/eluser]
It probably depends, to some extent, on the number and size of the messages you're pulling out, and whether you need the full versions in the view too.

What if you change your model method to take a size parameter - and when you call your 'get_message()' method you only return back the length you want. Of course you can return the whole message, too.

This way you're only hitting the database for the data you actually want, your controller is in charge of managing the string length (pull it from a config file, f.e.), your view display logic remains very simple.




Theme © iAndrew 2016 - Forum software by © MyBB