PHP if/else help

10 posts by 5 authors in: Forums > CMS Builder
Last Post: June 28, 2011   (RSS)

By zip222 - October 9, 2008 - edited: October 29, 2008

I have a portfolio-based site that I am working on right now. On the project detail pages, you can click small icons to rotate through the images associated with the project. For each project there are 1 to 4 images. But when these is only one image, I don't want the small icons to appear. Please reference these two pages:

Single image example:
[removed url]

Multiple image example:
[removed url]

Specifically, I am referring to the icons in the right sidebar.

What I am looking for is a method using php to count the number of images and then only run the "foreach" if the number of images exceeds 1.

Here is the cmsbuilder/php code I am using at the moment

<?php foreach ($record['images'] as $upload): ?>
<li><a href="#">.</a></li>
<?php endforeach ?>

Can anyone help me out with this?

thanks,
-jason

Re: [jdancisin] PHP if/else help

By Dave - October 9, 2008

Hi Jason,

Try this:

<?php if (count($record['images']) == 1): ?>
There is only 1 image
<?php endif ?>

or

<?php if (count($record['images']) == 1): ?>
<?php foreach ($record['images'] as $upload): ?>
<li><a href="#">.</a></li>
<?php endforeach ?>
<?php endif ?>


Hope that helps!
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] PHP if/else help

By zip222 - October 9, 2008

Perfect! Thanks.

Re: [pod9] PHP if/else help

By robin - March 30, 2011 - edited: March 30, 2011

Hey,

It looks like the problem is you're checking $record['special_offer'] outside of the foreach loop where it is created. You'll need to set another variable to check at the bottom. For example in your loop add one line so it looks something like this:
<?php if ($record['special_offer']): ?>
<?php $special_offer=1; ?>
<a href="<?php echo $record['_link'] ?>"><img src="<?php echo $upload['thumbUrlPath'] ?>"
width="<?php echo $upload['thumbWidth'] ?>"
height="<?php echo $upload['thumbHeight'] ?>"
alt="<?php echo $upload['info1'] ?>" border="0"/></a>
<?php endif ?>

Then where you check at the bottom:
<?php if (!$special_offer): ?>
Robin
Programmer
interactivetools.com

Re: [robin] PHP if/else help

By pod9 - June 17, 2011

Hi, I have another problem on the same web site - http://www.ibizaedgevillas.com

Just to re-cap, the column on the right hand side of the design is used to show special offers by using an include which you can see the code for below. These propogate based on an if statement. Basically if there is content in the special offers field in the cms then show this villa in the special offers column. For some reason some villas are not showing up even if they have content in the special offers field? I think I remember a problem like this before and it was something to do with record numbers? Any ideas?

<?php

require_once "/home/fhlinux130/i/ibizaedgevillas.com/user/htdocs/cmsAdmin/lib/viewer_functions.php";

list($villas_for_rentRecords, $villas_for_rentMetaData) = getRecords(array(
'tableName' => 'villas_for_rent',
'limit' => '20',
'orderBy' => 'createdDate DESC',
'allowSearch' => 0,
));

?>
<?php foreach ($villas_for_rentRecords as $record): ?>
<?php if ($record['special_offer']): ?>
<?php $special_offer=1; ?>
<?php foreach ($record['image'] as $upload): ?>
<a href="<?php echo $record['_link'] ?>"><img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="<?php echo $upload['info1'] ?>" border="0"/></a>
<?php endforeach ?>
<span class="villaintro"><?php echo $record['special_offer'] ?> <?php echo $record['beds'] ?> bedrooms.</span><br>
<a href="<?php echo $record['_link'] ?>"><b>view Ibiza villa details...</b></a>
<br><br />
<?php endif ?>
<?php endforeach ?>

<!-- if there is no content in any special offers record -->
<?php if (!$record['special_offer']): ?>
We have regular special offers on our <strong>Ibiza Villas</strong>. <br />
<br />
Please <a href="http://www.ibizaedgevillas.com/contact.php">contact us</a> if you would like to be added to our mailing list.<br />
<br />
If you see any of the same <strong>Ibiza Villas</strong> for rent at a cheaper price feel free to contact us and we will try our best to beat that price.
<?php endif ?>
Pod9

Re: [pod9] PHP if/else help

By Jason - June 17, 2011

Hi,

How many records are you expecting to see?

If you could fill out a [url http://www.interactivetools.com/support]2nd Level Support Request[/url] I can take a quick look and try to find what the issue is.

Thanks
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] PHP if/else help

By pod9 - June 28, 2011

hi,

sorry for the delay in getting back. i've been out of the office.

i have submitted the details though the second line support form (although it did not give me an indication that the info was submitted successfully, it just re-loaded the page?)

there should be one more vill showing called Casa Avalon (ref 0402). you will see all the villas in the "villas for rent" section when you login. Casa Avalon has some info in the "discounts" field and therefore should be showing in the right column.

thanks for your help, james
Pod9

Re: [pod9] PHP if/else help

By Jason - June 28, 2011

Hi,

I've taken a look at the code and found the problem. The issue is that you were selecting all your records, not just the ones with a value for special_offer. This wouldn't be a problem except that you set a limit of 20 records. Your fourth "special_offer" record fell below that 20 record threshold. What I did was to change your query to only select record that had a value in the special offers field like this:
list($villas_for_rentRecords, $villas_for_rentMetaData) = getRecords(array(
'tableName' => 'villas_for_rent',
'limit' => '20',
'orderBy' => 'createdDate DESC',
'allowSearch' => 0,
'where' => "special_offer != '' ",
));

Then then could remove the "if" statement completely.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] PHP if/else help

By pod9 - June 28, 2011

that makes sense. easy when you know how!

thanks for your help. much appreciated.
Pod9