php - Count the Number of match Word in Json -
how can possibly count word matches string... matched '201' in
this end...
<?php $con=mysqli_connect("localhost","root","","project"); function loaddata() { global $con; $listdata=""; $sql=mysqli_query($con,"select fguests,mguests reserved"); while($row=mysqli_fetch_array($sql)) { $mf= array(); $mf[]=$row['mguests'].';'.$row['fguests']; $data = array( 'mf'=>$mf ); $listdata[]=$data; } return json_encode($listdata); } echo loaddata();
?> this ajax...
function x() { $.ajax({ url:"ajax/try.php", type:"get", datatype:"json", data:"", success:function(data) { $.each(data,function(i,item) { var m=item.match(/201/g).length; $("#roomlist2").append(m); }); } }); } x();
it output 6...
it should 8...
i think reading through first object in array before appending (each loops through rather returning of results concatenated). because error when match returns null second element of array (there no 201 in one). need add if check whether match returns anything. want add these numbers before appending them.
function x() { $.ajax({ url:"ajax/try.php", type:"get", datatype:"json", data:"", success:function(data) { var m = 0; $.each(data,function(i,item) { var matched = item.mf.match(/201/g); if(matched) { m += matched.length; } }); $("#roomlist2").append(m); } }); } x();