0

这是我的代码片段:

<?php

if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}

class sample{

function __construct() {
    $this->ci = & get_instance();
    }

public $name;
public $style;


function set_data($data)
{
    /* List of parameters that you can set */

    $this->name = (isset($data['name']) ? $data['name']: ''); // Set select name
    $this->style = (isset($data['style']) ? $data['style']: ''); // Set select style

}

function select_both_dropdown()
{
    $select = '<select name="'.$this->name.'" class="chzn-select" style="'.$this->style.'">';
    $select .= '<option value=""></option>';
    $select .= '</select>';
return $select;
}

控制器:

$data['select'] = $this->sample->select_both_dropdown(array(
        'name' => 'eventselect',
        'style' => 'min-width: 247px;'
    ));

它是如何在控制器中加载的:function _construct( ) { parent:: _construct(); $this->load->library('tank_auth'); $this->load->library('sample'); }

当屏幕上的“选择负载”上没有填充。没有名称,没有样式等。我在做什么错?

4

1 回答 1

2

您需要正确引用它们;

在图书馆;

// as you have done
$this->select = 'foo';

在您的控制器中,它将类似于$this->sample->name$this->sample->style

但是 $select 不是属性或被返回,因此将不可用。

更新 - 基于评论控制器;

$this->sample->set_data(array(
        'name' => 'eventselect',
        'style' => 'min-width: 247px;'
    ));
$data['select'] = $this->sample->select_both_dropdown();

另一个更新

function select_both_dropdown($data)
{
    $this->set_data($data);
    $select = '<select name="'.$this->name.'" class="chzn-select" style="'.$this->style.'">';
    $select .= '<option value=""></option>';
    $select .= '</select>';
return $select;
}

然后我会将 set_data() 更改为私有而不是公共。

于 2012-05-23T14:53:36.987 回答