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.
Conring Automation Services
----------------------------------------
Software for Business and Industry Since 1987
By sublmnl - July 27, 2014
That works. Makes sense too.
Thanks a lot. One of the reasons why I love using iTools, is the support from the team and the community.
One thing that was missing in your code was the last ELSE which was to not link any text or image at all.
So I went ahead and added another checkbox for the 'new' option in the CMS and wrapped your code with a single IF -> ELSE -> endIF
I'm sure there is probably another way to do this, like to check for whether or not there is an External Link in the text Field or if the checkbox is checked for the Internal Page.
But this is what I did (it works):
<?php foreach ($record['list_image'] as $index => $upload): ?>
<?php if ($record['link_activity_to_another_page']):?>
<?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>
<?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 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.
Conring Automation Services
----------------------------------------
Software for Business and Industry Since 1987