Advertisement:

Author Topic: Showing "category" images with boardnews ssi  (Read 25817 times)

Offline Tim

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 2,857
  • Gender: Male
  • Marketing Consultant
    • tim.ceuppens on Facebook
    • http://www.linkedin.com/in/timceuppens on LinkedIn
    • @timcbe on Twitter
    • Tim Ceuppens
Showing "category" images with boardnews ssi
« on: July 04, 2004, 12:46:04 PM »
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: [Select]
<?php 
$array 
ssi_boardNews(null5null350, &#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";
    }
}
?>



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 :)
« Last Edit: October 06, 2004, 09:29:30 PM by Tim »

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: [ssi] Showing slashdot style boardnews
« Reply #1 on: July 04, 2004, 05:52:44 PM »
Code: [Select]
<?php 

$array 
ssi_boardNews(null5null350, &#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;;
}
}

?>

I still think this looks cleaner :P.

-[Unknown]

Offline Jim Yarbro

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 1,592
  • a.k.a. Cheschire
    • Warpstorm
Re: [ssi] Showing slashdot style boardnews
« Reply #2 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

hmm looks like he did already. cant find any . joins anymore  :P
« Last Edit: July 04, 2004, 06:02:55 PM by Cheschire »
Just because SMF isn't as bloated as you want it from the start doesn't mean it can't be. -[Unknown]

^ This means keep Simple Machines SIMPLE, and let people make ****** as complex as they want on their own. Stop making ****** so complex in the core!!

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: [ssi] Showing slashdot style boardnews
« Reply #3 on: July 04, 2004, 06:01:59 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]

Offline Jim Yarbro

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 1,592
  • a.k.a. Cheschire
    • Warpstorm
Re: [ssi] Showing slashdot style boardnews
« Reply #4 on: July 04, 2004, 06:03:23 PM »
i didnt mean in this, i meant SMF source code :)
Just because SMF isn't as bloated as you want it from the start doesn't mean it can't be. -[Unknown]

^ This means keep Simple Machines SIMPLE, and let people make ****** as complex as they want on their own. Stop making ****** so complex in the core!!

Offline Jim Yarbro

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 1,592
  • a.k.a. Cheschire
    • Warpstorm
Re: [ssi] Showing slashdot style boardnews
« Reply #5 on: July 04, 2004, 06:05:13 PM »
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.

Code: [Select]
<?php 

use [Unknown]&#39;s;

?>
« Last Edit: July 05, 2004, 11:10:39 AM by Cheschire »
Just because SMF isn't as bloated as you want it from the start doesn't mean it can't be. -[Unknown]

^ This means keep Simple Machines SIMPLE, and let people make ****** as complex as they want on their own. Stop making ****** so complex in the core!!

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: [ssi] Showing slashdot style boardnews
« Reply #6 on: July 04, 2004, 08:52:26 PM »
Oops, I knew I forgot something, meant to add the !== false on.

Code: [Select]
<?php 

$array 
ssi_boardNews(null5null350, &#39;array&#39;);
$newsCategories = array(&#39;php&#39;,&#39;mysql&#39;,&#39;perl&#39;);

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[&#39;subject&#39;]), &#39;[&#39; . $cat . &#39;]&#39;) !== false)
    
{
      echo &
#39;
            
<h5><a href="&#39;, $news[&#39;href&#39;], &#39;">&#39;, preg_replace(&#39;~\[&#39; . $cat . &#39;\]~i&#39;, &#39;&#39;, $news[&#39;subject&#39;]), &#39;</a></h5>
            
<img src="img/&#39;, $cat, &#39;.png" align="left" alt="&#39;, $cat, &#39;" />&#39;, $news[&#39;body&#39;], &#39;<br />&#39;;
      
$found true;
    }
  }

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

?>

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

-[Unknown]

Offline Jim Yarbro

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 1,592
  • a.k.a. Cheschire
    • Warpstorm
Re: [ssi] Showing slashdot style boardnews
« Reply #7 on: July 04, 2004, 09:12:52 PM »
yeah its something im trying to get in the habit of not doing. its just a bad ol habit from perl days :)
Just because SMF isn't as bloated as you want it from the start doesn't mean it can't be. -[Unknown]

^ This means keep Simple Machines SIMPLE, and let people make ****** as complex as they want on their own. Stop making ****** so complex in the core!!

Offline Tim

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 2,857
  • Gender: Male
  • Marketing Consultant
    • tim.ceuppens on Facebook
    • http://www.linkedin.com/in/timceuppens on LinkedIn
    • @timcbe on Twitter
    • Tim Ceuppens
Re: [ssi] Showing slashdot style boardnews
« Reply #8 on: July 05, 2004, 09:02:28 AM »
isn't open source swell :)