php - Trying to make a select list for product category upload in CodeIgniter -
i'm making website users can add products website. on form users can add product i'm trying make select dropdown bar users can select category belongs product i'm not sure how can that..
database info: category table: categories rows in category table: 1. id 2. name in products table have row called: category_id
this db helper file (db_helper.php) :
<?php if (!function_exists('get_categories_h')) { function get_categories_h(){ $ci = get_instance(); $categories = $ci->product_model->get_categories(); return $categories; } } ?>
this product_model file made get_categories function:
<?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; } /* categories */ public function get_categories(){ $this->db->select('*'); $this->db->from('categories'); $query = $this->db->get(); $result = $query->result_array(); return $result; } } ?>
this view form i'm trying make option select bar of categories:
<?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() $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 on dit cadeau..', 'size'=>25));?> </td> </tr> <tr> <td> <input type="submit" class="btn btn-primary" name="submit" value="cadeau aanbieden!" /> </td> </tr> </table> </form>
when load view form don't see 1 dropdown category option menu see 10 small selectbars empty when click on them. ( ps: have inserted 10 categories database)
i hope can me
the issue here:
<?php foreach (get_categories_h() $category) : ?> <select name="category"> <a href="#"><?php echo $category['name']; ?> </a> </select> <?php endforeach; ?>
you have put select
tag inside loop, , a
tag not part of select
. put option
inside loop , exclude select
loop.
change code to:
<select name="category"> <?php foreach (get_categories_h() $category) : ?> <option><?php echo $category['name']; ?></option> <?php endforeach; ?> </select>
Comments
Post a Comment