Where to format numbers in CodeIgniter? - php

Should I format my numbers 1234.20 => 1'234.20 in model or in the view using php and codeigniter?

It belongs to the view layer.
The model only cares about the value, not how you present it to the user. Also, you may need to display it differently, if your site is multilingual.

In the view, since the transformation is presentational in nature.

I would format it in the view, because this sperates the visual from the rest.

Related

Laravel - a good way to get db data depending on language

I'm programming a backend in which the admin can edit different texts for the frontend.
The website has about 20-30 different text passages and these are to be displayed in the respective language, depending on whether the user selects English or German in the frontend.
For different text languages I usually used the Laravel language packs. However, since the data is to be changed in the backend, I have to dynamically get the data from the database, depending on the selected language.
Now I'm looking for a good way to get the right data from the database without having to make a database query for each text passage.
Database dummy data looks like this:
With this code:
$helloText = Texts::where(
'title', 'hello')->where(
'code', Config::get('app.locale'))->first();
$aboutText = Texts::where(
'title', 'about')->where(
'code', Config::get('app.locale'))->first();
These 2 requests are for 2 text passages. I would have to do the same for 20-30 other text passages at the moment. I wanted to ask if there would be a better way to summarize them all in one query by specifying all the "titles" I need for this blade.
Or maybe another way. There need's to be another way to do that, without allways doing 20-30 querys. At least I hope for another way.
And if someone know's a better way, I would love to see how I output the correct data in the correct place in the view. Because I haven't found a better way right now..
Thanks for your help!
You can load all the translations with one query:
$texts = Texts::whereIn('title', ['hello', 'about'])
->where('code', config('app.locale'))
->get()
And then just use this collection. For example, you can use the firstWhere() method:
{{ $texts->firstWhere('title', 'hello')->text }}
This requirement is something that is not new, you could rely on existing solutions tackling the same problem.
I suggest you take a look at the translation composer package by waavi, see:
waavi/translation
That should be exactly what you are looking for.

CakePHP: How to show the same query data in all my views, directly in the layout file

I'm creating a website with CakePHP; the problem now is that I want to show in the website footer (that will be visible on all the pages), the result of a query (e.g. "Most popular products").
What is the correct way with CakePHP to achieve this?
At the moment I created a mostPopularList() in my Product controller and a most-popular-list.ctp view that only outputs an <ul> list, thinking that then I could include the output of this file in my layout (default.ctp), but I didn't find the CakePHP way to do that.
Thanks!
Use a view cell or call the query in the AppControllers beforeRender() callback and set it there to the view. Taken from the documentation:
View cells are small mini-controllers that can invoke view logic and render out templates. They provide a light-weight modular replacement to requestAction(). The idea of cells is borrowed from cells in Ruby, where they fulfill a similar role and purpose.
View Cells
Controller::beforeRender()
If this is needed on really every single page I would probably go for the beforeRender() callback, easy to do and change globally.

How to display accented characters entities in a iOS app?

In my iOS app I get data from an external PHP script which builds and returns strings using queries on a mySQL database. In this database, texts have HTML entities in them, e.g. Josè is written as
Josè
When I pass these built strings to my app, all the entities are still there but I'd like to transform them into human readable text in my app. I can't find a way to do this.
I saw questions like this one with accepted answers like this but I can't write a line for any of the hundreds of entities that exist. I mean, I could, but I can't believe there is not a way to do this in a more simple way.
Also, since I use said strings in many places from many views through all the app (text views, labels, table view cells, etc) I think it would VERY useful to apply the correct transformation in the PHP script itself, rather than in the app. So my final question is this: which is the correct way to build a string with entities in it so when I load it in my iOS app all the entities are readable characters? Thank you to ANYONE who will help me!
You can use the Google's category GTMNSString+HTML
This category possibly covers most of the HTML entities you might want to convert into human readable format.
Since I'm not able to find the Google code for this category, I'm pointing to an alternative location; MWFeedParser.
GTMNSString+HTML.h
GTMNSString+HTML.m

Php multilingual website

I am working on a website and the requirement is to make it in two languages i.e. icelandic and english.
just like facebook and other google, if a user selects a language, then the site is translated in that language.
I am not allowed to use google translator.
Any other way to do this in Php
Thanks in advance
Well, I never did it, but i did think about it :), for me i have to do something like this from scratch,
First, do not echo your String that will be displayed to your clients hardcoded, create a dictionary, this dictionary can be in any format, be it php file, xml file, json. You can also extend the functionality by adding Database in it. The main idea is to create a dictionary having all your messages that will be displayed to the user in all the languages you want to display it
consider if you do it using normal PHP FIle, use OOP built class say known as Message, then as attribute to the class add the several languages that you have to use and also some setters and getters
e.g.
Message
{
english;
french;
.....
}
then in PHP, when you echo your messages, try to get the language you want to use, and then
do something like this
echo message.getEnglishMessage();
Look, I've been very generic, now decide on the type of file that you'll use and build the dictionary
Hopes it helps :-)
I use an es.php (spanish not sure what icelandic is) and build all of the mod_rewrite off that. You treat it exactly same as you would if it were the index.php for english. For inputing data into the database have a column for language. All of your queries that call data will then have the language as a condition.
The "gettext" is the way you can go with but if you and your client are in nice understanding ask him to provide the data in language other than english as well and then in DB table there will be a column 'language' in which 'ic' or 'en' flag will be the data, and during fetching the data anywhere, according to language your sql query will contain the language as a where condition with desired flag as its value.

How can I separate logic and presentation code when building a calendar?

Ok, we have a page. A very simple one. Let's call it page.php. It will have a controller and a view. The controller will just catch up some GET variables and print them out in the beginning of that page, the view will show up an header a footer and a paragraph that will contain the GET variables from the controller.
Now let's imagine we want to add a calendar. You think it's good and easy. Well, it seems not at all to me.
The calendar is made up of <table><tbody>X</tbody></table> which is pure HTML and a logic PHP script (that will fill the X) that will create (based on the time()stamp of the current date and time) the month dates and and numbers.
Now I encounter an ethic problem: How could I divide logic and view of that calendar?
The block of code that will create the dates will print <td> and <tr> elaborating the time(). But if the logic prints HTML isn't it bad? I think so, because you loose that comfortably think called "division of logic and view" that is typical of the MVC pattern.
At the same time if we take all the calendar logic to the controller and we just send to the view and array of dates (with coordinates X and Y to tell him the place where to put the first date of the month) the view will still need of elaborate the array and will became logic as well.
How could I really divide logic and view from this calendar?
Simply iterating over an array in a view is perfectly fine and not a violation of the MVC pattern. The logic happens in the controller where you populate the array. Even better would probably be to put the logic into a Model named Calendar.php and simply initialize it in the controller to pass it to the view.
u can put logic codes in a php file and assign the result to the variables that will show in view file (for example array)
in view file you can echo the variables and put html tags easily.

Categories