PHP没有传递数据数组?

时间:2012-09-02 23:45:48

标签: php arrays codeigniter

我正在使用CodeIgniter工作,无法弄清楚为什么我的函数没有返回数组,这应该很简单。

我在返回之前在模型中进行了转储,只是为了查看它是否已经格式化并且有数据(确实如此)。问题似乎是在控制器中,函数没有返回数据,并且在视图属性中为null。

可能导致此问题的CI或PHP会发生什么?

Controller: 
function galleryManagement()
{
    if (! $this->session->userdata('is_admin'))
    {
        redirect('admin/index');
    }
    else
    {
        /*
        $selectedProperty = $this->input->post('selectedProperty');
        switch ($selectedProperty)
        {
            case "westgate":
                $selectedID = '1';
                break;
            case "prospector436":
                $selectedID = '2';
                break;
            case "prospector437":
                $selectedID = '3';
                break;
            case "prospectorcombo":
                $selectedID = '4';
                break;
            default:
                $selectedID = '-1';
        }
        if ($selectedID != '-1')
        {
        */
            $this->load->model('admin_model');
            $propertyObj = $this->admin_model->getProperties();
            $data['properties'] = $propertyObj;
            $data['propertyID'] = $selectedID;
            $data['galleryObj'] = $this->admin_model->getGallery($selectedProperty);
            //$data['textarea'] = array ( 'name' => 'altText', 'style' => "width: 320px; height: 36px;" );
            $this->load->view('db_site/admin_gallery_view', $data);
        /*
        }
        else
        {
            $this->load->model('admin_model');
            $data['propertiesObj'] = $this->admin_model->getProperties();
            $data['propertyID'] = $selectedID;
            $this->load->view('db_site/admin_gallery_view', $data);
        }
        */
    }
}

Model:
function getProperties()
{
    $this->db->select('property_id, name');
    $this->db->from('property');
    $q = $this->db->get();

    if ($q->num_rows != 0)
    {
        $propertiesObj = $q->result();
        foreach ($propertiesObj as $property)
        {
            $properties[$property->name] = $property->property_id;
        }
        echo "Model Dump: ";
        var_dump($properties);
        return $properties;
    }
    else
    {
        return false;
    }
}

View:
<?php
    var_dump($properties);
    echo form_open('admin/galleryManagement');
    echo "<p>Property: " . form_dropdown('selectedProperty', $properties) . "</p>";        
    echo form_submit('select', 'Select');
?>

1 个答案:

答案 0 :(得分:2)

在您的视图中,您使用的$properties未在控制器的$data其他数组块中传递。如果您更改控制器的else块,如下所示它将起作用。

    else
    {
        $this->load->model('admin_model');
        $data['properties'] = $this->admin_model->getProperties();
        $data['propertyID'] = $selectedID;
        $this->load->view('db_site/admin_gallery_view', $data);
    }

修改

从您最新的编辑中看起来您已经评论了我正在讨论的else块。你仍然遇到问题??