Please login or register.

Login with username, password and session length
Advanced search  

News:

You need/want an older version of sNews ? Download an older/unsupported version here.

Author Topic: [MOD] Random Articles  (Read 1241 times)

mfaraklit

  • Newbie
  • *
  • Karma: 1
  • Posts: 20
[MOD] Random Articles
« on: January 15, 2011, 10:06:39 PM »

Do you want to display random articles from all published posts in your site? If it is yes, the mentioned mod is so easy to apply...

Note: This mod is for Snews 1.6 and its sub versions

In this mod, we use the random property of MySQL: RAND(). Random mode shows new random articles when pressing F5 or reload page.

Firstly, take your backup of "core.php". Than open it and apply following pieces of codes:

Code: [Select]
// RANDOM ARTICLES
function random_articles($size) {
# select all published articles ordered by view column and limiting the output with numeric $size variable
$query = "SELECT * FROM ".db('prefix')."articles WHERE position = 1 AND published = 1 ORDER BY RAND() LIMIT $size";
$result = mysql_query($query);
# "home" title defined outside while-loop
$home = s('home_sef');
#optional formatting (uncomment if needed)
//echo '<ul>';
while ($r = mysql_fetch_array($result)) {
# find category SEF title (used to build links)
$categorySEF = find_cat_sef($r['category']);
# defining category name: if zero, we're at home, else retrieve category name
$categoryName = $r['category'] == 0 ? $home : retrieve('name', 'categories', 'seftitle', $categorySEF);
echo '<li><a href="'.db('website').$categorySEF.'/'.$r['seftitle'].'/" title="'.$r['title'].' - '.l('posted_in').' '.$categoryName.'">'.$r['title'].'</a></li>';
}
#optional formatting (uncomment if needed)
//echo '</ul>';
}

You can appy above codes wherever you want in core.php


Secondy, apply following code in your index.php wherever you want:

Code: [Select]
<ul><?php random_articles(10); ?></ul>
at above code, the number 10 describes how many randomly display articles in your page.



Demo: http://www.e-siber.com - you can see working example of random mode in the right side of page (RASTGELE YAZILAR)
Logged

Armen

  • Sr. Member
  • ****
  • Karma: 41
  • Posts: 338
    • http://www.funnydays.ru
Re: [MOD] Random Articles
« Reply #1 on: January 25, 2011, 05:30:27 PM »

Well done mfaraklit,

Just wanted to warn you, that if you have >300 articles in your sNews DB ORDER BY RAND() can slow down your website significantly.

http://translate.google.ru/translate?hl=ru&ie=UTF-8&oe=utf-8&sl=ru&tl=en&u=http://webdev.cz.cc/mysql/4-optimization/11-mysql-optimizaciya-order-by-rand&twu=1&client=firefox

Machine translated from Russian to English article about ways to optimize this solution.

Cheers.
Logged
Now ogres, oh, they're much worse. They'll make a suit from your freshly peeled skin. They'll shave your liver, squeeze the jelly from your eyes... Actually, it's quite good on toast.

Sven

  • ULTIMATE member
  • ******
  • Karma: 88
  • Posts: 2029
  • Chasing MY bugs!
    • hiseo.fr - rédacteur Web
Re: [MOD] Random Articles
« Reply #2 on: January 29, 2011, 02:55:23 PM »

Armen is back!!!  8)

Armen

  • Sr. Member
  • ****
  • Karma: 41
  • Posts: 338
    • http://www.funnydays.ru
Re: [MOD] Random Articles
« Reply #3 on: October 25, 2011, 03:55:41 PM »

Armen is back!!!  8)

Hi Sven =)

On the topic I'd like to add that I've been using solution I mentioned earlier a lot since the post. It is really great.

Still, for an ordinary sNews installation using RAND() for ordering shouldn't be a problem, I assume.
Logged
Now ogres, oh, they're much worse. They'll make a suit from your freshly peeled skin. They'll shave your liver, squeeze the jelly from your eyes... Actually, it's quite good on toast.