Default Image

9 posts by 3 authors in: Forums > CMS Builder
Last Post: March 9, 2011   (RSS)

By kdub718 - March 4, 2011

I have like a news/blog style page setup. Basically I want a default image to show up on every post unless I upload one that actually goes with the post. Anyone know how to do this? I have tried a few different ways but wasn't successful...

Re: [kdub718] Default Image

By zip222 - March 5, 2011

This can be done with an if/else statement.

Assuming you are trying to do this on a list page, not a detail page:
Inside of the foreach loop that outputs the blog listings, use the code below. you will need to change the "image" variable (in two places) to match your particular field name.

<?php if($record["image"]):?>
<?php foreach ($record['image'] as $upload): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" />
<?php endforeach ?>
<?php else: ?>
<img src="pathto/defaultimage.jpg" />
<?php endif; ?>


if you are trying to do this on a detail page, the code will need to change slightly.

Re: [zip222] Default Image

By kdub718 - March 5, 2011

Awesome, makes sense... Thank you... I have a couple more questions maybe you can help me with?

Right now in my cmsb I have a summary text field that I put a brief description in (say a paragraph of the full story) then I have a "full story" button that can be clicked to take them to the "details" page which obviously shows the entire story... So the question I have is can I get rid of that extra text field in cmsb (1 less field to have) and put say a character limit or line limit on the "list" page that will do that for me automatically?

Does that make sense?

Re: [kdub718] Default Image

By Jason - March 7, 2011

Hi,

Take a look at this post:
http://www.interactivetools.com/forum/gforum.cgi?post=66587#66587

It describes a function called maxWords that you can use to display only a certain number of words from a field.

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: [zip222] Default Image

By kdub718 - March 7, 2011

The maxWord worked perfectly thanks for that..

but back to my default image... I have it setup with a thumb nail.. I keep getting a syntax error on the <?php endforeach ?> thats after the <?php break ?>

<?php foreach ($newsRecords as $record): ?>
<div class="news">
<!-- news-->
<div class="info">
<!-- info--><h3><a href="news/<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a></h3>
<strong>Published: <?php echo date("D, M jS, Y", strtotime($record['date'])) ?></strong>
<p>
<?php $record['content']; echo maxWords($record['content'],43); ?>... <a href="news/<?php echo $record['_link'] ?>"> Full Story</a>
</p>
</div>
<!-- end info-->
<?php foreach ($record['uploads'] as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<a href="<?php echo $upload['urlPath']; ?>"></a>
<a href="news/<?php echo $record['_link'] ?>"><img src="<?php echo $upload['thumbUrlPath'] ?>" alt="" /></a>
<?php break ?>
<?php endforeach ?>
<?php else: ?>
<img src="pathto/defaultimage.jpg" />
<?php endif; ?>
</div>
<?php endforeach ?>
<?php if (!$newsRecords): ?>No records were found!
<?php endif ?>
</div><!-- end left-->


Maybe I can't do it with the thumbnail the way I have it... Will I have to just use what you have and set my max width and height to make it thumbnail?

Re: [Jason] Default Image

By kdub718 - March 9, 2011

Well I have tried a few different ways. But the default image won't show up... The Uploaded image that I have with the news shows up... so the "if" is working... But it also was working before I started this default image idea... lol...

I'm gonna show you the entire news code I have maybe you will see something thats wrong... I dunno

<div class="leftbox">
<!-- left-->
<div class="title">
<a href="news/">NEWS ARCHIVES</a>
<img src="images/title.png" alt="" />
</div>
<?php foreach ($newsRecords as $record): ?>
<div class="news">
<!-- news-->
<div class="info">
<!-- info--><h3><a href="news/<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a></h3>
<strong>Published: <?php echo date("D, M jS, Y", strtotime($record['date'])) ?></strong>
<p>
<?php $record['content']; echo maxWords($record['content'],43); ?>... <a href="news/<?php echo $record['_link'] ?>"> Full Story</a>
</p>
</div>
<!-- end info-->
<?php foreach ($record['uploads'] as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>

<a href="<?php echo $upload['urlPath']; ?>"></a>
<a href="news/<?php echo $record['_link'] ?>"><img src="<?php echo $upload['thumbUrlPath'] ?>" alt="" /></a>

<?php else: ?>

<a href="news/<?php echo $record['_link'] ?>"><img src="images/defaultnewsimage.jpg" alt="" /></a>

<?php endif; ?>
<?php endforeach ?>
</div>
<?php endforeach ?>
<?php if (!$newsRecords): ?>No records were found!
<?php endif ?>
</div><!-- end left-->


Thanks again for all your help.

Re: [kdub718] Default Image

By Jason - March 9, 2011

Hi,

Since you only want to output the default image if no image has been uploaded, try moving the if statement outside the foreach loop like this:

<?php foreach ($record['uploads'] as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>

<a href="<?php echo $upload['urlPath']; ?>"></a>
<a href="news/<?php echo $record['_link'] ?>"><img src="<?php echo $upload['thumbUrlPath'] ?>" alt="" /></a>

<?php endif ?>

<?php endforeach ?>

<?php if(!$record['uploads']): ?>

<a href="news/<?php echo $record['_link'] ?>"><img src="images/defaultnewsimage.jpg" alt="" /></a>
<?php endif ?>


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] Default Image

By kdub718 - March 9, 2011

Awesome that worked... thanks... I'm working on something else with images in the news section and probably will have a question or 2 later... lol

Thanks again