我正在学习代码点火器,我正在尝试创建一个可以在多个表上工作的购物车。我可以使用1个表,但是当我尝试JOIN方法时,它总是给出“产品不存在”。
我正在使用的模型是:
class Cart_model extends CI_Model {
function tvs(){
$query = $this->db->get('tvs');
return $query->result_array();
}
function computers(){
$query = $this->db->get('computers');
return $query->result_array();
}
function laptops(){
$query = $this->db->get('laptops');
return $query->result_array();
}
function validate_update_cart(){
$total = $this->cart->total_items();
$item = $this->input->post('rowid');
$qty = $this->input->post('qty');
for($i=0;$i < count($item);$i++)
{
$data = array(
'rowid' => $item[$i],
'qty' => $qty[$i]
);
$this->cart->update($data);
}
}
function validate_add_cart_item(){
$id = $this->input->post('product_id'); // Assign posted product_id to $id
$qty = $this->input->post('quantity'); // Assign posted quantity to $cty
$this->db->select('*');
$this->db->from('tvs');
$this->db->join('computers', 'computer.id = tv.id and computer.name = tv.name and computer.price = tv.price');
$this->db->join('laptops', 'laptops.id = tv.id and laptops.name = tv.name and laptops.price = tv.price');
$this->db->where('tvs.id', $id);
$query = $this->db->get();
return $query->result();
}
foreach($this->cart->contents() as $item) {
if($item['id'] == $id) {
$data = array(
'rowid' => $item['rowid'],
'qty' => $item['qty'] + $qty
);
$this->cart->update($data);
return TRUE;
}
}
$row = $query->row();
$data = array(
'id' => $id,
'qty' => $qty,
'price' => $row->price,
'name' => $row->name
);
this->cart->insert($data);
return TRUE;
{
}else{
return FALSE;
}
}
}
这是项目清单:
<ul class="products">
<?php foreach($TV as $p): ?>
<li>
<h1>
<a href="<?php echo current_url();?>/
<?php echo $p['link']; ?>" />
<?php echo $p['name']; ?>
</a>
</h1>
<img src="<?php echo base_url(); ?>assets/image/products/<?php echo $p['image']; ?>" alt="<?php echo $p['name']; ?>" />
<h3>
<?php echo $p['description']; ?>
</h3>
<small>£<?php echo $p['price']; ?></small>
<?php echo form_open('/testcart/home/add_cart_item'); ?>
<fieldset>
<label>Quantity</label>
<?php echo form_input('quantity', '1', 'maxlength="2"'); ?>
<?php echo form_hidden('product_id', $p['id']); ?>
<?php echo form_submit('add', 'Add to Cart'); ?>
</fieldset>
<?php echo form_close(); ?>
</li>
<?php endforeach;?>
</ul>`
控制器:
function add_cart_item(){
if($this->cart_model->validate_add_cart_item() == TRUE){
if($this->input->post('ajax') != '1'){
redirect('');
}else{
echo 'true';
}
}
}
javascript
$(document).ready(function() {
var link = baseurl;
$("ul.products form").submit(function() {
// Get the product ID and the quantity
var id = $(this).find('input[name=product_id]').val();
var qty = $(this).find('input[name=quantity]').val();
$.post(link + "home/add_cart_item", { product_id: id, quantity: qty, ajax: '1' },
function(data){
if(data == 'true'){
$.get(link + "home/show_cart", function(cart){
$("#cart_content").html(cart);
});
}else{
alert("Product does not exist");
}
});
return false;
});
$(".empty").live("click", function(){
$.get(link + "home/empty_cart", function(){
$.get(link + "home/show_cart", function(cart){
$("#cart_content").html(cart);
});
});
return false;
});
});
我不太确定哪里出错了,因为我对这一切都比较新,无论是Javascript还是PHP。
答案 0 :(得分:0)
这是您的无错误方法。你错误地从桌面电视中选择并在连接中使用电视
function validate_add_cart_item()
{
$id = $this->input->post('product_id'); // Assign posted product_id to $id
$qty = $this->input->post('quantity'); // Assign posted quantity to $cty
$this->db->select('*');
$this->db->from('tvs as tv');
$this->db->join('computers', 'computer.id = tv.id and computer.name = tv.name and computer.price = tv.price');
$this->db->join('laptops', 'laptops.id = tv.id and laptops.name = tv.name and laptops.price = tv.price');
$this->db->where('tv.id', $id);
$query = $this->db->get();
return $query->result();
}