Jump to content

Recommended Posts

Although I'm not completely new to PHP it's not my first choice of language so I may still fall in traps from time to time. I'm currently using this newsticker:

https://phppot.com/javascript/javascript-news-ticker/

and it's worked like a charm.

 

Recently, I decided to implement some basic I18n and wanted to isolate the small script:

 

<div id="grid-logo-comment" class="rss">
  <ul>
    <li>RSS: <?=$rss["title"]?></li>
    <li>Description: <?=$rss["description"]?></li>
    <?php foreach ($rss_feed->channel->item as $feed_item) { ?>
    <li><a href="<?=$feed_item->link?>"><?=$feed_item->title?></a></li>
    <?php } ?>
  </ul>
</div>
<script>startTicker('grid-logo-comment');</script>

like so:

[...]
$ticker_rss_template = __ROOT__ . "/views/ticker_rss_" . $language . ".php";
[...]

<div id="grid-logo-comment" class="rss">
  <?php include $ticker_rss_template; ?>
</div>
<script>startTicker('grid-logo-comment');</script>

with the template file being the very simple UL block from above:

Quote

<ul>
    <li>RSS: <?=$rss["title"]?></li>
    <li>Description: <?=$rss["description"]?></li>
    <?php foreach ($rss_feed->channel->item as $feed_item) { ?>
            <li><a href="<?=$feed_item->link?>"><?=$feed_item->title?></a></li>
    <?php } ?>
</ul>

 

For some reason, which currently eludes me, this doesn't work. It apparently has to be in there to work. I've tried various things like putting the 'startTicker' function in the <body onload=""> block but so far I've had no luck. What am I missing here?

Regards and TIA,

sbrothy

 

A couple of things:

  • There's no javascript in any of your code
  • When you have essential code, don't use include, use require().  There's really no reason to ever use include.
  • I would highly advise the use of alternative syntax when you are doing so much intermingling of html markup and code.  You are already using <?= so you might as well use it with all your control structure as well.  See https://www.php.net/manual/en/control-structures.alternative-syntax.php
    • I personally add spacing on lines where I use <?= ... ?> as it's more readable to me.  Just wanted to point out that it does work if you leave spaces.

Here's how you would rewrite your last template snippet (the foreach):

<ul>
    <li>RSS: <?= $rss["title"] ?></li>
    <li>Description: <?= $rss["description"] ?></li>
    <?php foreach($rss_feed->channel->item as $feed_item): ?>
            <li><a href="<?= $feed_item->link ?>"><?= $feed_item->title ?></a></li>
    <?php endforeach; ?>
</ul>

 

You can use string functions, use ternary syntax and null coalesce,  and do concatenation and interpolation inside a <?= block as well.  Simple example--

<?php
 $foo = 'Hello';
?>
<div>
  <?php for($x=1; $x < 6; $x++): ?>
      <p>I say to you: <?= $x . '.' . strtoupper($foo) ?></p>
  <?php endfor; ?>
</div>

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.