controller
Código
case 'update': $id = $_POST['id']; $editCode = $_POST['editCode']; $editProduct = $_POST['editProduct']; $editPrice = $_POST['editPrice']; $editStock = $_POST['editStock']; $update = new product(); if($update->update($id,$editCode,$editProduct,$editPrice,$editStock)){ $stock = $update->getProductStock($id); if ($stock > 20) { notification::removeNotifications($id); } }else{ } break;
model
Código
function update($id,$editCode,$editProduct,$editPrice,$editStock){ $sql="UPDATE product SET code = '{$editCode}',product='{$editProduct}',purchase_price={$editPrice},stock={$editStock} WHERE idproduct={$id}"; if($this->conexion->conexion->query($sql)){ return true; } else{ return display_errors(); } $this->conexion->cerrar(); }
Código
function searchProduct($id,$main=false,$limit=false){ if($main !== false && $limit !== false){ $sql="SELECT md5(idproduct),code,product,purchase_price,stock,product.status FROM product WHERE code like '%".$id."%' or product like '%".$id."%' ORDER BY idproduct ASC LIMIT $main,$limit"; }else{ $sql="SELECT md5(idproduct),code,product,purchase_price,stock,product.status FROM product WHERE code like '%".$id."%' or product like '%".$id."%' ORDER BY idproduct"; } $this->conexion->conexion->set_charset('utf8'); $result=$this->conexion->conexion->query($sql); while($record = $result->fetch_array(MYSQLI_NUM)){ $array[] = $record; } return $array; echo $result; $this->conexion->cerrar(); }
AJAX
Código
function searchProduct(id,page){ var page=Number(page); $.ajax({ url:'../controller/product_controller.php', type:'POST', data:'id='+id+'&page='+page+'&boton=search' }).done(function(resp){ var d=resp.split("*"); var Data = eval(d[0]); if(Data.length === 0) $('#info').html('<h4 style="color: red">Sorry there are no products available</h4>') else $('#info').html(''); html="<table class='table table-condensed table-hover table-striped'><thead><th>#</th><th>code</th><th>image</th><th>product</th><th>price</th><th>stock</th><th>status</th><th>option<th></thead><tbody>"; for(i=0;i<Data.length;i++){ status = (Data[i][5] == 1) ? "<span class='label label-success'>active</span>":"<span class='label label-danger'>inactive</span>"; data=Data[i][0]+"*"+Data[i][1]+"*"+Data[i][2]+"*"+Data[i][3]+"*"+Data[i][4]; html+="<tr><td>"+(i+1)+"<td>"+Data[i][1]+"</td><td>image</td><td>"+Data[i][2]+"</td><td>"+Data[i][3]+"</td><td>"+Data[i][4]+"</td><td>"+status+"</td></td></td><td><button class='btn btn-warning btn-options' data-toggle='modal' data-target='#modalproduct' onclick='mostrar("+'"'+data+'"'+");'><span class='glyphicon glyphicon-pencil'></span></button><button class='btn btn-danger btn-options' onclick='Delete("+'"'+Data[i][0]+'"'+")'><span class='glyphicon glyphicon-trash'></span></button></td></tr>"; } html+="</tbody></table>" $("#list-product").html(html); var totalrec = d[1]; var num_paginate = Math.ceil(totalrec/3); var fieldSeach=$("#search").val(); paginate="<ul class='pagination'>"; if(page>1){ //paginate+="<li><a href='javascript:void(0)' onclick='searchProduct("+'"'+fieldSeach+'","'+1+'"'+")'>«</a></li>"; paginate+="<li><a href='javascript:void(0)' onclick='searchProduct("+'"'+fieldSeach+'","'+(page-1)+'"'+")'>«</a></li>"; } else{ //paginate+="<li class='disabled'><a href='javascript:void(0)'>«</a></li>"; paginate+="<li class='disabled'><a href='javascript:void(0)'>«</a></li>"; } limit = 10; div = Math.ceil(limit / 2); pageMain = (page > div) ? (page - div) : 1; if (num_paginate > div){ pagRestantes = num_paginate - page; pagFin = (pagRestantes > div) ? (page + div) : num_paginate; } else{ pagFin = num_paginate; } for(i=pageMain;i<=pagFin;i++){ if(i==page) paginate+="<li class='active'><a href='javascript:void(0)'>"+i+"</a></li>"; else paginate+="<li><a href='javascript:void(0)' onclick='searchProduct("+'"'+fieldSeach+'","'+i+'"'+")'>"+i+"</a></li>"; } if(page<num_paginate){ paginate+="<li><a href='javascript:void(0)' onclick='searchProduct("+'"'+fieldSeach+'","'+(page+1)+'"'+")'>»</a></li>"; //paginate+="<li><a href='javascript:void(0)' onclick='searchProduct("+'"'+fieldSeach+'","'+num_paginate+'"'+")'>»</a></li>"; } else{ paginate+="<li class='disabled'><a href='javascript:void(0)'>»</a></li>"; //paginate+="<li class='disabled'><a href='javascript:void(0)'>»</a></li>"; } paginate+="</ul>"; $("#paginador").html(paginate); }); }