I have a simple shopping website in codeigniter in which I have an sql table like below:
So whenever a user selects a product size, I want to show how many pieces are left, so I took the following approach for this code:
function checkstock() {
var stockid = $(".stockid").val();
var idp = $("#idp").val();
var transfer = [stockid, idp];
$.ajax({
type: "post",
url: "<?php echo base_url(); ?>index.php/homecontroller/checkstock",
data: {
result: JSON.stringify(transfer)
},
success: function(response) {
if (response) {
$('#msg').html('<span style="color: green;">ONLY ' + stockid + "</span>");
}
}
});
}
<input id="idp" name="prodid" value="<?php echo $product->id;?>" type="hidden">
<input class="stockid" type="radio" onclick="checkstock();" name="size" value="xs">
<input class="stockid" type="radio" onclick="checkstock();" name="size" value="s">
<input class="stockid" type="radio" onclick="checkstock();" name="size" value="m">
Below is my controller's and view's code,
function checkstock(){
$post = json_decode($_POST['result']);
$idp = $post[1];
$stockid = $post[0];
$exists = $this->product->checkstock($idp,$exists);
echo $exists;
}
function checkstock($idp,$stockid)
{
$this->db->select($stockid);
$this->db->where('id', $idp);
$this->db->from('product');
$query = $this->db->get();
$result = $query->result();
return $result;
}
However this doesn't echo the count in the column, can anyone please tell me how to fix this, thanks in advance.
FROM
function checkstock($idp,$stockid)
{
$this->db->select($stockid);
$this->db->where('id', $idp);
$this->db->from('product');
$query = $this->db->get();
$result = $query->result();
return $result;
}
TO
function checkstock($idp,$stockid)
{
$this->db->select($stockid);
$this->db->where('id', $idp);
$this->db->from('product');
$query = $this->db->get();
$result = $query->row();
return $result->{$stockid};
}
Then you can get data into reponse variable and just console it.