简体   繁体   English

如何在控制器和视图Codeigniter之间传递参数

[英]How pass parameters between controller and view Codeigniter

My parameter is an array: 我的参数是一个数组:

Controller: 控制器:

$data=......;
$this->load->view('a/p/l',$data);

The data vector has like parameter: 数据向量具有相似的参数:

  0 => 
    array (size=4)
 'email' => string '' (length=21)
      ...
  1 => 
    array (size=4)
 'email' => string '' (length=21)
     ...
  2 => 
    array (size=4)
      'email' => string '' (length=21)

Anyone can show mem some View that I can read the elements in to the array? 任何人都可以向我展示一些View,我可以读取数组中的元素吗?

Here is a simple example 这是一个简单的例子

Here is my controller named welcome.php 这是我的控制器名为welcome.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function index()
    {

        $data['default'] = array(
            array(
                'email' => 'sample@gmail.com',
                'username' => 'username1'
            ),

            array(
                'email' => 'sample@yahoo.com',
                'username' => 'username2'
            ),

            array(
                'email' => 'sample@hot.com',
                'username' => 'username3'
            )
        );

        $data['title'] = 'Sample';

        $this->load->view('welcome_message', $data);
    }

}

In order to call the pass $data array in the views, we make sure that we have a reference key for the array like 为了在视图中调用pass $ data数组,我们确保我们有一个数组的引用键

$data['default'] = array $ data ['default'] =数组

$data['title'] = 'Sample'; $ data ['title'] ='样本';

In order for me to access those data in my view here is a sample view named 为了让我在我的视图中访问这些数据,这里有一个名为的示例视图

welcome_message.php welcome_message.php

<html lang="en">
    <head>

    </head>
    <body>
        <div id="container">
            <?php
                foreach ($default as $key => $value) {
            ?>
            <h1><?php echo $value['email'];?></h1>
            <?php
                }
            ?>

            <h6><?php echo $title;?></h6>

        </div>
    </body>
</html>

To be able to access those data pass, I used the reference key of the pass array 为了能够访问那些数据传递,我使用了传递数组的引用键

default and title 默认和标题

and from there I can do the processing already 从那里我就可以进行处理了

hope It could help you out. 希望它可以帮助你。

Hi the data array's index must be an associative index it must be letters first. 嗨数据数组的index必须是一个关联索引,它必须是字母优先。 CI convert the array as variable in view. CI将数组转换为视图中的变量。

Example: 例:

$data=array('value1'=>12,'value2'=>23)
$this->load->view('a/p/l',$data);

you can now access the values of the passed array by treating the indexes as new variable. 您现在可以通过将索引视为新变量来访问传递的数组的值。

in your view you can get the value of value1 index like this 在您的视图中,您可以像这样获取value1 index的值

echo $value1;

I think it won't work if you use a number as index, it is a basic php rules in variables. 我认为如果你使用数字作为索引它将无法工作,它是变量中的基本php规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM