ajax - PHP: What is the Best Way To Search Through Values? -
i'm not sure best way , quickest way search through values.
i have check list of 20 ids example below. can stored array too.
'6e0ed0ff736613fdfed1c77dc02286cbd24a44f9','194809ba8609de16d9d8608482b988541ba0c971','e1d612b5e6d2bf4c30aac4c9d2f66ebc3b4c5d96'....
what next set of items json api call php stdclass. when loop through items add html each item display on website. if 1 of item's id matches ids in checklist add different html
i'm doing in ajax call best , efficient way search through checklist?
for example
//get list of ids db , store in $checklist $checklist; $data = file_get_contents($url); $result = json_decode($data, true); foreach ( $result->results $items ) { $name = $items->name; $category = $items->category; $description = $items->description; $id = $items->id; // if id in $checklist use blue background. $displayhtml .="<div style=\"background-color: white;\">"; $displayhtml .="<h3>".$name."</h3>"; $displayhtml .="<p>".$description."</p>"; $displayhtml .="</div>"; }
thanks.
the simple way (if you're using php this) use in_array()
$checklist = array( '6e0ed0ff736613fdfed1c77dc02286cbd24a44f9', '194809ba8609de16d9d8608482b988541ba0c971', 'e1d612b5e6d2bf4c30aac4c9d2f66ebc3b4c5d96', 'etc.' ); foreach ($items $id) // $items similar array of ids you're checking { if ( ! in_array($id, $checklist)) { // not in checklist! } }
per example:
foreach ( $result->results $items ) { $name = $items->name; $category = $items->category; $description = $items->description; $id = $items->id; // if id in $checklist use blue background. if (in_array($id, $checklist)) { $bg = 'blue'; } else { $bg = 'white' } $displayhtml .='<div style="background-color: '.$bg.';">'; $displayhtml .="<h3>".$name."</h3>"; $displayhtml .="<p>".$description."</p>"; $displayhtml .="</div>"; }
there more elegant ways handle this, didn't ask rewrite. personally, starters add css class instead of inlined style, gets moving forward.
Comments
Post a Comment