$random :) <-- turned that frown upside down

Post your suggestions for the script here.

Postby twist » Tue Feb 06, 2007 2:44 pm

Yea I've played with it some more, I definitely am getting blank posts showing up on random_single. No errors and the html code is only generating the <!-- Powered by CuteNews.RU | http://cutenews.ru/ | http://cutephp.com/ --> code.



I have 20+ categories, any thoughts on category specific randomization? Also any possibility to be able to randomize per a time period? For instance Picture of the Day and have it randomize once a day or every hour?
twist
 
Posts: 28
Joined: Fri Jan 05, 2007 4:26 am

Postby FI-DD » Tue Feb 06, 2007 7:20 pm

If you have multiple categories per post I have no idea. Otherwise I might find a solution.
User avatar
FI-DD
Admin
 
Posts: 2971
Joined: Thu Sep 22, 2005 11:27 am
Location: Germany

Postby twist » Wed Feb 07, 2007 5:44 pm

Not multiple categories per post. Just multiple categories in general.



For instance I have:



1. Music

2. Movies

3. Games

4. Sports

5. TV



And then I have 5 or so news posts in each category.



On the home page I want to show 1 random item from the games category.



If I use



Code: Select all
<?php

$number = "1";

$template = "gametemplate";

$category = "3";

$random_single = true;

include("cutenews/show_news.php");

?>




Then go to the home page and hit refresh, I will randomly get 1 game item. I hit refresh again I get nothing (blank space). I hit refresh again and I get nothing again. I hit refresh again and I get a different game item. Etc. etc.



On the flip side if I use the code &random='true'; it will randomize the order for the last $number which was set. So set $number=4, it will randomize those 4. If I set it to just 1, it will only display the last item with no randomization. (This works as I'm sure it was intended... and I do not get any blank posts).



Make sense?
twist
 
Posts: 28
Joined: Fri Jan 05, 2007 4:26 am

Postby FI-DD » Wed Feb 07, 2007 7:54 pm

1. If you added the following code before, remove it from inc/show.news.php:

Code: Select all
if($random_single){

   $table_count = $sql->table_count('news');

   $skip = rand(0, $table_count-1);

   $random_single = false;

}




2. Find this:

Code: Select all
$query = $sql->select(array(

       'table'   => 'news',

       'orderby' => $sort,

       'join'      => array('table' => 'story', 'where' => 'id = post_id'),

       'where'   => $where,

       'limit'   => array(($skip ? $skip : 0), $number)

       ));




add below:

Code: Select all
if($random_single){

       foreach($sql->select(array('table' => 'news', 'where' => $where)) as $row){

         $selected_cat_arr[] = $row['id'];

       }

      

       $shuffle = array_rand($selected_cat_arr);

      

       $query = $sql->select(array(

               'table' => 'news',

               'join'    => array('table' => 'story', 'where' => 'id = post_id'),

               'where' => array("id = ".$selected_cat_arr[$shuffle])));

      

       $random_single = false;

}
User avatar
FI-DD
Admin
 
Posts: 2971
Joined: Thu Sep 22, 2005 11:27 am
Location: Germany

Postby twist » Wed Feb 07, 2007 8:16 pm

Flawless!! Nice work! and thank you very much, this was exactly what I needed.
twist
 
Posts: 28
Joined: Fri Jan 05, 2007 4:26 am

Postby Dr.Twister » Sun Sep 16, 2007 8:56 pm

New issue on this came up. If I want to include $random_single twice on the same page but with two different categories, then the categories end up in both includes.

For instance :

Code: Select all
<?PHP
$static['category'] = 1;
$static['template'] = 'Template1';
$static['number'] = 1;
$static['random_single'] = 'true';
include("cn/show_news.php");
?>


<?PHP
$static['category'] = 2;
$static['template'] = 'Template2';
$static['number'] = 1;
$static['random_single'] = 'true';
include("cn/show_news.php");
?>



If I have both of these in a single page then I get some of Category 1 in the Category 2/Template 2 include. I DO NOT get any of Category2 in Template 1 include.

-dt
Dr.Twister
 
Posts: 55
Joined: Mon Apr 23, 2007 4:57 am

Postby scottdallas » Mon Sep 17, 2007 2:58 am

FI-DD wrote:If you have multiple categories per post I have no idea. Otherwise I might find a solution.


Oh so THAT's why I've been having trouble with that.
Everyday I'm tumblin http://scodal.tumblr.com
User avatar
scottdallas
 
Posts: 2209
Joined: Thu May 04, 2006 6:46 pm
Location: US

Postby Yoshiii » Sun Mar 02, 2008 2:19 pm

I use this:

$category = "1,2,3";
$number = 25;

This means i show 25 news (headlines) on the page. When i random, it only randoms between the last 25. But i want him to search through my whole database and show only 25. How can i do that?
Yoshiii
 
Posts: 83
Joined: Fri Feb 29, 2008 9:24 pm

Postby scottdallas » Sun Mar 02, 2008 5:55 pm

Are you using either of the $random variables? You might have trouble specifying categories like 1,2,3, I always do.. that's why I use Main Categories and Sub-Categories that way I can show individual subcategories, or all content from a certain topic or main category. But that's beside the point.

Is it still just randomly showing 25 of the latest news kinda shuffled on each page load? I remember mine did that I don't know if I ever figured it out. And when I say I don't remember if I ever figured it out, what I really mean is I don't remember if FI-DD figured that out and shared it with us lol
Everyday I'm tumblin http://scodal.tumblr.com
User avatar
scottdallas
 
Posts: 2209
Joined: Thu May 04, 2006 6:46 pm
Location: US

Postby Yoshiii » Sun Mar 02, 2008 6:20 pm

If i specifiy 1 category it also just shows the 25 latest. I hope he has a fix for this :(
Yoshiii
 
Posts: 83
Joined: Fri Feb 29, 2008 9:24 pm

Postby alex » Thu Aug 28, 2008 6:25 pm

New issue on this came up. If I want to include $random_single twice on the same page but with two different categories, then the categories end up in both includes.

For instance :
Code:
<?PHP
$static['category'] = 1;
$static['template'] = 'Template1';
$static['number'] = 1;
$static['random_single'] = 'true';
include("cn/show_news.php");
?>


<?PHP
$static['category'] = 2;
$static['template'] = 'Template2';
$static['number'] = 1;
$static['random_single'] = 'true';
include("cn/show_news.php");
?>




If I have both of these in a single page then I get some of Category 1 in the Category 2/Template 2 include. I DO NOT get any of Category2 in Template 1 include.

Anybody found a solution for this.
alex
 
Posts: 24
Joined: Thu Aug 21, 2008 5:05 pm

Postby D72 » Wed Nov 11, 2009 8:37 pm

The time is come that i also need random news articles.
I have tried every option i could find on the forum, but none of them are working properly.
What i want is pretty similar to other requests.

I have these include codes in my page;
Code: Select all
<li><h2><a href="http://www.mydomain.nl/bands.php">Bands</a></h2></li>
<?php
$static['random_single'] = true;
$static['category'] = "4";
$static['number'] = "4";
$static['template'] = "MyTemplate1";
include 'cms/show_news.php';
?>

<li><h2><a href="http://www.mydomain.nl/singers.php">Singers</a></h2></li>
<?php
$static['random_single'] = "4";
$static['category'] = "5";
$static['number'] = "4";
$static['template'] = "MyTemplate2";
include 'cms/show_news.php';
?>


I added one of the latest codes FI-DD made in this topic and the news/titles are randomizing.
Only the the categories are mixed. The include code for category 4 shows one random news article from category 4 and 5
And, it doesn't show 4 articles, but only one per include instead of 4 per include.
Is there any fix or workaround to get this working?
User avatar
D72
 
Posts: 336
Joined: Thu Feb 22, 2007 12:05 am
Location: NL

Postby DarkSlim » Sun Dec 13, 2009 8:47 am

There is another easy solution for one single random post:

Code: Select all
$category = "1,2,3";
$number = "1";
$skip = mt_rand(1,100);
$template = "MyTemplate1";
include 'cms/show_news.php';


Now it will sort all your news in pages, each post will have a page
for itself (because $number = "1"), and using the $skip you can choose
which page-post you will show, so we make it random!
Using the my_rand php function:
The first number (in the example 1) is the lowest number for the random
to choose, for us, start from page/post 1
The second number is the highest number to random, for us, the oldest post

So we make a random between the numbers that we want
and it will show you a random post (just check that you have enough
posts for the numbers you choose)!

You can also make $number = "2";
Then it will show two posts per page (or more if you wish)
but these posts will be (by date) one after the other, not so bad

If you wish to random 2 or more posts, you can add a $sort to
try and make them not one after the other!
For example:
Code: Select all
$category = "1,2,3";
$number = "1";
$skip = mt_rand(1,100);
$sort = array('short', 'asc');
$template = "MyTemplate1";
include 'cms/show_news.php';


This will sort all the news by the number of signs in the short story!
Pretty random I'de say, plus you can also use one of the following:
Code: Select all
$sort = array('full', 'asc');
$sort = array('views', 'asc');
$sort = array('title', 'asc');
$sort = array('comments', 'asc');


Hope it helps, :)

DarkSlim
User avatar
DarkSlim
 
Posts: 298
Joined: Thu Aug 06, 2009 4:18 pm
Location: IL

Previous

Return to Suggestions



Who is online

Users browsing this forum: No registered users and 0 guests

cron