Customizing SMF > Tips and Tricks

Showing "category" images with boardnews ssi

(1/2) > >>

Tim:
Slashdot's news is often accompanied by an image representing the category in which it fits.

It's very easy to use SMF's SSI functions to create the same. Just use a tag like [mysql] in front of the subject and then use this code to display in on your page:


--- Code: ---<?php 
$array = ssi_boardNews(null, 5, null, 350, &#39;array&#39;);
foreach ($array as $news)
{
    #Check which tag is present in the subject, delete it and use the image
    if (preg_match("/\[php\]/i",  $news[&#39;subject&#39;])) { 
        echo "<h5><a href=\"", $news[&#39;href&#39;], "\">", preg_replace("|\[yabb\]|i", "", $news[&#39;subject&#39;]), "</a></h5>
        <img src=\"img/php.png\" align=\"left\" alt=\"php\" />", $news[&#39;body&#39;], "\n";

    }elseif(preg_match("/\[mysql\]/i",  $news[&#39;subject&#39;])) { 
        echo "<h5><a href=\"", $news[&#39;href&#39;], "\">", preg_replace("|\[smf\]|i", "", $news[&#39;subject&#39;]), "</a></h5>
        <img src=\"img/mysql.png\" align=\"left\" alt=\"mysql\" />", $news[&#39;body&#39;], "\n";

    }elseif(preg_match("/\[perll\]/i",  $news[&#39;subject&#39;])) { 
        echo "<h5><a href=\"", $news[&#39;href&#39;], "\">", preg_replace("|\[.nl\]|i", "", $news[&#39;subject&#39;]), "</a></h5>
        <img src=\"img/perl.png\" align=\"left\" alt=\"perl\" />", $news[&#39;body&#39;], "\n";
    #if no tag is present, just show the news
    }else{
        echo "<h5><a href=\"", $news[&#39;href&#39;], "\">", $news[&#39;subject&#39;], "</a></h5>
        ", $news[&#39;body&#39;], "\n";
    }
}
?>
--- End code ---



Hope I did someone a favor :)

I'm not sure if it's the most effecient way, but it's the only way I figured out. Should you know of a better one feel free to share :)

[Unknown]:

--- Code: ---<?php 

$array = ssi_boardNews(null, 5, null, 350, &#39;array&#39;);

foreach ($array as $news)
{
// Check which tag is present in the subject, delete it and use the image.
if (strpos(strtolower($news[&#39;subject&#39;]), &#39;[php]&#39;))
{
echo &#39;
<h5><a href="&#39;, $news[&#39;href&#39;], &#39;">&#39;, preg_replace(&#39;|\[php\]|i&#39;, &#39;&#39;, $news[&#39;subject&#39;]), &#39;</a></h5>
<img src="img/php.png" align="left" alt="php" />&#39;, $news[&#39;body&#39;], &#39;<br />&#39;;
}
elseif (strpos(strtolower($news[&#39;subject&#39;]), &#39;[mysql]&#39;))
{
echo &#39;
<h5><a href="&#39;, $news[&#39;href&#39;], &#39;">&#39;, preg_replace(&#39;|\[mysql\]|i&#39;, &#39;&#39;, $news[&#39;subject&#39;]), &#39;</a></h5>
        <img src="img/mysql.png" align="left" alt="mysql" />&#39;, $news[&#39;body&#39;], &#39;<br />&#39;;
}
elseif (strpos(strtolower($news[&#39;subject&#39;]), &#39;[perl]&#39;))
{
echo &#39;
<h5><a href="&#39;, $news[&#39;href&#39;], &#39;">&#39;, preg_replace(&#39;|\[perl\]|i&#39;, &#39;&#39;, $news[&#39;subject&#39;]), &#39;</a></h5>
<img src="img/perl.png" align="left" alt="perl" />&#39;, $news[&#39;body&#39;], &#39;<br />&#39;;
}
// If no tag is present, just show the news.
}
else
{
echo &#39;
<h5>&#39;, $news[&#39;link&#39;], &#39;</h5>
&#39;, $news[&#39;body&#39;], &#39;<br />&#39;;
}
}

?>
--- End code ---

I still think this looks cleaner :P.

-[Unknown]

Jim Yarbro:
I prefer [Unknown]'s code personally. I don't like using double quotes, as you get confused whether you are working with variables sometimes, etc etc. With single quotes it's much easier to work with inline if statements, variables, etc. Now the only thing is needs to go through and change all the . joins to , joins :p

hmm looks like he did already. cant find any . joins anymore  :P

[Unknown]:

--- Quote from: Cheschire on July 04, 2004, 06:00:17 PM ---I prefer [Unknown]'s code personally. I don't like using double quotes, as you get confused whether you are working with variables sometimes, etc etc. With single quotes it's much easier to work with inline if statements, variables, etc. Now the only thing is needs to go through and change all the . joins to , joins :p

--- End quote ---

They already were commas.

-[Unknown]

Jim Yarbro:
i didnt mean in this, i meant SMF source code :)

Navigation

[0] Message Index

[#] Next page

Go to full version