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

Post your suggestions for the script here.

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

Postby scottdallas » Sat Oct 07, 2006 11:52 pm

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
Last edited by scottdallas on Mon Oct 16, 2006 12:21 am, edited 1 time in total.
Everyday I'm tumblin http://scodal.tumblr.com
User avatar
scottdallas
 
Posts: 2209
Joined: Thu May 04, 2006 6:46 pm
Location: US

Postby FI-DD » Tue Oct 10, 2006 10:47 pm

Try this:



Open inc/show.news.php and find this:



Code: Select all
foreach ($query as $bg => $row){




add above:

Code: Select all
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.
User avatar
FI-DD
Admin
 
Posts: 2971
Joined: Thu Sep 22, 2005 11:27 am
Location: Germany

Postby scottdallas » Wed Oct 11, 2006 11:06 pm

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.
Everyday I'm tumblin http://scodal.tumblr.com
User avatar
scottdallas
 
Posts: 2209
Joined: Thu May 04, 2006 6:46 pm
Location: US

Postby FI-DD » Wed Oct 11, 2006 11:17 pm

I just tested it myself, and it's working for me.
User avatar
FI-DD
Admin
 
Posts: 2971
Joined: Thu Sep 22, 2005 11:27 am
Location: Germany

Postby scottdallas » Thu Oct 12, 2006 3:14 am

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.



Code: Select all
<?php

$number = "1";

$random = TRUE;

include("cutenews/show_news.php");

?>




Does that look right?



I've tried that and



Code: Select all
<?php

$number = "1";

$random = "true";

include("cutenews/show_news.php");

?>
Everyday I'm tumblin http://scodal.tumblr.com
User avatar
scottdallas
 
Posts: 2209
Joined: Thu May 04, 2006 6:46 pm
Location: US

Postby FI-DD » Thu Oct 12, 2006 11:44 am

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:

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)

       ));




and add above:



Code: Select all
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.
User avatar
FI-DD
Admin
 
Posts: 2971
Joined: Thu Sep 22, 2005 11:27 am
Location: Germany

Postby scottdallas » Thu Oct 12, 2006 9:36 pm

:D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D AWESOME!
Everyday I'm tumblin http://scodal.tumblr.com
User avatar
scottdallas
 
Posts: 2209
Joined: Thu May 04, 2006 6:46 pm
Location: US

Postby GoldenGonaz » Wed Oct 18, 2006 5:07 pm

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?
GoldenGonaz
 
Posts: 32
Joined: Wed Oct 04, 2006 1:00 pm

Postby scottdallas » Wed Oct 18, 2006 6:08 pm

You could fake it. Use the second example so you're using $random_single and instead of what you're probably doing:



Code: Select all
<?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.



Code: Select all
<?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?
Everyday I'm tumblin http://scodal.tumblr.com
User avatar
scottdallas
 
Posts: 2209
Joined: Thu May 04, 2006 6:46 pm
Location: US

Postby GoldenGonaz » Wed Oct 18, 2006 6:28 pm

scottdallas wrote:You could fake it. Use the second example so you're using $random_single and instead of what you're probably doing:



Code: Select all
<?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.



Code: Select all
<?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!
GoldenGonaz
 
Posts: 32
Joined: Wed Oct 04, 2006 1:00 pm

Postby scottdallas » Thu Oct 19, 2006 2:21 pm

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 :P
Everyday I'm tumblin http://scodal.tumblr.com
User avatar
scottdallas
 
Posts: 2209
Joined: Thu May 04, 2006 6:46 pm
Location: US

Postby scottdallas » Wed Oct 25, 2006 1:38 am

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.
Everyday I'm tumblin http://scodal.tumblr.com
User avatar
scottdallas
 
Posts: 2209
Joined: Thu May 04, 2006 6:46 pm
Location: US

Postby x-byte » Sat Feb 03, 2007 7:39 pm

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.
x-byte
 
Posts: 170
Joined: Mon Nov 14, 2005 6:31 am

Postby scottdallas » Sun Feb 04, 2007 12:14 am

It randomizes my entire database :shock:
Everyday I'm tumblin http://scodal.tumblr.com
User avatar
scottdallas
 
Posts: 2209
Joined: Thu May 04, 2006 6:46 pm
Location: US

Postby twist » Mon Feb 05, 2007 8:09 pm

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:



Code: Select all
<?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



Code: Select all
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.
twist
 
Posts: 28
Joined: Fri Jan 05, 2007 4:26 am

Next

Return to Suggestions



Who is online

Users browsing this forum: No registered users and 1 guest