Fog Creek Software
Discussion Board




Knowledge Base
Documentation
Terry's Tips
Darren's Tips

Any Random Capabilities?

I'm using several scripts to find and sort articles. I've been selecting the 10 most recent articles, but I'd like to find a way to randomly select 10 articles from a folder.

I'm not interested in doing it manually, because it would be very high maintenance. Instead, I'm wondering if there's any way to have a script select the articles that meet a criteria, and then use only a random selection of these.

Let me know if you have any ideas.

Thanks!

Jim L
Monday, December 01, 2003

You would need to do it with javascript or server scripting (asp, php, etc.)

joel goldstick
Monday, December 01, 2003

However you do it in CD, you'll get a selection that only changes when you republish. Is that what you want? If you want every page view to generate a different selection, you have to do it on the server - or do something horrible like publish all the links with <div style="display:none"> link goes here </div> and write a piece of JS that randomly unhides 10 of them. Messy.

Michael Wild
Monday, December 01, 2003

I'm setting up a simple rotating ad function - integrated into a CityDesk site (using PHP).

It took me a few minutes to modify my code so that when you hit publish in CityDesk - it will update a list of all articles, and then the PHP side of things randomly picks out ten articles to display.

Note that the php code could display the same link more than once in the list.

Might help....

Dan
Thursday, December 04, 2003

The code to insert:

<?php

$hreflist = array("");

//Here's where you use CityDesk to tell PHP about which files to randomly choose from
{$ forEach x in all $}
    $hreflist[] = "<a href={$x.link$}>{$x.filename$}</a>";
{$next $}

//PHP will loop through 10 times and display content from hreflist array
for ($x = 0; $x < 10; $x++) {
    srand((double) microtime() * 1000000 );
    $RandomNumber = rand (1, count($hreflist)-1);
    print("$hreflist[$RandomNumber]<br>");
}
?>

Dan
Thursday, December 04, 2003

Thanks for the info.

I'd like to avoid server-side scripting, because I'm more interested in randomizing what happens when I publish.

For example, my site has a blogroll, but it's gotten so large that I can't put it all on the page. Instead of having older links drop off, I'd like to randomly select 20 of them.

Similarly, I'd like to be able to select 5 articles at random and include links to them from my home page.

Any suggestions?

Jim L
Thursday, December 04, 2003

Cityscript is a tool that helps pull together the pieces of your articles and at the same time can create menus from the structure of your articles.  But it doesn't do any number calculating, and it doesn't have a randomizing function.  By the time you publish, it has created a static version of your site.

That said, your interest in changing a blog list at publish time is a different interest than serving up random links each time a reader requests a page.  I think you can still do what you want with server scripting.  Here's my idea:

Instead of creating a random 5 featured links each time the user requests your page, create the 5 links with a script that runs after you publish.  CD lets you run a program after you publish.  This little program could generate a simple text file with 5 random links.  Then, using server side code you could read this file to produce your page links.  Since the file only builds each time you publish, the links only change when you publish.

To get a list of possible links, create a special template which does a foreach loop on the folder where all of your links are placed.  This template can produce a text file, instead of html, that will be easy for your server script to parse.

joel goldstick
Thursday, December 04, 2003

That was sloppy.  Ill try again:

Make a template to produce a list of all possible links.  Call it allLinks.  Create an empty article, make it use allLinks, and call it AllMyLinks.  It will be called AllMyLinks.txt when it is published.

Write a php script or perl, or vb or whatever that CD will run after you publish.  It reads AllMyLinks.txt, picks 5 links and re-writes the file.

Then CD uploads your files to your host.  Your server script uses AllMyLinks.txt to build the 5 links you want to show on your page.

joel goldstick
Thursday, December 04, 2003

Use the date/time functions to generate a seed, which will be fixed at publish-time. Then write some javascript to use this to pick some articles.

Michael Wild
Friday, December 05, 2003

*  Recent Topics

*  Fog Creek Home