Simple Machines Community Forum

Customizing SMF => Tips and Tricks => Topic started by: Özgür on September 17, 2009, 09:37:16 PM

Title: Call first image of news on SSI boardnews..
Post by: Özgür on September 17, 2009, 09:37:16 PM
I search this in sm.org. But i can't find out. But now, after i learning to regex, this is very easy and useful.

If you don't have any idea for this look at that picture. This is we're final results.

(https://www.simplemachines.org/community/proxy.php?request=http%3A%2F%2Fimg44.imageshack.us%2Fimg44%2F5540%2F71532501.png&hash=9b785f2a207474ceaac9daf52ae48b9747d82892)
http://img44.imageshack.us/img44/5540/71532501.png
As you see, i find first image for post and showed floating left of news text. I work on boardnews function on smf 2.0.

Her you go..

Find in SSI.php
function ssi_boardNews($board = null, $limit = null, $start = null, $length = null, $output_method = 'echo')
{

This is we're function. All changes, should in that function.

Find on that function
        // Check that this message icon is there...
         if (empty($modSettings['messageIconChecks_disable']) && !isset($icon_sources[$row['icon']]))
             $icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.gif') ? 'images_url' : 'default_images_url';


Add After
            // search <img> in $row['body']
   $find = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $row['body'], $value);
   // find src="" value.
if(!empty($value[0]) && !empty($value[1]))
   $first_image = $value [1] [0];

  else{ // if message don't have picture, just show it default image
     $first_image = "http://www.site.com/images/default.jpg";
   }


Find
'subject' => $row['subject'],
Add After
'image' => $first_image,

So function completed. But we not include image in news.
Now we can add in it..

Find
    foreach ($return as $news)
     {
         echo '
             <div>
                 <a href="', $news['href'], '">', $news['icon'], '</a> <b>', $news['subject'], '</b>
                 <div class="smaller">', $news['time'], ' ', $txt['by'], ' ', $news['poster']['link'], '</div>

                 <div class="post" style="padding: 2ex 0;">', $news['body'], '</div>

                 ', $news['link'], $news['locked'] ? '' : ' | ' . $news['comment_link'], '
             </div>';

         if (!$news['is_last'])
             echo '
             <hr style="margin: 2ex 0;" width="100%" />';
     }


Replace for this
foreach ($return as $news)
     {
         echo '
             <div>
                 <a href="', $news['href'], '">', $news['icon'], '</a> <b>', $news['subject'], '</b>
                 <div class="smaller">', $news['time'], ' ', $txt['by'], ' ', $news['poster']['link'], '</div>

                 <div class="post" style="padding: 2ex 0;"><div class="image_border">
                 
                 <img src="', $news['image'], '" alt="', $news['subject'], '" class="news_image" /></div>
                 
                 ', $news['body'], '</div>

                 ', $news['link'], $news['locked'] ? '' : ' | ' . $news['comment_link'], '
             </div>';

         if (!$news['is_last'])
             echo '
             <hr style="margin: 2ex 0;" width="100%" />';
     }

But image and text didn't styled. And not seems like cool..  I already add class="" prority to image

Add that styles in where you used that function.


.image_border
{
float:left;
background:#ddd;
border:1px solid #ccc;}

.news_image{
width:150px;
height:150px;
padding:5px;
margin:3px;
}

Yeah cool now. But image called twice. And if news text centered or aligned to left,  displaying corrupted.

I use my strip tags on the news text

Add this to SSI.php last line (befor ?>)
function temizle($haber) {
     $strs=explode('<',$haber);
     $res=$strs[0];
     for($i=1;$i<count($strs);$i++)
     {
         if(!strpos($strs[$i],'>'))
             $res = $res.'&lt;'.$strs[$i];
         else
             $res = $res.'<'.$strs[$i];
     }
     return strip_tags($res);   
}


And replace to news body.
Find
  ', $news['body'], '</div>

Replace
  ', temizle($news['body']), '</div>

And final...
- Daydreamer
Title: Re: Call first image of news on SSI boardnews..
Post by: Arantor on September 20, 2009, 01:01:19 PM
Nice work! I'll move this to Tips & Tricks since that's really what it is.
Title: Re: Call first image of news on SSI boardnews..
Post by: Googgig on October 08, 2009, 09:25:54 PM
Thank you for this code. It's work!
Anyway I have a little problem. If the topic has smiley its will call smiley to the first. No matter there are 3-4 images before smiley, it will show smiley first.
Title: Re: Call first image of news on SSI boardnews..
Post by: Özgür on October 09, 2009, 01:45:03 AM
Hmm. I will look today afternoon, i'm still busy on my profile song mod sorry.
Title: Re: Call first image of news on SSI boardnews..
Post by: Alpay on June 05, 2011, 01:27:42 PM
Thank you Özgür. Goooood Work..
Title: Re: Call first image of news on SSI boardnews..
Post by: MyTime on November 23, 2014, 03:21:08 PM
Could someone explain what exactly this does? I think it's something I might be interested in, with adding an image too an article in a block
Title: Re: Call first image of news on SSI boardnews..
Post by: Xpresskonami on August 24, 2017, 07:52:43 PM
not working for me, any solution