Faryal Khan
Faryal Khan

Reputation: 869

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

Upvotes: 5

Views: 23050

Answers (5)

Dan Brown
Dan Brown

Reputation: 111

You are telling CodeIgniter that you will be receiving both the language name and id in your action.

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

Change that to just

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

For your example URL you should then get $box_id == 12 and $language_name == 'en'.

Then lookup the language id by using it's name either in a helper or as part of a Language model as Mike suggests.

Upvotes: 0

NDBoost
NDBoost

Reputation: 10634

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);

Upvotes: 5

Rooneyl
Rooneyl

Reputation: 7902

You could do it using jQuery. Something like;

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>

The 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');
            }
        });
    });
});

The controller:

public function box($language) {
    $box_id = $this->input->post('box_id');

    // do a llokup on the language as suggested by @vivek
}

Upvotes: 1

Jakub
Jakub

Reputation: 20475

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;

Upvotes: 3

vivek
vivek

Reputation: 2004

Make a table with language ID and Language name in the database, just pass the language name to the controller and get the language ID by doing a db call.

Upvotes: 2

Related Questions