controller
Código
case 'search': $main = 0; $limit = 3; $page = $_POST['page']; $main = ($page - 1) * $limit; } $id = $_POST['id']; $search = new product(); $__result= $search->searchProduct($id); $final_result = $search->searchProduct($id,$main,$limit); break; 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); } echo 'sucess'; }else{ echo "No se Actualizo los datos"; } break;
model
Código
function searchProduct($id,$main=false,$limit=false){ if($main !== false && $limit !== false){ $sql="SELECT 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 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; $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); }); } function mostrar(data){ var d=data.split("*"); console.log(d); $("#id").val(d[0]); $("#editCode").val(d[1]); $("#editProduct").val(d[2]); $("#editPrice").val(d[3]); $("#editStock").val(d[4]); }