bryan868 Posted May 12, 2009 Share Posted May 12, 2009 OK, I inherited some PHP code that I don't completely understand. PHP n00b here. OK, so I've got this function which takes a group of photos in a directory... // -------- GENERATE UTIL SAVINGS LINK -------- function utilsave_link($model_id, $homeormodel = 'model', $linktext = 'View Photos'){ $path_to_photo_folder = HM_ASSETS_PATH.$homeormodel.'s/'.$model_id.'/utilsave/'; $photoinfo = dirList($path_to_photo_folder); $i = 6; foreach($photoinfo as $photo){ if($i > 6){$nameit = '';}else{$nameit = $linktext;} $utilsavelink .= '<a href="/hm_assets/'.$homeormodel.'s/'.$model_id.'/utilsave/'.$photo.'" rel="lytebox" title="Energy Savings">'.$nameit.'</a>'; $i++; } echo $utilsavelink; } And displays them on another page like so... <div class="utilsave"><?php utilsave_link($modelsubmodel[base]->id, 'model', 'Click here'); ?> to view the <?php echo $modeltitle; ?>'s utility savings. </div> Now not every model is going to have photos. So for those that don't have photos I want to hide that div using display:none;. Here's how I figured it might work... <?php if($utilsavelink == null) echo 'style="display:none;"'; ?> Except that's not working, because I'm dumb. I'm pretty sure I know why it's not working, I just don't know how to make it do what I want. Any ideas? Link to comment https://forums.phpfreaks.com/topic/157844-solved-function-checking-help/ Share on other sites More sharing options...
Zhadus Posted May 12, 2009 Share Posted May 12, 2009 Is this code: <?php if($utilsavelink == null) echo 'style="display:none;"'; ?> On the display page or in the function? Link to comment https://forums.phpfreaks.com/topic/157844-solved-function-checking-help/#findComment-832577 Share on other sites More sharing options...
bryan868 Posted May 12, 2009 Author Share Posted May 12, 2009 On the display page. <div class="utilsave" <?php if($utilsavelink == null) echo 'style="display:none;"'; ?>><?php utilsave_link($modelsubmodel[base]->id, 'model', 'Click here'); ?> to view the <?php echo $modeltitle; ?>'s utility savings. </div> Link to comment https://forums.phpfreaks.com/topic/157844-solved-function-checking-help/#findComment-832581 Share on other sites More sharing options...
Zhadus Posted May 12, 2009 Share Posted May 12, 2009 The variable $utilsavelink only exists within the function. If you adjust things a little bit, you can better the usability of your function. Instead of 'echo'ing the result in the function, have it 'return' a value. Like this: <?php // -------- GENERATE UTIL SAVINGS LINK -------- function utilsave_link($model_id, $homeormodel = 'model', $linktext = 'View Photos'){ $path_to_photo_folder = HM_ASSETS_PATH.$homeormodel.'s/'.$model_id.'/utilsave/'; $photoinfo = dirList($path_to_photo_folder); $i = 6; foreach($photoinfo as $photo){ if($i > 6){$nameit = '';}else{$nameit = $linktext;} $utilsavelink .= '<a href="/hm_assets/'.$homeormodel.'s/'.$model_id.'/utilsave/'.$photo.'" rel="lytebox" title="Energy Savings">'.$nameit.'</a>'; $i++; } return $utilsavelink; } ?> Now in your display page, set it up like this: <?php $utilsavelink = utilsave_link($modelsubmodel[base]->id, 'model', 'Click here'); if ($utilsavelink != null) {?> <div class="utilsave"><?php echo $utilsavelink ?> to view the <?php echo $modeltitle; ?>'s utility savings. </div> <?php }?> Link to comment https://forums.phpfreaks.com/topic/157844-solved-function-checking-help/#findComment-832583 Share on other sites More sharing options...
bryan868 Posted May 12, 2009 Author Share Posted May 12, 2009 Oh man, of course! Excellent! That completely makes sense and works perfectly. Thank you so much! Link to comment https://forums.phpfreaks.com/topic/157844-solved-function-checking-help/#findComment-832586 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.