How to get all languages data in codeigniter - php

I am trying to fetch all language files data and store them in array. But I am only able to get default language data.
I need all language data to sync them in another files. So far I have tried following code,
public function SyncJsLanguageFile($Language) {
//I need German (de) and other data as well data
$this->lang->load('translate','de');
//But I can only get default language data. So I think above line is not working.
$LanguageMessage = $this->lang->language;
echo "<pre>";
print_r($LanguageMessage);
die;
}
I don't want to change default language, I just need all language data in array for specific purpose. I should not change Site default language.
Please help me out here.
I have tried,
$data = $this->lang->load('translate','de', TRUE);
but it is not working. :(

Please do these steps
//load helper for language
$this->load->helper('language');
//mytest is the language file in english folder
//application\language\english\mytest_lang.php
$this->lang->load('mytest','english');// filename, folder name in
//fetch all the data in your custom $all_lang_array variable
$all_lang_array=$this->lang->language;
//display or test the data.
print_r( $all_lang_array);

Related

'Security' helper not working in CodeIgniter

I am using CodeIgniter v 3.1.4. In one of the form, i have a text box to save urls (like https://stackoverflow.com/questions/ask, http://google.com etc.) into the MySql database.
Before save, i am sanitizing the urls like shown below:
Inside Constructor:
$this->load->helper('security');
Inside Add function:
$hlt_url = $this->security->xss_clean($this->input->post('txtHLnk'));
$hlt_hlines = $this->security->xss_clean($this->input->post('txtAreaHLine'));
When i enter simple/plain string in the textboxes, then the data gets saved in the DB, however when i enter url in the textboxes, the data is not saved in DB, neither does it show any error message.
Please explain with code whats i am doing wrong. Thanks
$hlt_url = $this->input->post('txtHLnk', true);
$hlt_hlines = $this->input->post('txtAreaHLine', true);
I thin kthis is the new implementation of sanitizing inputs in latest version.

Storing multiple artist info from API into file (PHP)

I really need help. I'm using an API to gather artist information depending on the artist name ($artist_name = $_GET['artistname']).
I need to add the API results into an array and store it into a file (NOT a database). This file will be ever growing as more and more artist entries are added to it.
Once I have an array in the file, I need to be able to read it and parse it. That way I can display the information without repeatedly using the API.
I have figured out how to add an array to a file with one entry, but how can I add more keys into the same array?
This is what I'm using now...
//ARRAY
$artist_info_location_array = array($artist_name => $location_entry);
//FILE
$artist_location_file = get_template_directory()."/Database/Artists/info-location.json";
//GET ARRAY FILE
$get_location_array[] = json_decode(file_get_contents($artist_location_file), true);
if (is_array($get_location_array)) {
if (!array_key_exists($artist_name, $get_location_array)) {
file_put_contents($artist_location_file, json_encode($artist_info_location_array));
}
}
It prints this to the file:
{"Imagine Dragons":"Las Vegas, NV, US"}
That's cool, but I need to be able to add more artists to this SAME ARRAY. So the result should look like this with another artist added:
{"Imagine Dragons":"Las Vegas, NV, US", "Adele":"London, UK"}
That shows Imagine Dragons and Adele both added to the same array.
Can someone help me "append" or add extra keys and values to the same array as they are added to the file?
Thanks.
EDIT 1 (In response to Martin):
I have a panel on the side of the page in question. This panel will show relevant information about the artist that has been searched for. Let's say you search for the artist "Adele". $artist_name would = Adele.
Lets say I'd like to store all artist locations, I would use the example I posted to store each artist location in the file called info-location.json ($artist_location_file).
So every time an artist page is loaded, the artist name and location would be added to the array in the file.
If my example doesn't make any sense, please show me an example on how to add multiple entries into ONE ARRAY. I am using an API and would like to cache this information to use instead of requesting the API on each load.
Hope this makes sense. :)
I might be misunderstanding your question, but if you just want to read in a json file, add an associative array key to it if it does not exist and then put it back into the json file why dont you do something like this:
if (is_array($get_location_array)) {
if (!array_key_exists($artist_name, $get_location_array)) {
$get_location_array[$artist_name] = $location;
file_put_contents($artist_location_file, json_encode($artist_info_location_array));
}
}
file_put_contents will overwrite an existing file (pretty sure). But your best option is to use a database. If you can't do that, then I suggest to prevent writing to the file while you are doing this I suggest you use fopen, flock, and fwrite and then fclose

how to change language in codeigniter

I've saved a product name in my DB with two language (En & Fr)
how can i load them differently by just changing the url
for example
localhost/mysite/en/products/
loads the "en_name" out of my table
and
localhost/mysite/fr/products/
loads the "fr_name" for me?
The language library is used if you want to store the text in different languages in a file (it will be included in the page) and not in a database. So if the language text is stored in the database, you want to check the method responsible for checking the url segment to check if the url contains the en or the fr and load the corresponding language.
So the method to use is:
$this->uri->segment(n)
More info on how to use it:
https://www.codeigniter.com/user_guide/libraries/uri.html
and your condition should be something like:
Edit
if($this->uri->segment(1) == "fr"){
$this->session->set_userdata('lang', 'fr');
}else{
$this->session->set_userdata('lang', 'en');
}
Now you can easily check for the language selected from the model (db files) by checking the value of the lang Session
I think the simplest method may be to pass the uri segment to the where clause of the query.
https://www.codeigniter.com/user_guide/libraries/uri.html
https://www.codeigniter.com/user_guide/database/query_builder.html?highlight=select#selecting-data
Something like:
$lang = $this-uri->segment(3);
$this->db->where('language', $lang);
$this->db->get('table');
i finally did it with this CI extension i18n and here is my code:
$lang = $this->uri->segment(1);
echo $prodct[$lang."_name"];
so now if i enter my url like this
localhost/mysite/en/products/
it will get those fields from DB which is like "en_name" ,
same goes for "fr" too.

jqgrid php cannot populate the grid

I am trying using the jqgrid. I set the column, and i attached to that the widget. Everything is working except the grid does not contain data. It only contains an empty row. Column, widgets (like the calendar) works.
The following is the beginning of my grid setting:
// Create the jqGrid instance
$grid = new jqGridRender($conn);
// Write the SQL Query
$grid->SelectCommand = "SELECT * FROM `$table_name`";
// set the ouput format to json
$grid->dataType = 'json';
$grid->table = "$table_name";
$grid->setPrimaryKeyId("matter_party_ID");
$grid->serialKey = false;
$grid->setColModel();
// Set the url from where we obtain the data
$grid->setUrl(????????);
$grid->addCol(array( etc. etc.
As you can see I am retriving the data with a a database query and returning a jason object to the grid:
// Write the SQL Query
$grid->SelectCommand = "SELECT * FROM `$table_name`";
// set the ouput format to json
$grid->dataType = 'json';
But the data is not there. After few invane searches I have been suggested to add the line:
$grid->setUrl(????????);
But I don't understand it. Why do I need to set a url if the data has been fetch on the current url using a select? Can you help?
Thank you
Watch the link. As given here when you are not assigning arrays to the jqGrid you need to provide a url from where the data can be retrieved. This is because the jqgrid needs formatted data in the form of json or xml and doesn't process resultset directly. You could select the data and process it as required as shown in the link and then use the php as a url for your jqgrid which is defined in another php file.
I am answering my own question in case someone needs to solve the same or a similar problem. In the:
$grid->setUrl(????????);
I pass a php file like grid.php that calls the function that builds the grid again!
$grid->setUrl('grid.php');
The problem is that when I generate a file through my own MVC it automatically adds a header and a footer compromising the json object returned by the grid constructor and not allowing the grid to be populated! I got rid of header and footer and the data magically reappeared!

how to make a multi-lingual php site?

I am developing a site using php and mysql. I want to know... what's a good way to deal with multi-lingual support? I want a user to be able to select from a drop down and select their language. Then everything (content, buttons, links) except the user-written content is in their language.
What's a good way to approach this? Use a cookie? Session?
Something like this works fine:
Langs.php
<?
// check if language switch as been set at url var
if ($_GET["lang_change"]) {
$_SESSION['session_name']["lang"] = $_GET["lang_change"];
}
// set value to lang for verification
$active_lang = $_SESSION['session_name']["lang"];
// verify $lang content and set proper file to be load
switch ($active_lang) {
case 'prt':
$lang_file = 'prt.php';
break;
case 'gbr':
$lang_file = 'gbr.php';
break;
case 'fra' :
$lang_file = 'fra.php';
break;
case 'esp' :
$lang_file = 'esp.php';
break;
case 'deu' :
$lang_file = 'deu.php';
break;
default:
$lang_file = 'gbr.php';
}
// load proper language file for site presentation
include_once ('$lang_file);
?>
LANG GBR FILE (gbr.php)
define("LANG_PAGETITLE_HOMEPAGE", 'Homepage');
define("LANG_BTN_KNOW_MORE", 'know more');
METHOD TO CHANGE LANGUAGE (url sample)
USE ENG
Basically, you have PHP files with constants, each file with a lang.
On click you set a url var (ex: lang_change = lang).
That will force page reload, and the langs.php file include at top of your index.php will load the selected language...
If you need more explanation about this, leave a comment and I'll send you a working sample!
Ps: session variables shown in this code is usefully for interaction with login systems, or just to avoid having the url parameters...
Save all dynamic content flagged with actual language
Make use of gettext() for buttons, etc. This one is much faster than including .php files with arrays
First of all you have to add all values in each language dynamically.
While adding dynamic content to your website, u can add languageId to each field of your tables in database. And then you can show that content at front end on behalf of that languageId.
I think it's a good idea to consider working with a framework that has internationalization support.
Take a look at this example using CakePHP http://bakery.cakephp.org/articles/view/p28n-the-top-to-bottom-persistent-internationalization-tutorial
I think the following will help you get some basic idea of developing it.
In a website, specially a multilingual website should have user interfaces / templates where hardcoded labels should be linked to variables. These variables should be loaded with correct language values. This can be done easily by including the language file containing the values in that specific language. You can have as many language files in a folder.
You will need to write a script in php, as whenever the user selects the language from the drop down, the page can reload with a language session. Another php script to fetch the selected language inside this session data and include the relevant language file inside the template/UI.
The same approach can be used in fetching content data from a table, where in all MySQL queries, you can use an additional lookup for language type from the content table. so that that file will be loaded.
SELECT * FROM posts WHERE lang='en' AND featured = 1
In many cases, the languages require HTML and CSS to be set accordingly to make the language render perfectly inside the browser. This means, you can also define language inside the HTML and in CSS define the fonts and directions (right to left or left to right).
I am recommending you to read the following in order to get more information on how to do it.
http://www.stylusinc.com/website/multilanguage_support.htm

Categories