Daily Lucky Numbers:
2
13
20
29
42
43

Scrolling (marquee) Recent Posts code for custom PHP Block

Started by dman, December 08, 2022, 04:35:30 AM

Previous topic - Next topic

dman

Hey guys, have been using the following PHP code for years on my older SMF 2.0+ installs with no issue.  When attempting to use in a custom PHP block in SMF 2.1.3 with ehPortal 1.39 it actually works, BUT, is throwing about 10 errors in the admin log.  It actually still works, pulls the posts, and scrolls, BUT still creates the following errors in the log.  Anyone more familiar with what would fix this ??  Thank you.

 
Quote/home3/xxxxxxxxx/public_html/xxxxxxxx/sp_blocks_temp/blockid_15_1_20263bd486.php (Line 40)

2: Undefined array key "time"
2: Undefined array key "new_from"
2: Undefined array key "topic"
2: Undefined array key "new"
2: Trying to access array offset on value of type null
2: Undefined array key "poster"
2: Undefined array key "subject"
2: Undefined array key "href"
2: Trying to access array offset on value of type null
2: Undefined array key "poster"

global $txt, $scripturl, $settings, $context, $color_profile;

$boards = null;
$limit = 10;
$type = 'ssi_recentPosts'; // Can also be 'ssi_recentPosts' for recent posts.
$display = 'compact'; // Could also be 'compact' for showing on the left and right sides. Although it doesn't work as well.

$items = $type($limit, null, $boards, 'array');

if (empty($items))
{
echo '
', $txt['error_sp_no_posts_found'];
return;
}
else
$items[count($items) - 1]['is_last'] = true;

$colorids = array();
foreach ($items as $item)
$colorids[] = $item['poster']['id'];

if (!empty($colorids) && sp_loadColors($colorids) !== false)
{
foreach ($items as $k => $p)
{
if (!empty($color_profile[$p['poster']['id']]['link']))
$items[$k]['poster']['link'] = $color_profile[$p['poster']['id']]['link'];
}
}

echo '<marquee behavior="scroll" direction="up" height="200px" scrolldelay=" 10" scrollamount=" 2" onmouseover="this.stop()" onmouseout="this.start()">';

if ($display == 'compact')
{
foreach ($items as $key => $item)
echo '
<a href="', $item['href'], '">', $item['subject'], '</a> <span class="smalltext">', $txt['by'], ' ', $item['poster']['link'], $item['new'] ? '' : ' <a href="' . $scripturl . '?topic=' . $item['topic'] . '.msg' . $item['new_from'] . ';topicseen#new" rel="nofollow"><img src="' . $settings['lang_images_url'] . '/new.gif" alt="' . $txt['new'] . '" border="0" /></a>', '<br />[', $item['time'], ']</span><br />', empty($item['is_last']) ? '<hr />' : '';
}
elseif ($display == 'full')
{
echo '
<table class="sp_fullwidth">';

foreach ($items as $item)
echo '
<tr>
<td class="sp_recent_icon sp_center">
', sp_embed_image(empty($parameters['type']) ? 'post' : 'topic'), '
</td>
<td class="sp_recent_subject">
<a href="', $item['href'], '">', $item['subject'], '</a>
', $item['new'] ? '' : '<a href="' . $scripturl . '?topic=' . $item['topic'] . '.msg' . $item['new_from'] . ';topicseen#new"><img src="' . $settings['images_url'] . '/' . $context['user']['language'] . '/new.gif" alt="' . $txt['new'] . '" border="0" /></a>', '<br />[', $item['board']['link'], ']
</td>
<td class="sp_recent_info sp_right">
', $item['poster']['link'], '<br />', $item['time'], '
</td>
</tr>';

echo '
</table>';
}

echo '</marquee>';

Chen Zhen


Your code just needed some array key value checks prior to attempting to use them.

Further notes:
[marquee] is deprecated in HTML5 & has been replaced with css animations.
Standard HTML tables do not display nicely on mobile/tablets and have been replaced with either div table or flex.
"rem" rather than "px" might be a better option for height since 1rem is a device's standard line height (% for width). 

Here is an example with the above noted changes:

ehPortalRecentMarquee(null, 10, 'ssi_recentPosts', '13.33rem', '20rem', 25, 'compact');
function ehPortalRecentMarquee($boards, $limit, $type, $height, $maxHeight, $speed, $display = 'compact')
{
global $txt, $scripturl, $settings, $context, $color_profile;

$context['html_headers'] .= '
<style>
.ehPort_container {
overflow: hidden;
height: ' . $height . ';
max-height: ' . $maxHeight . ';
}
.ehPort_scrolling:hover {
-webkit-animation-play-state:paused;
-moz-animation-play-state:paused;
-o-animation-play-state:paused;
animation-play-state:paused;
cursor: pointer;
}
.ehPort_scrolling {
animation: ehPort_marquee ' . $speed . 's linear infinite;
}
@keyframes ehPort_marquee {
from {
transform: translateY(' . $limit . 'rem);
}
to {
transform: translateY(-100%);
}
}
</style>
';
$items = $type($limit, null, $boards, 'array');

if (empty($items) || !is_array($items))
{
echo '
', $txt['error_sp_no_posts_found'];
return;
}
else
$items[count($items) - 1]['is_last'] = true;

$colorids = array();
$newImage = file_exists($settings['actual_images_url'] . '/' . $context['user']['language'] . '/new.gif') ? $settings['actual_images_url'] . '/' . $context['user']['language'] . '/new.gif' : $settings['default_images_url'] . '/' . $context['user']['language'] . '/new.gif';
foreach ($items as $item)
$colorids[] = !empty($item['poster']['id']) ? $item['poster']['id'] : 0;

if (!empty($colorids) && sp_loadColors($colorids) !== false)
{
foreach ($items as $k => $p)
{
if (!empty($p['poster']['id']) && !empty($color_profile[$p['poster']['id']]['link']))
$items[$k]['poster']['link'] = $color_profile[$p['poster']['id']]['link'];
}
}

echo '
<div class="ehPort_container">
<div class="ehPort_scrolling">';
if ($display == 'compact')
{
foreach ($items as $key => $item) {
if (empty($item['href']))
continue;
echo '
<a href="', $item['href'], '">', $item['subject'], '</a> <div style="display: inline;" class="smalltext">', $txt['by'], ' ', $item['poster']['link'], $item['new'] ? '' : ' <a href="' . $scripturl . '?topic=' . $item['topic'] . '.msg' . $item['new_from'] . ';topicseen#new" rel="nofollow"><img style="border: 0px;" src="' . $newImage . '" alt="' . $txt['new'] . '" /></a>', '<div>[', $item['time'], ']</div></div><div>', (empty($item['is_last']) ? '<hr />' : ''), '</div>';
}
}
else
{
echo '
<div style="display: table;" class="sp_fullwidth">';

foreach ($items as $item) {
if (empty($item['href']))
continue;
echo '
<div style="display: table-row;">
<div style="display: table-cell;" class="sp_recent_icon sp_center">
', sp_embed_image(empty($parameters['type']) ? 'post' : 'topic'), '
</div>
<div style="display: table-cell;" class="sp_recent_subject">
<a href="', $item['href'], '">', $item['subject'], '</a>
', $item['new'] ? '' : '<a href="' . $scripturl . '?topic=' . $item['topic'] . '.msg' . $item['new_from'] . ';topicseen#new"><img src="' . $newImage . '" alt="' . $txt['new'] . '" border="0" /></a>', '<div>[', $item['board']['link'], ']</div>
</div>
<div style="display: table-cell;" class="sp_recent_info sp_right">
', $item['poster']['link'], '<div>', $item['time'], '</div>
</div>
</div>';
}

echo '
</div>';
}

echo '
</div>
</div>';
}

dman

@Chen Zhen, I thank you VERY much for the fast response, as even knowing that the marquee was depreciated, there is no way I could have figured out how to sort out the array issues.  Thank you as well for the more mobile friendly code as well.