News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Showing "category" images with boardnews ssi

Started by Tim, July 04, 2004, 12:46:04 PM

Previous topic - Next topic

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:

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

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

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




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]

<?php 

$array 
ssi_boardNews(null5null350'array');

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

?>


I still think this looks cleaner :P.

-[Unknown]

Cccddd

#2
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

They already were commas.

-[Unknown]

Cccddd

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

Cccddd

#5
My own little dooah here... got rid of all the extra bracks, and made it variable so that it's more easily expandable, and the code ports easier. also there were a couple bugs.. cant remember the second one but hte first one was that if the variable thing came at the front of the subject, it wouldnt work cuz the strpos was 0, which wouldnt register properly. !== false took care of that.

<?php 

use [Unknown]'s;

?>


[Unknown]

Oops, I knew I forgot something, meant to add the !== false on.

<?php 

$array 
ssi_boardNews(null5null350'array');
$newsCategories = array('php','mysql','perl');

foreach (
$array as $news)
{
  
$found false;

  
// Check which tag is present in the subject, delete it and use the image.
  
foreach ($newsCategories as $cat)
  {
    if (
strpos(strtolower($news['subject']), '[' $cat ']') !== false)
    {
      echo 
'
            <h5><a href="'
$news['href'], '">'preg_replace('~\[' $cat '\]~i'''$news['subject']), '</a></h5>
            <img src="img/'
$cat'.png" align="left" alt="'$cat'" />'$news['body'], '<br />';
      
$found true;
    }
  }

  
// If no tag is present, just show the news.
  
if (!$found)
      echo 
'
            <h5><a href="'
$news['href'], '">'$news['subject'], '</a></h5>
            '
$news['body'], '<br />';
}

?>


I have this big time pet peeve with using 1/0 when you want true/false.

-[Unknown]

Cccddd

yeah its something im trying to get in the habit of not doing. its just a bad ol habit from perl days :)

Tim


Advertisement: