私のコントローラーコード
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('welcome_model');
}
public function index() {
$data = array();
$data['category'] = $this->welcome_model->select_category();
$data['product'] = $this->welcome_model->select_product();
$this->load->view('purchase_entry', $data);
}
function get_products($category) {
header('Content-Type: application/x-json; charset=utf-8');
echo(json_encode($this->welcome_model->select_product_by_category($category)));
}
public function add() {
$result = $this->welcome_model->select_product_by_id($this->input->post('product_id', TRUE));
$insert = array(
'id' => $this->input->post('product_id', true),
'qty' => $this->input->post('quantity', true),
'price' => $this->input->post('unit_price', true),
'name' => $result->product_name
);
$this->cart->insert($insert);
redirect("welcome/index");
}
}
私のモデルクラス
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome_Model extends CI_Model {
public function select_category()
{
$this->db->select('*');
$this->db->from('category');
$query_result= $this->db->get();
$result=$query_result->result();
return $result;
}
public function select_product()
{
$this->db->select('*');
$this->db->from('product');
$query_result= $this->db->get();
$result=$query_result->result();
return $result;
}
public function select_product_by_category($category_id)
{
$this->db->select('*');
$this->db->from('product');
$this->db->where('category_id',$category_id);
$query_result= $this->db->get();
$result=$query_result->result();
return $result;
}
public function select_product_by_id($product_id)
{
$this->db->select('*');
$this->db->from('product');
$this->db->where('product_id',$product_id);
$query_result= $this->db->get();
$result=$query_result->row();
return $result;
}
}
?>
私のビューファイルは
<html>
<head>
<title></title>
<script type="text/javascript" src="<?php echo base_url(); ?>js/jquery-1.7.1.min.js"></script>
<script>
$(document).ready(function(){
$('#category_id').change(function(){
$("#product_id > option").remove();
var category_id = $('#category_id').val();
$.ajax({
type: "POST",
url: "http://localhost/test_cart/index.php/welcome/get_products/"+category_id,
success: function(cities)
{
$.each(cities,function(id,name)
{
var opt = $('<option />');
opt.val(id.product_id);
opt.text(name.product_name);
$('#product_id').append(opt);
});
}
});
});
});
</script>
<style type="text/css">
a{text-decoration: none;}
</style>
</head>
<body>
<form action="<?php echo base_url(); ?>welcome/add" method="post">
<table width="400px" align="center">
<caption><h3>Purchase Invoice</h3></caption>
<tr>
<td>Invoice Date :</td>
<td>
<input type="text" name="invoice_date" value=""/>
</td>
</tr>
<tr>
<td>Select Category :</td>
<td>
<select name="category_id" id="category_id">
<option>--- Select Category ---</option>
<?php foreach ($category as $aCategory) { ?>
<option value="<?php echo $aCategory->category_id; ?>"><?php echo $aCategory->category_name; ?></option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td>Select Product :</td>
<td>
<select name="product_id" id="product_id">
<option>--- Select Product ---</option>
</select>
</td>
</tr>
<tr>
<td>Unit Price :</td>
<td>
<input type="text" name="unit_price" value=""/>
</td>
</tr>
<tr>
<td>Quantity :</td>
<td>
<input type="text" name="quantity" value=""/>
</td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" value="Submit"/>
</td>
</tr>
</table>
</form>
<form action="" method="post">
<h2 align="center">Invoice Details</h2>
<table border="1px" cellpadding="5px" cellspacing="1px" style="margin-top:10px;border-collapse:collapse" width="500px" align="center">
<tr align="center" style="font-family:Arial, Helvetica, sans-serif; font-size:12px;">
<th>Serial</th>
<th>Product Name</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Amount</th>
<th>Action</th>
</tr>
<?php if ($cart = $this->cart->contents()) ?>
<?php
$i = 1;
foreach ($cart as $item) {
?>
<tr>
<td><?php echo $i++; ?></td>
<td><?php echo $item['name']; ?></td>
<td><?php echo $item['qty']; ?></td>
<td><?php echo $item['price']; ?></td>
<td><?php echo $item['subtotal']; ?></td>
<td>
<a href="">Update</a> | <a href="">Remove</a>
</td>
</tr>
<?php } ?>
</table>
</form>
</body>
私の問題は、製品をカートに追加したいときに、コントローラーの add メソッドが投稿からパラメーターを見つけることができなかったことです。つまり、product_id がビュー値から渡されていません。オプション値をビューからコントローラーにパラメーターとして渡すにはどうすればよいですか?