Little help needed with elseif and if statement

4 posts by 2 authors in: Forums > CMS Builder
Last Post: July 27, 2014   (RSS)

By sublmnl - July 19, 2014

Hey guys, hope you can help me here.

I am trying to run through wether or not we should wrap an Image with a link to an internal page (list detail page) or an external page.

I've done this several times, however I want to add another feature to this, if a Checkbox is checked in the CMS, then don't open the External link in a new window.

I thought this would work and neither did running two elseif 's

added this part to the second line and not working, it prints the target="_blank"

 <?php if ($record['open_in_same_window']):?>><?php else:?> target="_blank"><?php endif ?>

<div class="activity_list"><div><?php foreach ($record['list_image'] as $index => $upload): ?><?php if ($record['internal_page']):?><a href="<?php echo $record['_link'] ?>"><img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="<?php echo $upload['info1'] ?>" /></a>
<?php elseif ($record['external_link']): ?><a href="<?php echo $record['external_link'] ?>" <?php if ($record['open_in_same_window']):?>><?php else:?> target="_blank"><?php endif ?><img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="<?php echo $upload['info1'] ?>" /></a>
<?php else:?><img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="<?php echo $upload['info1'] ?>" /><?php endif ?><?php endforeach ?>

By rconring - July 23, 2014

If I were doing this, I would create 2 variables, one for target URL and one for target frame.  Then I would only use one hyperlink reference using the 2 variables:

<?php 
    if ($record['internal_page']) {$targetURL = $record['_link'] ;}
    elseif ($record['external_link']) {$targetURL = $record['external_link'] ;}

    if ($record['open_in_same_window']) {$targetFrame = "_self" ;}
    else {$targetFrame = "_blank" ;}
?>    
<a target="<?php echo $targetFrame ?>" href="<?php echo $targetURL ?>"> 
    <img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="<?php echo $upload['info1'] ?>" />
</a>

I can't test this code, but  it should work because I do it frequently almost the same way except reverse.  My target frame variable is a check box to open in new window.  I hope this will help you.

Ron Conring
Conring Automation Services
----------------------------------------
Software for Business and Industry Since 1987

By rconring - July 27, 2014

Yes, there are scads of variations ... I was just demonstrating the fundamental logic. 

And yes, this forum is invaluable!  I have learned everything I know about PHP-MySQL programming from mainly this forum and others such as Stack Overflow.  When I purchased CMS Builder a few years back, I knew NOTHING about web development.  I took a chance on CMSB and am very glad I did.  Between their support team and the user community I can get just about anything resolved.

I am happy to give back if I can.

Ron Conring
Conring Automation Services
----------------------------------------
Software for Business and Industry Since 1987