Codeigniter的form_dropdown用于在下拉列表中显示类别名称,但是插入类别ID

时间:2014-04-29 06:00:17

标签: php mysql codeigniter

我想在CI中的博客模块中显示类别列表的下拉列表。

在博客表格中:  我有news_id, category_id, news_title, news_slug

在类别表中: 我有category id, category name, category slug

更新:请记住,表单应在博客表中插入cat id,但显示另一个表中的类别名称 here is one with more details

<div class="form-group">
        <label for="category">News category <span class="required">*</span></label>
        <?php echo form_error('category_id'); ?>
        <?php $options = array( '' => 'Select category',

         foreach ($categories as $category) {
             # code...
         }
         ); 
         ?>

        <?php $htmlelements = 'class = "form-control" id="subject" required="required"';
        echo form_dropdown('', $options, set_value('category_id'), $htmlelements);

博客控制器

function add()
    {       
        $this->load->library('form_validation');    
        $this->form_validation->set_rules('news_title', 'Blog Title', 'required|trim|xss_clean');           
        $this->form_validation->set_rules('news_body', 'news body', 'required|trim|xss_clean');         

        $this->form_validation->set_rules('category_id', 'Blog category', 'required|trim|xss_clean');

        $this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');

        if ($this->form_validation->run() == FALSE) // validation hasn't been passed
        {


            //$this->load->view('add_blog_view');
            $data['view_file'] = "add_blog_view";
            $this->load->module('template');
            $this->template->public_one_col($data);


        }
        else // passed validation proceed to post success logic
        {
            // build array for the model

            $form_data = array(
//$news_slug = ($this->input->post('title'), 'dash', TRUE);
                            'news_title' => set_value('news_title'),
                            'news_slug' => set_value('news_slug'),
                            'news_body' => set_value('news_body'),
                            'category_id' => set_value('category_id')
                            //'news_slug' => set_value('news_slug')


                        );


            // run insert model to write data to db

            if ($this->mdl_blogs->SaveForm($form_data) == TRUE) // the information has therefore been successfully saved in the db
            {
                redirect('blogs/success');   // or whatever logic needs to occur
            }
            else
            {
            echo 'An error occurred saving your information. Please try again later';
            // Or whatever error handling is necessary
            }

        }

    }

完整代码添加博客视图

<?php // Change the css classes to suit your needs    

$attributes = array('class' => '', 'id' => '');
echo form_open_multipart('blogs/add', $attributes); ?>
<h1>Add a blog </h1>
<div class="form-group">

        <label for="news_title">News Title <span class="required">*</span></label>
        <?php echo form_error('news_title'); ?>

        <?php echo form_input( array( 'name' => 'news_title', 'class' => 'form-control', 'id' =>'news_title', 'required' => 'required','placeholder' => 'Enter a title','rows' => '5', 'cols' => '80', 'value' => set_value('news_title') ) );?>
</div>
<div class="form-group">

        <label for="news_slug">News Slug <span class="required">*</span></label>
        <?php echo form_error('news_slug'); ?>

        <?php echo form_input( array( 'name' => 'news_slug', 'class' => 'form-control', 'id' =>'news_slug', 'required' => 'required','placeholder' => 'Separate each word by underscore','rows' => '5', 'cols' => '80', 'value' => set_value('news_slug') ) );?>
</div>
<div class="form-group">
        <label for="news_body">News <span class="required">*</span></label>
    <?php echo form_error('news_body'); ?>


    <?php echo form_textarea( array( 'name' => 'news_body', 'class' => 'form-control', 'id' =>'newsbody','rows' => '5', 'cols' => '80','placeholder' => 'Write here an article for blog', 'value' => set_value('news_body') ) )?>
</div>

<div class="form-group">
        <label for="category">News category <span class="required">*</span></label>
        <?php echo form_error('category'); ?>

<?php 

$options = array( '' => 'Select category');
foreach ($categories as $catID => $category) {
    $options[$catID] =  $category;                
}

?>

        <?php $htmlelements = 'class = "form-control" id="subject" required="required"';
        echo form_dropdown('news_id', $options, set_value('category_id'), $htmlelements);


       // form_dropdown('category_id', $drop_category_id,$category_id);



        ?>


</div>                                            



        <?php echo "<br/>" . 

          form_submit(array('name' => 'submit', 'class' => 'btn btn-primary', 'id' => 'btnSubmit'), 'Submit'); ?>


<?php echo form_close(); ?>

类别视图

<div class="table-responsive">
              <table class="table table-bordered">
                <thead>
                    <tr>
                        <th class="col-md-2">Category ID</th>
                        <th class="col-md-6">Category</th>
                        <th class="col-md-2">Category Slug</th>
                        <th class="col-md-2">Edit</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                    <?php

                    echo anchor('category/add', '<h2>Add Category</h2>');
                    foreach ($query->result() as $row) {

                    $category_name = $row->category_name;
                    $category_id = $row->category_id;
                    $cat_slug = $row->cat_slug;

                    ?>
                        <td class="col-md-2"><?php echo "<p>".$category_id. "</p>";?></td>
                        <td class="col-md-6"><?php echo "<p>".$category_name. "</p>";?></td>
                        <td class="col-md-2"><?php echo $cat_slug;?></td>
                        <td class="col-md-2">Edit</td>

                        </tr>


                    <?php



                        }



                    ?>

                </tbody>

              </table>
    </div>

2 个答案:

答案 0 :(得分:1)

而不是

<?php $options = array( '' => 'Select category',

         foreach ($categories as $category) {
                         # code...
              }
               ); 
           ?>

试试这个

$options['']="Select category";

foreach($categories as $category)
{
  $options[$category->id]=$category->categoryname // your code
}

答案 1 :(得分:1)

如果你想要,你可以做..

<?php

$options = array( '' => 'Select category');
foreach ($categories as $catID => $category) {
    $options[$catID] =  $category;                
}

?>

要将变量从控制器传递到视图,您需要执行以下操作:

$data['categories'] = $categories;  //you need to assign the categories array here
$this->load->view('add_blog_view', $data); //then pass this to the view

有关详细信息,请查看此link