Jump to content

[SOLVED] Function checking, help!


bryan868

Recommended Posts

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

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
}?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.