2016-02-29 135 views
0

嗨,我已经调用codeigniter控制器中的数据库值并计算数据库的值并存储在一个新变量中。那么我怎样才能同时获得数据库值和我在控制器中创建的新变量,因为我想在视图中显示所有数据库值和新的计算值。请建议我。从控制器中检索Ajax数据

我的控制器:欢迎

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 

class Welcome extends CI_Controller { 

public $total; 

public function __construct() { 
    parent::__construct(); 
    $this->load->library('table'); 
    $this->load->model('product_database'); 
} 

public function index() { 
    $data['show_table'] = $this->view_table(); 
    $this->load->view('welcome_message', $data); 
} 
public function view_table(){ 
    $result = $this->product_database->show_all_data(); 
    if ($result != false) { 
     return $result; 
    } else { 
     return 'Database is empty !'; 
    } 
} 
public function AddtoCart(){ 



    $id = $this->input->post('product_id'); 
    $qty = $this->input->post('qty'); 

    $this->db->where('id', $id); // Select where id matches the posted id 
    $query = $this->db->get('productlist', 1); // Select the products where a match is found and limit the query by 1 

     foreach ($query->result() as $row) 
      { 
        $data = array(
        'id'  => $id, 
        'qty'  => $qty, 
        'price' => $row->price, 
        'name' => $row->name 

       ); 
       $total=$qty*$row->price; 
       echo $total; 


      } 



     } 

} 
?> 

我的观点:WELCOME_MESSAGE

<!DOCTYPE HTML> 
<html> 
<head> 
<title>Product list</title> 
<meta charset="utf-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<link rel="stylesheet"  href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js">  </script> 
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"> </script> 
<script type="text/javascript"> 
$(document).ready(function() { 
$("#add").click(function(event) { 
event.preventDefault(); 


jQuery.ajax({ 
type: "POST", 
url: "<?php echo base_url(); ?>" + "index.php/Welcome/AddtoCart", 
dataType: 'json', 
data: {name: }, 
success: function(res) { 

if (res) 
{ alert(); 
// Show Entered Value 

jQuery("#total").html(res.total); 

} 
} 
}); 
}); 
}); 
</script> 
</head> 
<body> 

<div class="container"> 
<div class="message"> 
      <?php 
      if (isset($read_set_value)) { 
       echo $read_set_value; 
      } 
      if (isset($message_display)) { 
       echo $message_display; 
      } 
      ?> 
     </div> 
<div> <?php 
     if (isset($show_table)) { 
      echo "<div class='productlist'>"; 
      if ($show_table == 'Database is empty !') { 
       echo $show_table; 
      } else { 

       echo '<h2>Product List</h2><br/><br/>'; 
       ?> 
       <div class="row"> 
       <div class="col-sm-8"> 
       <div class="well"> 

          <div class="table-responsive"> 
       <?php 
       echo "<table width='98%', >"; 
       echo '<tr><th class="e_id">Id</th><th>ProductName</th>  <th>Price</th> <tr/>'; 
       $i=1; 
       foreach ($show_table as $value) { 
        ?> 
        <tr class="well" > 

         <?php 
         echo "<td width='30%' height='27px'>" . $value->id . "</td>" . "<td width='70%' height='27px'>" . $value->name . "</td>" . "<td height='27px'>" . $value->price . "</td>"; 
         ?> 
         <?php echo form_open('/Welcome/AddtoCart'); ?> 
          <td><input type="number" name="qty" value="1" style="width:40px;" min="1" max="99"></td> 
          <input type="hidden" name="product_id" value="<?php echo $value->id ?>" /> 
          <td><input type="submit" value="Add" id="add" width="100%"></td> 
          <td><input type="submit" value="Rmv" id="rmv" width="100%"></td></br> 
         <?php echo form_close(); ?> 

       </tr> 

       <?php 
       $i=$i+1;} 
       echo '</table>'; 
       ?> 
       </div> 

       </div> 
       </div> 
       <div class="col-sm-4"> 
        <div class="well"> 
         <h4>Cart</h4> 
         Total Rs:<input type="text" value="" id="total"></br><br> 
         <input type="submit" value="Checkout" id="chk_out"> 

        </div> 


       </div> 
       </div> 

       <?php 
       } 
      echo "</div>"; 
     } 
     ?> 
     </div> 
</div> 
</body> 
</html> 

而且我的模型

<?php 

if (!defined('BASEPATH')) 
exit('No direct script access allowed'); 

class Product_database extends CI_Model { 

public function show_all_data() { 
    $this->db->select('*'); 
    $this->db->from('productlist'); 
    $query = $this->db->get(); 
    if ($query->num_rows() > 0) { 
     return $query->result(); 
    } else { 
     return false; 
    } 
} 

} 
?> 
+0

'echo json_econde($ yourArray);',或者更好,请提供您的代码。试过了什么?它有什么作用?它应该做什么呢? – ROAL

+0

问题不明确。您是否遇到了获取AJAX数据的问题,或者您不知道如何在codeigniter中与数据库进行交互?你能澄清这个问题吗? – Farside

+0

显示您的控制器代码 –

回答

0

在地方的echo $total你可以把这个代码:

$total= array('total'=> $total); 
$youarray=array_merge($data, $total); 
echo json_econde($youarray,true); 
+0

和解析json在你的AJAX成功 –

+0

所以我可以使用上面的代码调用价值 – Pittz

+0

是和你的ajax成功警报(水库) –

相关问题