php 如何在codeigniter中将变量从视图传递给控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9821542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:42:23  来源:igfitidea点击:

How to pass a variable from view to controller in codeigniter

phpcodeigniter

提问by Faryal Khan

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

我想将我从视图中单击的每个链接的语言 ID 传递给控制器​​。我的视图代码是

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

语言数组包含语言 ID 和语言名称

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

我希望名称在 url 中,但不是 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

如果我在 url 中发送语言 ID,则它是可见的,否则它在控制器中不可见。如何获取控制器中每个链接的语言 ID 而不在 url 中发送它

Thanks

谢谢

回答by gorelative

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

在没有 ID 的 url 中传递语言名称,与表中的 languag_name 列进行比较。

Lets assume you have url: http://localhost/mediabox/home/box/en

让我们假设你有网址: 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;

然后,您将拥有一个与对象关联的变量,然后您可以简单地调用$lang->lang_name;$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);

回答by Jakub

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

您的困惑在于“传回”给控制器。不要认为它来自控制器 => 视图(传递它说$data['something']变量)。

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

它基本上是一个<form>,所以看看表单助手然后表单验证。这将使您了解如何使用 codeigniter 语法创建表单。

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

在您的控制器中,您将进行验证,如果它与语言(或您提交的任何内容)匹配,那么您可以利用会话为每个页面保存它(因此您不需要在 URL 中使用它)。

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

回答by Rooneyl

You could do it using jQuery. Something like;

你可以使用 jQuery 来做到这一点。就像是;

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;

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
}

回答by vivek

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.

在数据库中创建一个带有语言 ID 和语言名称的表,只需将语言名称传递给控制器​​并通过执行 db 调用获取语言 ID。

回答by Dan Brown

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

您告诉 CodeIgniter 您将在操作中同时收到语言名称和 ID。

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'.

对于您的示例 URL,您应该得到 $box_id == 12 和 $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.

然后按照 Mike 的建议,通过在助手中或作为语言模型的一部分使用它的名称来查找语言 ID。