| Author |
Message |
scottdallas

Joined: 04 May 2006 Posts: 1843 Location: US |
|
$random :) <-- turned that frown upside down |
|
I know I've probably mentioned it before.. I really miss $random in cnr 2.5.4 how hard is it to add? I remember doing some research on it before and trying some things then failing.. it's the only thing that has me going back to cn 1.3.6-1.4.1 once in a while. Does strawberry have a $random function?
Random + XFields = Fun
_________________ www.scottdizzle.com uses cnr
last update: 07-22-08: 8:30 pm
Last edited by scottdallas on Mon Oct 16, 2006 12:21 am; edited 1 time in total |
|
| Sat Oct 07, 2006 11:52 pm |
|
 |
Guest
|
|
|
Please login to hide the ads.
|
|
|
|
 |
FI-DD
Admin

Joined: 22 Sep 2005 Posts: 2736 Location: Germany |
|
|
|
Try this:
Open inc/show.news.php and find this:
 |  | foreach ($query as $bg => $row){ |
add above:
 |  | if(!function_exists('shuffle_it')){
function shuffle_it($array) {
while (count($array) > 0) {
$val = array_rand($array);
$new_arr[$val] = $array[$val];
unset($array[$val]);
}
return $new_arr;
}
}
if($random == TRUE){ $query = shuffle_it($query); } |
Use $random = TRUE in your include code.
|
|
| Tue Oct 10, 2006 10:47 pm |
|
 |
scottdallas

Joined: 04 May 2006 Posts: 1843 Location: US |
|
|
|
Well I tried it but no go. I guess I'll live without it and stop pestering you about it I just really loved that feature. Had so much potential.
_________________ www.scottdizzle.com uses cnr
last update: 07-22-08: 8:30 pm |
|
| Wed Oct 11, 2006 11:06 pm |
|
 |
FI-DD
Admin

Joined: 22 Sep 2005 Posts: 2736 Location: Germany |
|
|
|
I just tested it myself, and it's working for me.
|
|
| Wed Oct 11, 2006 11:17 pm |
|
 |
scottdallas

Joined: 04 May 2006 Posts: 1843 Location: US |
|
|
|
oh well let me have another go at it... think i could have any server side issues like i seem to always have? i doubt it.. i must be doing something wrong.. i just tried on my advanced godaddy account, my regular godaddy account, and localhost and none of them want to do it. i dont think it's your fault though.. definitely something im doing wrong.
 |  | <?php
$number = "1";
$random = TRUE;
include("cutenews/show_news.php");
?> |
Does that look right?
I've tried that and
 |  | <?php
$number = "1";
$random = "true";
include("cutenews/show_news.php");
?> |
_________________ www.scottdizzle.com uses cnr
last update: 07-22-08: 8:30 pm |
|
| Thu Oct 12, 2006 3:14 am |
|
 |
FI-DD
Admin

Joined: 22 Sep 2005 Posts: 2736 Location: Germany |
|
|
|
You're right. The above code only works when you display more than one news per page.
If you want to show a random single post find this in inc/show.news.php:
 |  | $query = $sql->select(array(
'table' => 'news',
'orderby' => $sort,
'join' => array('table' => 'story', 'where' => 'id = post_id'),
'where' => $where,
'limit' => array(($skip ? $skip : 0), $number)
)); |
and add above:
 |  | if($random_single){
$table_count = $sql->table_count('news');
$skip = rand(0, $table_count-1);
$random_single = false;
} |
Add $random_single = true; to your include code.
|
|
| Thu Oct 12, 2006 11:44 am |
|
 |
scottdallas

Joined: 04 May 2006 Posts: 1843 Location: US |
|
|
| Thu Oct 12, 2006 9:36 pm |
|
 |
GoldenGonaz
Joined: 04 Oct 2006 Posts: 28
|
|
|
|
How do I show 4 random articles?
I tried both methods above none gave the right result.
With the first method, it only randomly displayed the 4 latest articles in a different order, instead of showing ANY 4 random articles in any order.
The second method, $random_single = true; doesn't work properly for 4 random articles either.
What would need to be added/changed to allow ANY 4 random articles from all categoeries from any date?
|
|
| Wed Oct 18, 2006 5:07 pm |
|
 |
scottdallas

Joined: 04 May 2006 Posts: 1843 Location: US |
|
|
|
You could fake it. Use the second example so you're using $random_single and instead of what you're probably doing:
 |  | <?php
$number = "4";
$template = "yourtemplate";
$category = "1";
$random = true;
include("cutenews/show_news.php");
?> |
Do this.. I know it looks funny and is a pain.. but it'll show up on your page for your visitors correctly I promise.. even if it's rigged.
 |  | <?php
$number = "1";
$template = "yourtemplate";
$category = "1";
$random_single = true;
include("cutenews/show_news.php");
?>
<?php
$number = "1";
$template = "yourtemplate";
$category = "1";
$random_single = true;
include("cutenews/show_news.php");
?>
<?php
$number = "1";
$template = "yourtemplate";
$category = "1";
$random_single = true;
include("cutenews/show_news.php");
?>
<?php
$number = "1";
$template = "yourtemplate";
$category = "1";
$random_single = true;
include("cutenews/show_news.php");
?> |
See what I mean?
_________________ www.scottdizzle.com uses cnr
last update: 07-22-08: 8:30 pm |
|
| Wed Oct 18, 2006 6:08 pm |
|
 |
GoldenGonaz
Joined: 04 Oct 2006 Posts: 28
|
|
|
|
 |  | You could fake it. Use the second example so you're using $random_single and instead of what you're probably doing:
 |  | <?php
$number = "4";
$template = "yourtemplate";
$category = "1";
$random = true;
include("cutenews/show_news.php");
?> |
Do this.. I know it looks funny and is a pain.. but it'll show up on your page for your visitors correctly I promise.. even if it's rigged.
 |  | <?php
$number = "1";
$template = "yourtemplate";
$category = "1";
$random_single = true;
include("cutenews/show_news.php");
?>
<?php
$number = "1";
$template = "yourtemplate";
$category = "1";
$random_single = true;
include("cutenews/show_news.php");
?>
<?php
$number = "1";
$template = "yourtemplate";
$category = "1";
$random_single = true;
include("cutenews/show_news.php");
?>
<?php
$number = "1";
$template = "yourtemplate";
$category = "1";
$random_single = true;
include("cutenews/show_news.php");
?> |
See what I mean? |
Yeh, great idea! Thanks, I'll try and set it up now, my only worry is it might if a visitor is really unlucky show 2 of the same, 3 of the same or maybe even all 4!
|
|
| Wed Oct 18, 2006 6:28 pm |
|
 |
scottdallas

Joined: 04 May 2006 Posts: 1843 Location: US |
|
|
|
mm true but it's probably pretty unlikely. I hadn't thought of it like that. If your database is or gets large enough the odds will get slimmer 
_________________ www.scottdizzle.com uses cnr
last update: 07-22-08: 8:30 pm |
|
| Thu Oct 19, 2006 2:21 pm |
|
 |
scottdallas

Joined: 04 May 2006 Posts: 1843 Location: US |
|
|
|
Can we zip the modified show.news.php file and put it in the additional downloads? I think people'd love to see it there. Just download and overwrite existing. I'll upload zip if you want unless this isn't worthy of that section. I really find it handy.
_________________ www.scottdizzle.com uses cnr
last update: 07-22-08: 8:30 pm |
|
| Wed Oct 25, 2006 1:38 am |
|
 |
x-byte
Joined: 14 Nov 2005 Posts: 170
|
|
|
|
Yea, this only randomize the first number of post (the number you choose) and not from the entire database. Would be great to have a fully functional one.
|
|
| Sat Feb 03, 2007 7:39 pm |
|
 |
scottdallas

Joined: 04 May 2006 Posts: 1843 Location: US |
|
|
|
It randomizes my entire database 
_________________ www.scottdizzle.com uses cnr
last update: 07-22-08: 8:30 pm |
|
| Sun Feb 04, 2007 12:14 am |
|
 |
twist
Joined: 05 Jan 2007 Posts: 28
|
|
|
|
Yea it randomizes the entire database, but I think that is where the problem lies. It will random all news, but it will only display what you have assigned for your category. It's not randomizing within the category.
So it would appear if I had something like this setup:
 |  | <?PHP
$static['template'] = 'Music;
$static['number'] = 1;
$static['category'] = 5;
$random_single = true;
("cnr/show_news.php");
?>
<?PHP
$static['template'] = 'Movies;
$static['number'] = 1;
$static['category'] = 7;
$random_single = true;
("cnr/show_news.php");
?> |
Using this script inside show.news.php
 |  | if($random_single){
$table_count = $sql->table_count('news');
$skip = rand(0, $table_count-1);
$random_single = false;
} |
It seems as if it calculates the total number of news items for both categories 5 and 7 and randomizes a single ID. So if the ID the $random_single identifies in the Music category is actually a movie, it will display nothing at all.
OR it is Randomizing the Categories themselves and will only display the random item if it falls on the same category that was identified in the embed code.
|
|
| Mon Feb 05, 2007 8:09 pm |
|
 |
|