IF image THEN ...

13 posts by 2 authors in: Forums > CMS Builder
Last Post: May 6, 2008   (RSS)

By rasbro - April 28, 2008

How do I display some text ONLY if an image is uploaded/present?

Example:

IF
<?php if ($upload['hasThumbnail']): ?>
THEN
<br /><span>Click to view larger image</span>


Thanks,
Brian

Re: [rasbro] IF image THEN ...

By Dave - April 28, 2008

Hi Brian, try this:

<?php if ($upload['hasThumbnail']): ?>
...
<br /><span>Click to view larger image</span>
<?php endif; ?>


And you can display something if there is no image like this:

<?php if ($upload['hasThumbnail']): ?>
...
<br /><span>Click to view larger image</span>
<?php else: ?>
Sorry, no image!
<?php endif; ?>


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

Re: [Dave] IF image THEN ...

By rasbro - April 28, 2008

Okay, this is kind of what I'm looking for, except I need more fine tuning...

What do I put if I don't want to include the text for each image. So for example, I only want this line,
<br /><span>Click to view larger image</span>
to be added once.

Here is what I am working with now, which returns up to four lines if I have all four images uploaded:

<?php foreach (getUploads($options['tableName'], 'other_images', $record['num']) as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<a href="<?php echo $upload['urlPath'] ?>"><img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="<?php echo $upload['filename'] ?>" title="<?php echo $upload['info1'] ?>" /></a>
<?php endif ?>
<?php endforeach ?>
<?php foreach (getUploads($options['tableName'], 'other_images', $record['num']) as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<p id="otherimages-clicklarger">Click on small photos to enlarge</p>
<?php endif ?>
<?php endforeach ?>


Thanks,
Brian

Re: [Dave] IF image THEN ...

By rasbro - April 28, 2008

Also, I am looking for a way to return an image and not just text, except the image is not uploaded via CMS Builder. I am trying to display a "Sorry, No Image" image when there is not an image uploaded to represent the product. Here is what I have:

<?php foreach (getUploads($options['tableName'], 'thumbnail_image', $record['num']) as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="75" height="75" alt="inset photo" />
<?php else: ?>
<img src="/images/NoImage-thumbnail2.jpg" width="75" height="75" alt="inset photo" />
<?php endif ?>
<?php endforeach ?>

It doesn't work the way I have it currently.

Thanks,
Brian

Re: [rasbro] IF image THEN ...

By Dave - April 29, 2008

Hi Brian,

Do you have a mockup html page that shows the effect or design you're going for? That might help us get there quicker.

>What do I put if I don't want to include the text for each
>image. So for example, I only want this line, <br />
><span>Click to view larger image</span>to be added once.

If you only want to print one image, you can break out for a foreach loop (and stop looping over additional images) with the break function, like this: <?php break; ?> Just put that after the text you want displayed.

>I am trying to display a "Sorry, No Image" image when there
>is not an image uploaded to represent the product. Here is
>what I have ... It doesn't work the way I have it currently.

Try replacing this line:
<?php foreach (getUploads($options['tableName'], 'thumbnail_image', $record['num']) as $upload): ?>

With these lines:
<?php $uploads = getUploads($options['tableName'], 'thumbnail_image', $record['num']); ?>
<?php foreach ($uploads as $upload): ?>


Then you'll be able to test if there was any images _after_ your foreach loop like this:

<?php $uploads = getUploads($options['tableName'], 'thumbnail_image', $record['num']); ?>

<?php foreach ($uploads as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="75" height="75" alt="inset photo" />
<?php endif ?>
<?php endforeach ?>

<?php if (count($images) == 0): ?>
<img src="/images/NoImage-thumbnail2.jpg" width="75" height="75" alt="inset photo" />
<?php endif ?>


Let me know it goes!
Dave Edis - Senior Developer
interactivetools.com

Re: [rasbro] IF image THEN ...

By Dave - May 2, 2008

Hi Brian,

So you want it to show "Click on small photos..." only once if there is any images uploaded? Is that right? Try this:

<?php if (count($images) > 0): ?>
Click on small photos to enlarge
<?php endif ?>


And feel free to attach your viewer if you need more help and I'll take a look at your code.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] IF image THEN ...

By rasbro - May 2, 2008

Dave, that worked to remove the text when other images are not uploaded but now I get an error when there are other images uploaded, as with this page: http://www.owenequipmentsales.com/product.php/14-23/

Notice: Undefined variable: images in /.../public_html/include-ProductRecordInsert.php on line 47

See the include file attached which contains the viewer code.

Thanks,
Brian

Re: [rasbro] IF image THEN ...

By Dave - May 2, 2008

Thanks, try this:

<td colspan="2" id="sumimg-otherimages"><br />

<?php $otherImages = getUploads($options['tableName'], 'other_images', $record['num']); ?>

<?php foreach ($otherImages as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<a href="<?php echo $upload['urlPath'] ?>"><img src="<?php echo $upload['thumbUrlPath'] ?>"
width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>"
alt="<?php echo $upload['filename'] ?>" title="<?php echo $upload['info1'] ?>" /></a>
<?php endif ?>
<?php endforeach ?>

<?php if (count($otherImages) > 0): ?>
<p id="otherimages-clicklarger">Click on small photos to enlarge</p>
<?php endif ?>

</td>


Here's how it works. First, we load the uploads from the 'other_images' field into $otherImages. Next, we loop over those and for each one that has a thumbnail we display it. Finally, we check the count of 'other_images' and if there is 1 or more uploads we display the "Click on small photos..." text.

Let me know how it goes!
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] IF image THEN ...

By rasbro - May 2, 2008

Phew, that finally did it!

Thanks Dave!

Hey, what do you think about the other IF/THEN statement issue regarding the display of the "Sorry, No Image" image when main product image not uploaded? Any ideas? Refer to same viewer in the include file I attached earlier.

Thanks,
Brian