php - Count the Number of match Word in Json -


how can possibly count word matches string... matched '201' in enter image description here

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(); 

jsfiddle of working


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -