gpt4 book ai didi

php - Codeigniter 的 form_dropdown 在下拉列表中显示类别名称,但插入类别 id

转载 作者:行者123 更新时间:2023-11-29 08:02:50 28 4
gpt4 key购买 nike

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

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

在类别表中:我有类别 ID、类别名称、类别 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);

博客 Controller

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
}

}

}

完整代码添加博客 View

<?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(); ?>

类别 View

<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 - Codeigniter 的 form_dropdown 在下拉列表中显示类别名称,但插入类别 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23356656/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com