【问题标题】:Codeigniter's form_dropdown to display category name in dropdown list but insert category idCodeigniter form_dropdown 在下拉列表中显示类别名称但插入类别 ID
【发布时间】:2014-04-29 06:00:17
【问题描述】:

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

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

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

更新:记住表单应该在博客表中插入猫 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>

【问题讨论】:

  • 你不能在这样的数组声明中抛出一个 foreach。
  • 你好 xd6。我该怎么做?

标签: php mysql codeigniter


【解决方案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
}

【讨论】:

  • 遇到 PHP 错误严重性:警告消息:为 foreach() 提供的参数无效
  • 和另一个错误遇到了 PHP 错误严重性:通知消息:未定义变量:类别
  • 检查 $categories 是否包含值以及您如何传递类别。也分享控制器代码。
  • 请看我的控制器
【解决方案2】:

如果你愿意,你可以做..

<?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

【讨论】:

  • 遇到 PHP 错误严重性:通知消息:未定义变量:类别文件名:views/add_blog_view.php 行号:34 遇到 PHP 错误严重性:警告消息:为 foreach() 提供的参数无效
  • @Robin,你能做 var_dump($categories) 并告诉我你得到了什么吗?并包括第 30-40 行的内容,错误所在。
  • @Robin - 您是否确保在加载此视图的控制器中设置了 $data['categories'] ?它在视图中转换为 $categories。
  • 你好丑埃迪检查stackoverflow.com/questions/23316093/…这些行
  • 控制器在上面添加
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多