link change based on field contents

5 posts by 2 authors in: Forums > CMS Builder
Last Post: November 15, 2010   (RSS)

By Oakweb - November 13, 2010

I am setting up a website for a a client which is basically a directory of organizations. There is the list page with a few details and then a "Find out more" link that goes to the detail page. All was fine until the client changed their mind and decided that if the organisation listed had a website, they wanted the Find out more link to go straight to the website, if not they want the link to go to the detail page.

So for the list page I currently have

<?php foreach ($adventure_and_sportRecords as $record): ?><div class="itemPreview">
<a href="<?php echo $record['_link'] ?>"><h1><?php echo $record['title'] ?></h1></a>
<?php echo $record['preview_description'] ?><a href="<?php echo $record['_link'] ?>"><strong>Find out more &raquo;</strong></a>
</div>
<?php endforeach ?>

This gives a few details of the organisation and then links to the detail page.

I have a field for the website address in the section editor - <?php echo $adventure_and_sportRecord['website'] ?>

So what I need to do is if the "website" field has a Url in it I need the "Find out more" link to go straight to that site and if the field is empty I need it to go to the detail page.

I have seen some bits about If statements on the forum, but can't work out if it is possible to utilize it here.

Any help would be much appreciated.

Re: [Oakweb] link change based on field contents

By zip222 - November 14, 2010

This should do it...

<?php foreach ($adventure_and_sportRecords as $record): ?>
<div class="itemPreview">
<?php if ($record['website']): ?>
<h1><a href="<?php echo $record['website'] ?>"><?php echo $record['title'] ?></a></h1>
<?php echo $record['preview_description'] ?>
<strong><a href="<?php echo $record['website'] ?>">Find out more &raquo;</a></strong>
<?php else ?>
<h1><a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a></h1>
<?php echo $record['preview_description'] ?>
<strong><a href="<?php echo $record['_link'] ?>">Find out more &raquo;</a></strong>
<?php endif ?>
</div>
<?php endforeach ?>

Re: [zip222] link change based on field contents

By Oakweb - November 15, 2010

Thank you

Unfortunately this has thrown up the error

Parse error: syntax error, unexpected ';', expecting ':' in /home/asksam/3P216Y7H/htdocs/adventure_and_sport.php on line 88

Line 88 refers to <?php else ?>

Re: [zip222] link change based on field contents

By Oakweb - November 15, 2010

Hi - I've cracked it.

Should have been

<?php foreach ($adventure_and_sportRecords as $record): ?>
<div class="itemPreview">
<?php if ($record['website']): ?>
<h1><a href="<?php echo $record['website'] ?>"><?php echo $record['title'] ?></a></h1>
<?php echo $record['preview_description'] ?>
<strong><a href="<?php echo $record['website'] ?>">Find out more &raquo;</a></strong>
<?php elseif ($record['_link']): ?>
<h1><a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a></h1>
<?php echo $record['preview_description'] ?>
<strong><a href="<?php echo $record['_link'] ?>">Find out more &raquo;</a></strong>
<?php endif ?>
</div>
<?php endforeach ?>

It was the <?php elseif ($record['_link']): ?> bit.

Thanks for your help though as I wouldn't have got that far without your start.