How to pass a variable from view to controller in codeigniter

I want to pass the language identifier for each link that I click from my view to the controller. My view code

<?php foreach ($languages as $lang) { ?> <li> <a href="<?php echo base_url(); ?>home/box/<?php echo $template_data['box_id']?>/<?php echo $lang['language_name']?>"></a> </li> <?php } ?> 

My controller

 public function box($box_id=null, $language_name=null, $language_id=null) { /// my function code echo $box_id; echo $language_name; echo $language_id; $data['languages'] = $this->Home_model->getLanguages($box_id); } 

The language array contains the language identifier and language name

I want the name to be in url but not id

The URL looks like this:

 http://localhost/mediabox/home/box/12/en 

if I send the language id in the url it is then displayed, otherwise it is not displayed in the controller. How can I get the language id for each link in the controller without sending it to url

thanks

+4
source share
5 answers

pass the language name in the url without id, compare the languag_name column in the table.

Suppose you have a URL: http://localhost/mediabox/home/box/en

controller

 <?php # I wont write a controller but you should know how to do that, im also writing code as if you are just focusing on getting language. public function box( /**pass in your other uri params as needed **/ $lang_name = 'en'){ #you could load this in the constructor so you dont have to load it each time, or in autoload.php if your using it site wide. $this->load->model('lang_model', 'langModel'); #this example shows loading the library and running the function $this->load->library('lang_library'); $this->lang_library->_getLang($lang); #this example shows putting the getLang function inside the controller itsself. self::_getLang($lang); } 

library / private function

 <?php private functon _getLang($lang = 'en'){ #run the query to retrieve the lang based on the lang_name, returns object of lang incl id $lang = $this->langModel->getLang($lang_name); if (!$lang){ die('language not found'); }else{ return $lang; } 

lang model

 <?php public function getLang($lang_name = 'en'){ $this->db->where('lang_name', $lang_name); $this->db->limit(1); $q = $this->db->get('languages'); if ($q->mysql_num_rows > 0){ return $q->result(); }else{ return false; } } 

you will have a variable with an object associated with it, then you can just call $lang->lang_name; or $lang->lang_id;

Session Storage

 <?php #you could call this in the beginning after using an ajax `$.post();` to retrieve the ID.. the easiest route though is whats above. I use this in my REST APIs $this->session->set_userdata('lang', $lang); 
+4
source

Your confusion is to "return" to the controller. Do not think of it as from controller => View (passing it says $data['something'] variables).

This is basically a <form> , so look at the form helper , and then check the form . This will give you an idea of ​​how to create a form using codeigniter syntax.

In your controller, you do a check, and if it matches the language (or whatever you send), you can use sessions to save it for each page (so you don't need this in the url).

Sessions are very simple and saving an element is as simple as:

 $this->session->set_userdata('varname', 'value'); 

Later, on every other controller, you can check the variable

 $language = $this->session->userdata('varname'); // load language etc; 
+3
source

Make a table with the language identifier and the language name in the database, just pass the language name to the controller and get the language identifier by calling db.

+1
source

You can do this with jQuery. Sort of:

In view

 <ul id="language_selector"> <?php foreach ($languages as $lang) { ?> <li> <a href="javascript:;" class="change-language" data-language="<?php echo $lang['language_name']?>" data-box="<?php echo $template_data['box_id']?>"> <img src="<?php echo base_url(); ?>public/default/version01/images/country_<?php echo $lang['language_name'] ?>.png" width="27" height="18" border="0" /> </a> </li> <?php } ?> </ul> 

JS;

 $(function() { $('.change-language').live('click', function() { var language_name = $(this).data('language'); var box_id = $(this).data('box'); $.ajax({ url: '/home/box/'+language_name, type: 'post', data: 'box_id='+box_id, success: function( data ) { self.parent.location.reload(); }, error: function( data ) { alert('oops, try again'); } }); }); }); 

Controller:

 public function box($language) { $box_id = $this->input->post('box_id'); // do a llokup on the language as suggested by @vivek } 
+1
source

You tell CodeIgniter that in your action you will get the name and language identifier.

 public function box($box_id=null, $language_name=null, $language_id=null) { } 

Change it to

 public function box($box_id=null, $language_name=null) { } 

For your example url, you should get $ box_id == 12 and $ language_name == 'en'.

Then find the language identifier using its name either in the helper or as part of the language model, as Mike suggests.

0
source

Source: https://habr.com/ru/post/1402866/


All Articles