News:

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

Main Menu

Call first image of news on SSI boardnews..

Started by Özgür, September 17, 2009, 09:37:16 PM

Previous topic - Next topic

Özgür

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.


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
So Long

Arantor

Nice work! I'll move this to Tips & Tricks since that's really what it is.

Googgig

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.

Özgür

Hmm. I will look today afternoon, i'm still busy on my profile song mod sorry.
So Long

Alpay


MyTime

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

Xpresskonami


Advertisement: