0

I am customising a Joomla module (Override) and it does not seem to accommodate if an image is not selected (just shows a broken image symbol).

What I'd like to do is check if there is an image and if not, assign a placeholder image.

<div class="sp-portfolio-thumb">
    <a href="<?php echo $item->link; ?>"><img src="<?php echo $item->image; ?>" alt="<?php echo $item->title; ?>" /></a>
    <div class="sp-portfolio-overlay">
        <div>
            <!--<a class="sp-portfolio-preview" rel="lightbox" title="<?php echo $item->title; ?>" href="<?php echo $item->image_full; ?>"><?php echo JText::_('PREVIEW'); ?></a>-->
            <?php if($show_readmore){ ?>
                <a class="sp-portfolio-link" href="<?php echo $item->urls->urla; ?>"><?php echo JText::_('MORE_DETAILS'); ?></a>
            <?php } ?>
        </div>
    </div>
</div>

I've tried playing about with the part of the code relating to the image, but I'm clearly not very proficient in php.

<?php if ($item->image){ ?>
    <a href="<?php echo $item->link; ?>"><img src="<?php echo $item->image; ?>" alt="<?php echo $item->title; ?>" /></a>
<?php else ?>
    <img class="test" src="images/placeholder.jpg">
<?php } ?>

I'm getting:

0 - syntax error, unexpected 'else' (T_ELSE)

Can any one point me in the right direction with this?

  • 1
    Please award green ticks on your historically resolved questions. Those which are not resolved should be updated so that they progress toward a green ticked solution. – mickmackusa Jun 1 at 23:51
  • 2
    I recommend that you conduct your professional web development on a good IDE which will expose typos of this kind before you even execute it. I am quite happy to endorse PHPStorm. Before posting php errors (unrelated to Joomla!'s software), please research on Stackoverflow because this type of error has been posted hundreds of times there. – mickmackusa Jun 1 at 23:58
  • If I have not resolved those then I will not award them, I have multiple projects ongoing and come back to these issues and mark them resolved as and when I get back round to finishing them. – Dtorr1981 Jun 2 at 7:01
  • 1
    I'm not asking for green ticks where they are not earned. I moreso wish to see progress on your questions that have stalled to abandonment despite answers being posted on them. – mickmackusa Jun 2 at 7:18
2

Your are missing brackets around else:

<?php if ($item->image) { ?>
...
    <?php } else { ?>
...
<?php } ?>

You may also give alternative syntax a try, see if it's more readable to you:

<?php if ($item->image) : ?>
...
    <?php else : ?>
...
<?php endif; ?>
1

Another idea is to initialize an $image variable earlier in your script like so:

$image = $item->image ? $item->image : 'images/placeholder.jpg';

The $image will get the value of the $item->image if it's set, or the placeholder file path you set.

Then you use the $image variable in place of the $item->image in your script. *You won't need the conditional test anymore, as the $image will always have a value.

So this part will look like this:

<a href="<?php echo $item->link; ?>"><img src="<?php echo $image; ?>" alt="<?php echo $item->title; ?>" /></a>
  • 1
    The OP doesn't want a hyperlink on the placeholder. This doesn't perform as required. – mickmackusa Jun 2 at 7:20
  • 1
    Are you able to migrate this page to Stackoverflow where it belongs? – mickmackusa Jun 2 at 7:33
  • 1
    @mickmackusa - it's not clearly specified if having a hyperlink or not is a requirement. This may appear in the code provided by the OP, but it doesn't mean a lot. I answered to the part of his question that says: "What I'd like to do is check if there is an image and if not, assign a placeholder image." Moreover, having links to the article either for the image, or the placeholder is a good thing for a consistent layout - but of course that may depend on what someone want to do... ;) – FFrewin Jun 2 at 7:59
  • 1
    What needs to happen within JSE to add Stackoverflow as a place to migrate pages to? When I try to vote to Close and migrate, the only destination option is JSE Meta. – mickmackusa Jun 2 at 8:09
  • 1
    @mickmackusa - I had also this question in the early days of JSE. This simply isn't possible/available at the moment and we don't know much more about this feature. Let this question be here for now. If you like add a comment under the Q to mention that this type of questions are out of scope/ off-topic for JSE with a link to the related Help page (joomla.stackexchange.com/help/on-topic). – FFrewin Jun 2 at 8:14
0

I gave up on this in the end and requested the client manually add the placeholder image when creating the post.

Thank you all for your contributions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.