Php 尝试在CodeIgniter中创建产品类别上载的选择列表
我正在制作一个网站,以便用户可以向网站添加产品。在用户可以添加某个产品的表单上,我正在尝试创建一个选择下拉栏,以便用户可以选择属于某个产品的某个类别,但我不确定如何才能做到这一点Php 尝试在CodeIgniter中创建产品类别上载的选择列表,php,codeigniter,drop-down-menu,codeigniter-3,Php,Codeigniter,Drop Down Menu,Codeigniter 3,我正在制作一个网站,以便用户可以向网站添加产品。在用户可以添加某个产品的表单上,我正在尝试创建一个选择下拉栏,以便用户可以选择属于某个产品的某个类别,但我不确定如何才能做到这一点 Database info: category table: categories Rows in the category table: 1. id 2. name In my products table I also have a row called: catego
Database info:
category table: categories
Rows in the category table:
1. id
2. name
In my products table I also have a row called: category_id
这是我的db助手文件(db_helper.php):
这是我的产品模型文件,我在其中创建了get_categories函数:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Product_model extends CI_model {
public function saveProduct($data) {
$this->db->insert('products', $data);
$product_id = $this->db->insert_id();
return $product_id;
}
public function get_product_details($product_id) {
$arrReturn = array();
$this->db->select('*');
$this->db->from('products');
$this->db->where('product_id', $product_id);
$query = $this->db->get();
$result = $query->result_array();
if (!empty($result)) {
$arrReturn = $result[0];
}
return $arrReturn;
}
/*
Get categories
*/
public function get_categories(){
$this->db->select('*');
$this->db->from('categories');
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
}
?>
这是我的视图窗体,我试图在其中设置类别的选项选择栏:
<?php echo form_open_multipart('Product/upload'); ?>
<table class="aanbieding-cadeau">
<tr>
<td>
<?php echo form_input(array('id'=>'product_naam', 'name'=>'product_naam', 'placeholder' => '1. Naam van het cadeau', 'size'=>25));?>
</td>
</tr>
<tr>
<?php foreach (get_categories_h() as $category) : ?>
<select name="category">
<a href="#"><?php echo $category['name']; ?></a>
</select>
<?php endforeach; ?>
</tr>
<tr>
<td>
<?php echo form_input(array('id'=>'ophaal_plaats', 'name'=>'ophaal_plaats', 'placeholder' => '3.Kies een stad', 'size'=>25));?>
</td>
</tr>
<div class="checkbox">
<label><input type="checkbox" value="">Gebruik adres van mijn account</label>
</div>
<tr>
<td>
<h4>Upload foto</h4>
<input type="file" name="userfile" />
</td>
</tr>
<tr>
<td>
<?php echo form_textarea(array('type'=>'textarea','id'=>'product_beschrijving', 'name'=>'product_beschrijving', 'placeholder' => '5. Vertel iets over dit cadeau..', 'size'=>25));?>
</td>
</tr>
<tr>
<td>
<input type="submit" class="btn btn-primary" name="submit" value="Cadeau aanbieden!" />
</td>
</tr>
</table>
</form>
Gebruik adres van mijn账户
上传foto
当我加载视图表单时,我没有看到一个下拉类别选项菜单,但我看到10个小的选择栏,当你点击它们时,它们是空的。(注:我已经在数据库中插入了10个类别)
我希望有人能帮助我问题在这里:
<?php foreach (get_categories_h() as $category) : ?>
<select name="category">
<a href="#"><?php echo $category['name']; ?>
</a>
</select>
<?php endforeach; ?>
问题在于:
<?php foreach (get_categories_h() as $category) : ?>
<select name="category">
<a href="#"><?php echo $category['name']; ?>
</a>
</select>
<?php endforeach; ?>
将选择标记代码更改为:
<tr>
<select name="category">
<?php foreach (get_categories_h() as $category) : ?>
<option ><?php echo $category['name']; ?></option>
<?php endforeach; ?>
</select>
</tr>
注:
1) Select
标记必须位于循环外侧
2) 使用
选项
选择标记不锚定
**将选择标记代码更改为:
<tr>
<select name="category">
<?php foreach (get_categories_h() as $category) : ?>
<option ><?php echo $category['name']; ?></option>
<?php endforeach; ?>
</select>
</tr>
注:
1) Select
标记必须位于循环外侧
2) 使用
选项
选择标记不锚定
***此外,您现在是否知道如何在您选择类别时插入我的类别id行?此外,您现在是否知道如何在您选择类别时插入我的类别id行?