【问题标题】:Code igniter shopping cart joining and selecting from multiple tablesCodeigniter 购物车加入并从多个表中选择
【发布时间】:2012-08-23 13:53:00
【问题描述】:

我正在学习代码点火器,目前正在尝试创建一个适用于多个表的购物车。我可以使它使用 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>&pound;<?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。

【问题讨论】:

  • 你确实意识到了这个 return $query->result();将退出您的模型功能。之后的 foreach 是什么?您的意思是添加另一个函数吗?
  • 不知道查询->结果在那里做了什么。 foreach 用于当您第二次单击添加到购物车时,它将更新数量。

标签: php database codeigniter shopping-cart multiple-tables


【解决方案1】:

这是您的无错误方法。您在从 table tvs 中选择并在连接中使用 tv 时犯了错误

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();
}

【讨论】:

  • 好地方!更改了它,但它仍然给出“产品不存在”:(。
  • 抱歉,还有其他问题。阅读 codeigniter 用户指南还阅读 Jamie Rumbelows Codeigniter 手册,这将为您提供很多帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多