开发者

Output image src with SimpleXML

开发者 https://www.devze.com 2023-03-07 03:32 出处:网络
I\'m trying to output an image with SimpleXML, but the image tag doesn\'t appear in the source code. Can anyone help me outpout this image:

I'm trying to output an image with SimpleXML, but the image tag doesn't appear in the source code.

Can anyone help me outpout this image:

Here's my XML and code:

        <?php foreach($xml->Event as $event) { ?>
        <li>
            <a href="<?php echo $event->link; ?>">
                <?php if ($event->Media['url'] == !null) { ?>
                    <img 开发者_如何学JAVAsrc="<?php echo $event->Media['url'];?>" alt="<?php echo $event->title;?> thumbnail" />
                <?php } ?>
                <h3><?php echo $event->title; ?></h3>
                <p><strong><?php echo $event->beginDate; ?> at <?php echo $event->beginTime; ?></strong></p>
                <p><?php echo $event->location; ?></p>
            </a>
        </li>
        <?php } ?>


Your issue is here:

            <?php if ($event->Media['url'] == !null) { ?>
                <img src="<?php echo $event->Media['url'];?>" alt="<?php echo $event->title;?> thumbnail" />
            <?php } ?>

You're trying to access url as though it were an attribute, you need to access it as a child element by using ->url instead.

            <?php if ($event->Media->url != null) { ?>
                <img src="<?php echo $event->Media->url;?>" alt="<?php echo $event->title;?> thumbnail" />
            <?php } ?>

EDIT: By the way, == !null works as you expect, but != null is a bit friendlier and less confusing


Your if statement is incorrect. It should be:

if ($event->Media['url'] != null)
0

精彩评论

暂无评论...
验证码 换一张
取 消