Welcome Guest, Not a member yet? Register   Sign In
Best way to format data before outputting to the user
#1

[eluser]digitalbloke[/eluser]
Hi all. I had a quick question about the best way to format data before outputting it to the user.

For example. Say I have an online store and I want to output the price of an item. Sometimes I might want to prefix the price with a currency symbol, and I may also want to format the number for larger numbers, i.e: £1,850.00 But other times I want to output the price as a simple number, i.e: 1850.00 (maybe in an input field within the admin area).

So, in short, where would you format the number? These are the options as I see them:

1) In the Model, but as far as I understand it, models should be just database actions.

2) Add the formatting option in the view. This could cause a problem if I wanted to allow the user to choose how they wanted to format prices / dates from a number of options, and might also cause non-continuity throughout the site.

As I see it, this leaves my third idea:

3) In a Library that corresponds to the Model. i.e the get_price() function in the library, gets the price from the model, and then if a "format" boolean is set to true returns a formatted version of the number.

Am I making this far too complicated?

Thanks in advance,


Tom.
#2

[eluser]JHackamack[/eluser]
I created a helper for just this thing.

It takes a number and a set of parameters (ie currency, local currency symbol, etc) and returns a string with the formatted values.

You can use this in your view and have one place to control everything, that way if you switch currencies, you just set a session variable (thats what i use)
#3

[eluser]digitalbloke[/eluser]
Hi JHackamack

Thanks for your reply. A helper is a good idea, quite new to CI so overlooked that! I'm still drawn to my option 3 for some strange reason but unless anyone else has any suggestions I think I'm going to go with helpers Smile


t.




Theme © iAndrew 2016 - Forum software by © MyBB