Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a variable from view to controller in codeigniter

I want to pass a language id for each link i click from my view to controller. My view code is

  <?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 is

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 languages array contain the language id and language name

i want the name to be in url but not the id

The url looks like this

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

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

Thanks

like image 250
Faryal Khan Avatar asked Mar 22 '12 11:03

Faryal Khan


People also ask

How can we pass data from view to controller?

Pass value from view to controller using Parameter In MVC we can fetch data from view to controller using parameter. In MVC View we create html control to take input from the user. With the help of name element of html control we can access these data in controller.

How do you pass data from controller to view and from view to controller?

The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.

How pass data from one page to another in CodeIgniter?

call first controller from first view and pass form data to second view. On second view you can create hidden inputs and set their values from controller data. Now submit the second form to final controller and you will get all the values of both form. Hope this helps you.


2 Answers

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

Lets assume you have 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 then have a variable with object associated to it then you can simply 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);
like image 52
NDBoost Avatar answered Sep 28 '22 15:09

NDBoost


Your confusion is in 'passing back' to the controller. Don't think of it as from controller => View (passing it say $data['something'] variables).

Its basically a <form>, so take a look at form helper and then at form validation. That will give you an idea of how to create a form using codeigniter syntax.

In your controller, you would do a validation, and if it matches the language (or whatever you submit), then you can utilize sessions to save it for every page (so you don't need it in the URL).

Sessions are very simple and saving an item is as easy 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;
like image 43
Jakub Avatar answered Sep 28 '22 17:09

Jakub