php - Get query result plus mysql message (or additional data) at a time -
when fetch query result database, result on success or message on error.
when run same query directly on phpmyadmin additional data well. can see marked message below:
my question can additional data too, on return of query ?
why important me ?
i want display records (lets blog posts/ job posts) using ajax method, based on filter. on select of each filter, generate sql , run on server have run 2 times because need show both posts , how many records have been found.
so on single run if can posts data (which obvious return) , total records found have been less costly server.
those wanted see code or pointed question negative marking-- if know twbspagination jquery plugin know talking about. if don't know plugin please don't answer question.
note: th etwbspagination plugin create pagination before display or call pagination requires number of total page (which retriving using 1 ajax ) again page requires post data display (where using ajax request)
here code
//this ajax display data function data_view(a){ var x=$('#view_job').serializearray(); x.push( {name:'page', value:a} ); //a page number $.post( $('#view_job').attr('action'), x, function(response){ $('#view_table').empty(); $('#view_table').append(response);//inserting success message }) } //calling pagination plugin $('#pages').twbspagination({ totalpages: <?php echo $trow; ?>, visiblepages: 7, onpageclick: function (event, page) { data_view(1); //this post data being called var tx=record/perpage; if(parseint(tx)<page){rec=record%perpage+(page*perpage-perpage);}else{rec=(page*perpage);} $('.record').html('showing ' + (page*perpage+1-perpage) + ' ' + rec + ' of <?php echo $rownum; ?> records'); } }); //this part pagination plugin reset function filter(){ var newrecord; var $pagination = $('#pages'); //where pagination displayed var defaultopts = { startpage: 1, totalpages: 100, newrecord: 500, //setting defaults (will replaced data ajax) onpageclick: function (event, page) { data_view(page); //calculating number of records var tx=newrecord/perpage; if(parseint(tx)<page){var rec=newrecord%perpage+(page*perpage-perpage);}else{rec=(page*perpage);} $('.record').html('showing ' + (page*perpage+1-perpage) + ' ' + rec + ' of ' + newrecord + ' records'); } }; $pagination.twbspagination(defaultopts); //another ajax retrive number of records var x=$('#view_job').serializearray(); x.push( {name:'action', value:'job_tr'} ); //getting total record , determining total page number $.post( $('#view_job').attr('action'), x, function(data){ newrecord=data; if(data%perpage>=1){var pq=parseint(data/perpage)+1;}else if(data%perpage<1){pq=1;}else{pq=parseint(data/perpage);} var totalpages = pq; //setting new total page $pagination.twbspagination('destroy'); $pagination.twbspagination($.extend({}, defaultopts, { startpage: 1, totalpages: totalpages, newrecord: newrecord })); }) } <?php //the php part //========================= //the post data display part function view_job(){ $page= $_post['page']; $num=(int)$page*20+1-20; $start=$page*20-20; $status= $_post['status']; $cat= $_post['cat']; $name= $_post['name']; $q=0; //suppose don't have parameter //now checking if parameters there , pushing them inside variable if($cat!=0){$q=1;$varx[]=" cat='$cat' ";} if($status !=0){$q=1;$varx[]=" status='$status' ";} if($name !=''){ $q=1; $varx[]=" name '%$name%' ";} //if have parameter add clause if($q==1){ $match=" where"; $j = 0;//j used run while loop $r=0; //r use see if there multiple parameter add using , while($j<=3){ //check if variable available or not if($varx[$j]!=''){ //check if value or -r- has increased (then should add , clause before term) if($r>0){$and=" , ";}else{$and="";} $match .= $and . $varx[$j]; $r++; } $j++; } } $sql ="select id,title,status " . $match . " order date desc limit $start, 20"; $result = config::readdata($sql); //reading database foreach ($result $key => $value){ $id=$value['id']; $name = $value['name']; $status=$value['status']; ?> <tr> <td><?php echo $name; ?></td> <td><?php echo $status; ?></td> </tr> <?php } }//end function <?php //retriving records function job_tr(){ $status= $_post['status']; $cat= $_post['cat']; $name= $_post['name']; $q=0; if($cat!=0){$q=1;$varx[]=" cat='$cat' ";} if($status !=0){$q=1;$varx[]=" status='$status' ";} if($name !=''){ $q=1; $varx[]=" name '%$name%' ";} if($q==1){ $match=" where"; $j = 0; $r=0; while($j<=3){ //check if variable available or not if($varx[$j]!=''){ if($r>0){$and=" , ";}else{$and="";} $match .= $and . $varx[$j]; $r++; } $j++; } } $sql ="select id company" . $match; $stmt = config::connection()->prepare($sql); $stmt->execute(); $result = $stmt->rowcount(); echo $result ; }//end of function ?>
if job.id equal number of job posting have, can add count(job.id) select statement , return how many job id's have. can grab number , display it. (you can count function on field relevant number of jobs returned)
update: submitting code. bit unclear on reading code, if case want use data 1 ajax call 2 seperate outputs, store return ajax call , later use. way can modify first query suggested above , change parsing store count later. few examples in stack overflow (without async: false) are:
var return_first; function callback(response) { return_first = response; //use return_first variable here } $.ajax({ 'type': "post", 'global': false, 'datatype': 'html', 'url': "ajax.php?first", 'data': { 'request': "", 'target': arrange_url, 'method': method_target }, 'success': callback(data) });
jquery return ajax result outside variable
and
var reqs = []; $('.inp').each(function(){ // code reqs.push( $.ajax({ // settings }) ); }); $.when.apply($, reqs).then(function() { setupgraph(); });
storing ajax response variable use later in script?
also, note may bit insecure way dynamically building queries. best not use variables build queries allow sql injection despite use of prepared statements (i.e. prepared statements, type out entire query , send parameters separate). may more work, more secure imo
ex:
if($cat!=0){ $sql ="select count(id) company cat = ?"; $stmt = config::connection()->prepare($sql); $stmt->bind_param($cat); $stmt->execute(); $stmt->bind_result($number_of_id); echo $number_of_id; }
Comments
Post a Comment