我有一個由我的數據庫填充的下拉列表,所以我想要的是如果用戶想要添加另一個項目,但無法在下拉列表中找到該類別,他會從列表中選擇 「其他」,然後將創建一個新的類別爲特定項目Codeigniter如果添加textarea如果在下拉列表中選擇了某個值
視圖
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Add Item</title>
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/bootstrap-theme.min.css" rel="stylesheet">
<link rel="shortcut icon" href="assets/ICON.PNG">
<link href='https://fonts.googleapis.com/css?family=Raleway:200' rel='stylesheet' type='text/css'>
</head>
<body>
<div class="container">
<div class="row" >
<div class="col-md-10">
<br>
<div class="panel panel-default">
<div class="panel-body">
<div class="panel panel-default">
<div class="panel-body">
<?php echo form_open_multipart('add_products/do_upload'); ?>
<center> <?php if (validation_errors()): ?>
<div class="alert alert-danger alert-dismissible" role="alert" style="width: 700px;">
<?php echo validation_errors(); ?>
</div>
<?php endif ?>
<div class="form-group">
<label class="col-sm-2 control-label" style=" color: white"></label>
<label class="col-sm-2 control-label">Product Image</label>
<div class="col-sm-5">
<input type="file" class="form-control" placeholder="" name="userfile">
</div>
</div>
<br> <br>
<br>
<div class="form-group">
<label class="col-sm-2 control-label" style=" color: white"></label>
<label class="col-sm-2 control-label">Product Name</label>
<div class="col-sm-5">
<input type="text" class="form-control" placeholder="ex. coffee"
name="name"
value="<?php echo set_value('name'); ?>">
</div>
</div>
<br> <br>
<div class="form-group">
<label class="col-sm-2 control-label" style=" color: white"></label>
<label class="col-sm-2 control-label">Description</label>
<div class="col-sm-5">
<input type="text" class="form-control" placeholder="ex. brewed drink from roasted coffee"
name="description"
value="<?php echo set_value('description'); ?>">
</div>
</div>
<br> <br>
<div class="form-group">
<label class="col-sm-2 control-label" style=" color: white"></label>
<label class="col-sm-2 control-label">Price</label>
<div class="col-sm-5">
<input type="number" class="form-control" placeholder="ex. P180"
name="price"
value="<?php echo set_value('price'); ?>">
</div>
</div>
<br> <br>
<div class="form-group">
<label class="col-sm-2 control-label" style=" color: white"></label>
<label class="col-sm-2 control-label">Category</label>
<div class="col-sm-5">
<select class="form-control" name="prod_category">
<?php
foreach($category as $row)
{
echo '<option value="'.$row->category.'">'.$row->category.'</option>';
}
?>
<option>Other</option>
</select></div>
</div>
<br> <br>
<button style="margin-left: 75px; width: 320px;" type="submit" class="btn btn-success">Submit</button>
</form>
<?php echo form_close() ?></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
控制器」
function index()
{
$this->load->helper(array('form'));
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$data['category'] = $this->User->get_category();
$this->load->view('add_products',$data);
}
型號:
public function get_category(){
$p = $this->db->query("select category from product_category");
$p1 = $p->result();
return $p1;
}
我在哪裏放最後的代碼? 我試過你的代碼,但是當我插入一個現有的類別時,它返回0,但其他類別正常工作 – Christian
@Christian最後的代碼把它提交到你的控制器,你提交表單 並插入這個$ this-> db-> insert( 「product_category」,array(「category」=> $ category)) –