dimanche 25 décembre 2016

PHP Match Text from JSON

I have a JSON file with data in, I'm trying to do a live search box. I have the code below so far, there is a few problems I can't seem to get around!

If the field matches I would like to show the results, but if the field does not match it should just show no match, currently though it will show no match for each foreach. In the code below it will only every show no matches.

Is there something I'm not doing right?

$str_tenapp = file_get_contents('../site-wide/cron/cache/tenants-applicants.json'); 
$str_tenapp = str_replace("\xEF\xBB\xBF",'',$str_tenapp); 
$obj_tenapp = json_decode($str_tenapp); 

$searchterm = $_REQUEST['query'];
$onetitle = 0;
$matchnumber = 0;
foreach($obj_tenapp->response->entries as $value) 
{   
    $onetitle++;
    if ($onetitle==1){ echo '<p class="tiles-title" style="background:#f4f5f7; font-size:10.5px;font-family: Open Sans;letter-spacing: 0.5px;font-weight: 600; color:#56646e; border:none!important;">APPLICANTS & TENNATS</p>'; }

    if(strcasecmp($value->{2.3},$searchterm)==0){
    $matchnumber++;
    echo '<p>'. $value->{2.3} .' '. $value->{2.6} .'</p>';
    } else if ($matchnumber==0){ echo '<p>No applicants or tenants match <b>'. $searchterm .'</b></p>'; return; }
};

Aucun commentaire:

Enregistrer un commentaire