CodeIgniter中的form_dropdown set_value

时间:2015-01-19 19:34:44

标签: php codeigniter-2

我是CodeIgniter的新手,我在管理面板中使用CodeIgniter中的form_dropdown,但是我无法实时更新set_value。所以你可以帮我解决这个问题吗?

这是我的模特:

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

class Update_Pages_Model extends CI_Model {
    function get_dropdown_sections($id) {
        $sections = $this->db->query('SELECT section_name FROM sections');
        $dropdowns = $sections->result();
        $sections =$this->db->query('SELECT section_name FROM sections WHERE ID_Page = SectionPage');
        $dropdownlist[''] = $sections;
        foreach ($dropdowns as $dropdown) {
            $dropdownlist[$dropdown->section_name] = $dropdown->section_name;
        }
        $finaldropdown = $dropdownlist;
        return $finaldropdown;
    }
    function get_dropdown_categories($id) {
        $categories = $this->db->query('SELECT category_name FROM categories');
        $dropdowns = $categories->result();
        $this->db->query('SELECT section_name FROM sections WHERE ID_Page = CategoryPage');
        $dropdownlist[''] = $categories;
        foreach ($dropdowns as $dropdown) {
            $dropdownlist[$dropdown->category_name] = $dropdown->category_name;
        }
        $finaldropdown = $dropdownlist;
        return $finaldropdown;
    }

    function update_page_id1($id,$data){    
                $this->db->where('ID_Page', $id);
                $this->db->update('pages', $data);
    }
}
?>

这是我的控制器:

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

class update_pages extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->helper('form');
        $this->load->model('update_pages_model');
    }
        function delete_page_id() {
        if($this->session->userdata('logged_in'))
           {
                $session_data = $this->session->userdata('logged_in');
                $data['Username'] = $session_data['Username'];
                $id = $this->uri->segment(3);
                $this->update_pages_model->delete_page_id($id);
                $this->show_page_id();
           }
           else
            {
             $this->load->view('view_error');
            }
    }
    function edit_page_id() {
        if($this->session->userdata('logged_in'))
        {
        $this->load->view('admin/components/nav_menu_dashboard_view');
        $session_data = $this->session->userdata('logged_in');
        $data['Username'] = $session_data['Username'];
        $id = $this->uri->segment(3);
        $data['sections'] = $this->update_pages_model->get_dropdown_sections($id,$data);
        $data['categories'] = $this->update_pages_model->get_dropdown_categories($id,$data);
        $data['pages'] = $this->update_pages_model->show_pages();
        $data['single_page'] = $this->update_pages_model->show_page_id($id);
        $this->load->view('admin/user/view_edit_pages_form', $data);
        }
        else
        {
             $this->load->view('view_error');
        }
    }
    function show_page_id() {
        if($this->session->userdata('logged_in'))
        {
        $this->load->view('admin/components/nav_menu_dashboard_view');
        $session_data = $this->session->userdata('logged_in');
        $data['Username'] = $session_data['Username'];
        $id = $this->uri->segment(3);
        $data['pages'] = $this->update_pages_model->show_pages();
        $data['single_page'] = $this->update_pages_model->show_page_id($id);
        $this->load->view('admin/user/view_edit_pages_form', $data);
        $data['CategoryPage']= $this->update_pages_model->get_dropdown_categories($data);
        }
        else
        {
             $this->load->view('view_error');
        }
    }
    function update_page_id1() {
        if($this->session->userdata('logged_in'))
           {
        $session_data = $this->session->userdata('logged_in');
        $data['Username'] = $session_data['Username'];
        $id= $this->input->post('ID_Page'); 
        $data = array(
            'TitlePage' => $this->input->post('TitlePage'),
            'SectionPage' => $this->input->post('SectionPage'),
            'CategoryPage' => $this->input->post('CategoryPage'),
            'NamePage' => $this->input->post('NamePage'),
            'DescriptionPage' => $this->input->post('DescriptionPage'),
            'BodyPage' => $this->input->post('BodyPage')
        );
        $this->update_pages_model->update_page_id1($id,$data);
        $this->show_page_id();
        redirect('dashboard', 'refresh');
        }
        else
        {
             $this->load->view('view_error');
        }
    }

}
    ?>

这是我的用于编辑表单的View文件:

<?php $this->load->view('admin/components/page_head');?>
<?php foreach ($single_page as $PageName): ?>
<?php 
            $TitlePage = array(
              'name'        => 'TitlePage',
              'id'          => 'TitlePage',
              'maxlength'   => '290',
              'type'         => 'text',
              'size'        => '90',
              'style'       => 'position:relative;min-height:40px;width:100%;',
            );
            $ID_Page = array(
              'name'        => 'ID_Page',
              'id'          => 'hide',
              'maxlength'   => '990',
              'type'         => 'text',
              'size'        => '0',
              'style'       => 'display:none;min-height:0px;width:0%;visibility:hidden',
            );
            $SectionPage = array(
              'name'        => 'SectionPage',
              'id'          => 'SectionPage',
              'maxlength'   => '290',
              'type'         => 'text',
              'size'        => '90',
              'style'       => 'position:relative;min-height:40px;width:100%;',
            );
            $CategoryPage = array(
              'name'        => 'CategoryPage',
              'id'          => 'CategoryPage',
              'maxlength'   => '290',
              'type'         => 'text',
              'size'        => '90',
              'style'       => 'position:relative;min-height:40px;width:100%;',
            );
            $NamePage = array(
              'name'        => 'NamePage',
              'id'          => 'NamePage',
              'maxlength'   => '290',
              'type'         => 'text',
              'size'        => '90',
              'style'       => 'position:relative;min-height:40px;width:100%;',
            );
            $DescriptionPage = array(
              'name'        => 'DescriptionPage',
              'id'          => 'DescriptionPage',
              'maxlength'   => '390',
              'type'         => 'text',
              'size'        => '90',
              'style'       => 'position:relative;min-height:40px;width:100%;',
            );
            $BodyPage = array(
              'name'        => 'BodyPage',
              'id'          => 'BodyPage',
              'type'        => 'text',
              'size'        => '90',
              'cols'        => '80',
              'rows'        => '15',
              'maxlength'   => '3990',
              'aria-hidden' => 'true',
              'style'       => 'position: relative; min-height: 320px; width: 100%;',
            );
            $dsubmit = array(
              'name'        => 'dsubmit',
              'id'          => 'submit',
              'type'         => 'submit',
              'value'        => 'Update',
              'style'       => 'position:relative;min-height:50px;width:100%;max-width:120px;',
              'class'       => 'btn btn-primary',
            );
?>
<div id="main">
<div class="container">
<div class="row">
<div class="col-sm-12">
<?php echo validation_errors(); ?>
<?php echo form_open('/update_pages/update_page_id1'); ?>
<?php   form_label('ID:', 'lbl_ID_Page', 'class="hide_label"', 'id=hide');?>
<?php echo form_input($ID_Page, set_value('ID_Page', $PageName->ID_Page))?>
<?php  echo form_label('Title of page:');?><br/>
<?php echo form_input($TitlePage, set_value('TitlePage', $PageName->TitlePage))?><br/>
<?php  echo form_label('Section of page');?><br/>
<?php 
$Sections = $this->db->query('SELECT SectionPage FROM pages');
echo form_dropdown('SectionPage', $sections)?><br/>
<?php  echo form_label('Category of page');?><br/>
<?php echo form_dropdown('CategoryPage', $categories)?><br/>
<?php  echo form_label('URL of page');?><br/>
<?php echo form_input($NamePage, set_value('NamePage', $PageName->NamePage))?><br/>
<?php  echo form_label('Description of page');?><br/>
<?php echo form_input($DescriptionPage, set_value('DescriptionPage', $PageName->DescriptionPage))?><br/>
<?php  echo form_label('Content of page');?><br/>
<?php echo form_textarea($BodyPage, set_value('BodyPage', $PageName->BodyPage));?><br/><br/>
<?php echo form_submit($dsubmit);?>
<?php echo form_close();?>
</div>
</div>
</div>
</div>
<?php endforeach; ?>

这是我的数据库:

CREATE TABLE `articles` (
  `article_id` int(11) NOT NULL,
  `article_title` varchar(955) NOT NULL,
  `article_url` varchar(955) NOT NULL,
  `publicated_date` date NOT NULL,
  `body_article` varchar(9000) NOT NULL,
  `created_date` date NOT NULL,
  `modified_date` date NOT NULL,
  `article_section` varchar(500) NOT NULL,
  `article_category` varchar(500) NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=2;

CREATE TABLE `categories` (
  `category_id` int(11) NOT NULL,
  `category_name` varchar(255) NOT NULL,
  `category_description` varchar(255) NOT NULL,
  `category_img` varchar(255)  NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=10 ;

CREATE TABLE `pages` (
  `ID_Page` int(11) NOT NULL,
  `TitlePage` varchar(255)  NOT NULL,
  `SectionPage` varchar(255)  NOT NULL,
  `CategoryPage` varchar(255)  NOT NULL,
  `NamePage` varchar(255)  NOT NULL,
  `BodyPage` varchar(9000)  NOT NULL,
  `DescriptionPage` varchar(255)  NOT NULL,
  `parent_id` int(11) unsigned NOT NULL DEFAULT '0',
  `Section_ID` int(11) NOT NULL DEFAULT '0',
  `Category_ID` int(11) NOT NULL DEFAULT '0'
) ENGINE=MyISAM AUTO_INCREMENT=14 ;


CREATE TABLE `sections` (
  `section_id` int(11) NOT NULL,
  `section_name` varchar(255) NOT NULL,
  `section_description` varchar(255)  NOT NULL,
  `section_img` varchar(255)  NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=8;

CREATE TABLE `users` (
  `UserID` int(255) NOT NULL,
  `Username` varchar(65) NOT NULL,
  `Password` varchar(32) NOT NULL,
  `EmailAddress` varchar(255) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3;

目前,我的表单在模型上使用了这些功能:

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

class Update_Pages_Model extends CI_Model {
    function get_dropdown_sections() {
        $sections = $this->db->query('SELECT section_name FROM sections');
        $dropdowns = $sections->result();
        $dropdownlist[''] = 'Please Select';
        foreach ($dropdowns as $dropdown) {
            $dropdownlist[$dropdown->section_name] = $dropdown->section_name;
        }
        $finaldropdown = $dropdownlist;
        return $finaldropdown;
    }
    function get_dropdown_categories() {
        $categories = $this->db->query('SELECT category_name FROM categories');
        $dropdowns = $categories->result();
        $dropdownlist[''] = 'Please Select';
        foreach ($dropdowns as $dropdown) {
            $dropdownlist[$dropdown->category_name] = $dropdown->category_name;
        }
        $finaldropdown = $dropdownlist;
        return $finaldropdown;
    }

如何使用此form_dropdown返回set_value?

2 个答案:

答案 0 :(得分:2)

谢谢兄弟我用这段代码修复我的代码

<?php echo form_dropdown('CategoryPage', $categories, set_value('CategoryPage', $PageName->CategoryPage) );?>

<_>在view_form

答案 1 :(得分:0)

for ($i = 0; $i < $dropdown_count; i++) {
     $this->form_validation->set_rules(
         $dropdown_name[i],
         $dropdown_name[i], 
         "required"
     );
}