Customizing SMF > Tips and Tricks

Call first image of news on SSI boardnews..

(1/2) > >>

Ö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
 
--- Code: ---function ssi_boardNews($board = null, $limit = null, $start = null, $length = null, $output_method = 'echo')
 {
--- End code ---
This is we're function. All changes, should in that function.

Find on that function
 
--- Code: ---        // 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';
--- End code ---

Add After
 
--- Code: ---            // 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";
   }
 
--- End code ---

Find
 
--- Code: ---'subject' => $row['subject'],
--- End code ---
Add After
 
--- Code: ---'image' => $first_image,
--- End code ---

So function completed. But we not include image in news.
Now we can add in it..
 
 Find
 
--- Code: ---    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%" />';
     }
--- End code ---

Replace for this
 
--- Code: ---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%" />';
     }
--- End code ---
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.
 
 
--- Code: --- .image_border
 {
 float:left;
 background:#ddd;
 border:1px solid #ccc;}
 
 .news_image{
 width:150px;
 height:150px;
 padding:5px;
 margin:3px;
 }
 
--- End code ---
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 ?>)
 
--- Code: ---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);   
 }
--- End code ---

And replace to news body.
Find
 
--- Code: ---  ', $news['body'], '</div>
--- End code ---

Replace
 
--- Code: ---  ', temizle($news['body']), '</div>
--- End code ---

And final...
- Daydreamer

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.

Alpay:
Thank you Özgür. Goooood Work..

Navigation

[0] Message Index

[#] Next page

Go to full version