arrays - Use same code for multiple PHP foreach loops efficiently -
i have few different sets of data go through foreach loop. lot of code inside each foreach loop similar, identical, i'm using variables based on defined arrays on initial data put foreach loop. there way combine these loops sort of function without having pass of data function?
meaning, can define function within loop , have use data in way? need because if want display rows have associated data, loop through using data. if want display rows whether there associated data or not, need use different array loop through.
i need 2 separate loops else because needs layout items common arrangements , layout items specific arrangement, , if there's data need display 1 way, , if it's empty, need different, have check well.
here's example of multiple loops using same data:
if($hideempty == 'hide'){ foreach($bannerarray $bannerid => $records){ foreach($records $id => $record){ ?> <tr> <td><?php echo $record['layout_name']; ?></td> <td><?php echo $record['company_name']; ?></td> </tr> <?php } } } else { $layoutgrouparray = mysqli_fetch_assoc(mysqli_query($link, "select `set_layout_group` `settopics` `set_id` = $setid")); $layoutgroup = $layoutgrouparray['set_layout_group']; foreach($banners[0] $bannerid => $layoutinfo){ if(isset($bannerarray[$bannerid])){ foreach($bannerarray[$bannerid] $id => $record){ ?> <tr> <td><?php echo $record['layout_name']; ?></td> <td><?php echo $record['company_name']; ?></td> </tr> <?php } } else { ?> <tr> <td><?php echo $layoutinfo['layout_name']; ?></td> <td>empty</td> </tr> <?php } } foreach($banners[$layoutgroup] $bannerid => $layoutinfo){ if(isset($bannerarray[$bannerid])){ foreach($bannerarray[$bannerid] $id => $record){ ?> <tr> <td><?php echo $record['layout_name']; ?></td> <td><?php echo $record['company_name']; ?></td> </tr> <?php } } else { ?> <tr> <td><?php echo $layoutinfo['layout_name']; ?></td> <td>empty</td> </tr> <?php } } } it doesn't have function use, i'm not sure how write code efficiently. doing lot of repeating, can't come way access data need without doing repeating.
why not using function?
function layout_company($layout, $company) { ?> <tr> <td><?=$layout?></td> <td><?=$company?></td> </tr> <?php } function show_records(array $records) { foreach($records $id => $record) { layout_company($record['layout_name'], $record['company_name']); } } if(hideempty == 'hide') { foreach($bannerarray $bannerid => $records) { show_records($records); } } else { function show_banners(array $banners, array $bannerarray) { foreach($banners $bannerid => $layoutinfo) { if(isset($bannerarray[$bannerid])) { show_records($bannerarray[$bannerid]); } else { layout_company($layoutinfo['layout_name'], 'empty'); } } } $layoutgrouparray = mysqli_fetch_assoc(mysqli_query($link, "select `set_layout_group` `settopics` `set_id` = $setid")); $layoutgroup = $layoutgrouparray['set_layout_group']; show_banners($banners[0], $bannerarray); show_banners($banners[$layoutgroup], $bannerarray); }
Comments
Post a Comment