Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 编码点火器';s form_下拉列表,在下拉列表中显示类别名称,但插入类别id_Php_Mysql_Codeigniter - Fatal编程技术网

Php 编码点火器';s form_下拉列表,在下拉列表中显示类别名称,但插入类别id

Php 编码点火器';s form_下拉列表,在下拉列表中显示类别名称,但插入类别id,php,mysql,codeigniter,Php,Mysql,Codeigniter,我想在CI中的博客模块中显示类别列表的下拉列表 在Blog表中: 我有news\u id、category\u id、news\u title、news\u slug 在类别表中: 我有category id、category name、category slug 更新:记住表单应该在blog表中插入cat id,但显示另一个表中的类别名称 新闻类别* 而不是 <?php $options = array( '' => 'Select category', f

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

在Blog表中: 我有
news\u id、category\u id、news\u title、news\u slug

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

更新:记住表单应该在blog表中插入cat id,但显示另一个表中的类别名称


新闻类别*
而不是

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

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

如果你愿意,你可以做

<?php

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

?>

有关更多信息,请选中此项

您不能在这样的数组声明中抛出foreach。您好xd6。我应该怎么做?遇到PHP错误严重性:警告消息:为foreach()提供的参数无效,另一个错误遇到PHP错误严重性:注意消息:未定义变量:categories检查$categories是否包含值以及如何传递类别。还共享控制器代码。请查看我的controllerA遇到的PHP错误严重性:注意消息:未定义变量:类别文件名:views/add_blog_view.PHP行号:34遇到PHP错误严重性:警告消息:为foreach()@Robin提供的参数无效,是否可以执行var_dump($categories)告诉我你得到了什么?还包括第30-40行中的错误。@Robin-您是否确保在加载此视图的控制器中设置了$data['categories']?它在视图中转换为$categories。hello丑陋的Eddie检查这些线条控制器添加在上面
<?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>
<?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

$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