random blog list
So you have a really long list of blogs. But you only want a select few to show up on your main page, because that list is just too long and it overwhelms your site.
Here's how you do it (in php)…
Make your list of blog links – one blog link per line. You can even have little decorative characters around the links… Like, on Christine's blog, she has those little "::" before each link. Just make sure you don't have extra returns in there…
then add this code where you want your random blog links to show up:
<?
$array = file("/path/to/your/file/bloglinks.php");
shuffle($array);
for ($i=0; $i<10; $i++) {
echo $array[$i];
}
?>
This will show 10 random links on your page. Change the "<10" to <20 or <25 or however many you want to show…
assumptions:
1) you've named your file: bloglinks.php
2) you're able to run php scripts on this page
————————————
in case you don't want to read through the comments – I've moved this code into the main post…
If you want to alphabetize that list:
<?
$array = file("/path/to/your/list/of/blogs/bloglinks.php");
shuffle($array);
for ($i=0; $i<10; $i++) {
$miniarray[$i] = $array[$i];
}
sort($miniarray);
for ($i=0; $i<count($miniarray); $i++) {
echo $miniarray[$i];
}
?>
and make your bloglinks file look like this:
<!– A life uncommon –> <a href="etc etc.
<!– Blahblahblog –> <a href="etc. etc.
Additional note: if it seems like you're list is being pulled in completely, and you're on a Mac – there is something about text files edited on a Mac where the "returns" aren't read as "returns" by the server… you might want to try this method:
Between each blog line add "|||" so your list would look like this:
<!– a life uncommon –> <a href="http://a.lifeuncommon.com">a life uncommon</a><br>|||
<!– big pink cookie –> <a href="http://www.bigpinkcookie.com">big pink cookie</a><br>
etc.
Then where you want your list to show up, use this code:
<?
$link_array = file("/path/to/your/list/of/blogs/bloglinks.php");
$link_array2 = implode("", $link_array);
$link_array3 = explode ("|||", $link_array2);
shuffle($link_array3);
for ($i=0; $i<10; $i++) {
$miniarray[$i] = $link_array3[$i];
}
sort($miniarray);
for ($i=0; $i<count($miniarray); $i++) {
echo $miniarray[$i];
}
?>
(change that "10" in the code above to the number of links you want to display)
May 26th, 2002 at 9:24 am
Heyhey,
So, I got it working! Turns out, my HTML text editor has a saving file prefernce that can save your line breaks as Windows, instead, and that worked!
(Would Unix line breaks would have worked?)
Also, how would I get my long regular list of links to be ABC'd (without shuffling) on another page? So I don't have to keep ABCing it myself.
Thanks for all the help,
Liz
May 26th, 2002 at 11:28 am
Jennifer thank you!
May 26th, 2002 at 9:01 am
Sara – yeah – on your page that you want to show all the links, where you want them to show up do this:
<?
$array = file("bloglinks.php");
sort($array);
foreach($array as $link) {
echo $link;
}
?>
Liz – you can also try that other way of setting up the links list (001 = …link; 002=…link) – that should work whether you're on a Mac or not.
May 31st, 2002 at 8:09 am
Thanks so much for the code!!
June 27th, 2002 at 9:09 am
i have the "sorted" code in my page… i just don't know why the links don't appear…
http://www.ummhmm.net/z/iblog/
help… =)
June 27th, 2002 at 9:10 am
i have the "sorted" code in my page… i just don't know why the links don't appear…
http://www.ummhmm.net/z/iblog/
help… =)
September 3rd, 2002 at 6:15 am
I think this script can be slightly changed to allow it to display random images yes?
in bloglinks.php simply put
<imf src="path/to/image.jpg>
with necessary size and alt text for each
and in the array, call 1 instead of 10
yeah, I think that could work!
January 26th, 2003 at 3:00 am
in bloglinks.php simply put
with necessary size and alt text for each
and in the array, call 1 instead of 10
yeah, I think that could work!
yess
February 6th, 2003 at 10:44 pm
Oooh! I'm goint to have to try it for the pictures
Just wondering if – for the blog links – this will work with a target tag……?
<!– happy pills –> <a href="http://www.happy-pills.org/" target="new">happy pills</a><br>|||
I'm just getting my list together now but I'm dead tired so I won't be posting it till the morning…..just wondering if anyone had tried that……
I missed all these fun scripts
May 30th, 2003 at 6:14 pm
Absolutely love this script Jennifer!! Thank you so much!!!
June 22nd, 2003 at 2:42 pm
What if I wanted to have MT make an automatic list of entries for me? So, like I would like this script to pull random entries out of the php file instead of links. Can I make MT generate a list of entry links without going in and adding every entry manually?
June 22nd, 2003 at 9:18 pm
Jane, the easiest way to show random entries is with the RandomEntries plugin; just install and you're random – check it out in my sidebar if you want to see it in action. Like all plugins, it changes the entry on rebuild, not on a new page load, but it's random enough for me!
August 9th, 2003 at 2:43 pm
Thankyou for this marvellous script! I have been looking for this everywhere (I should have known to come here first).
September 24th, 2003 at 12:39 am
This script is good. This way you don't have to use blogrolling.
What I am wanting to do with it is have it set up in rows, but the way I do it it puts duplicate urls in. Is there a way to make it so it doesn't take duplicates?
December 1st, 2003 at 11:52 am
Thanks! Very cool PHP random list HTML tag generator.
February 22nd, 2004 at 4:42 pm
Is it possible to modify this code for it to show the given amount of links for 1 day and not show the same links twice for a given amount of days?
February 28th, 2004 at 12:55 am
You'd use natecasesort() rahter than sort.
ciao
Alberto
http://www.unitedscripters.com/
April 8th, 2004 at 2:34 am
hello Scripty G,
love your script .. just looking now for a way i can offer people the whole list as well as only seven .. if i feed a link via the url .. oo yes it works
michael
May 26th, 2002 at 8:06 am
How would I use the same file in a pop up screen for the full reads list, that's alphabetized too? I would love to skin that..is there a way?
May 26th, 2002 at 8:44 am
Hey,
I think the problem is I am on a Mac.
I'm trying to put enter instead of a return, but for some reason, my enter key will not work inside my text editor…hmm…I'm going to try typing it on something else, I'll let you know what happens.
Thanks again
,
Liz
May 26th, 2002 at 12:30 am
Heyhey,
I added it, and I made sure my links file (named random.php) had no other tags, it starts with the links, one per line, ect. It works just like an include php tag, I tried both the original suffle tag and the abc one, niether works, I'm not sure why.
Thanks,
Liz
May 26th, 2002 at 7:14 am
Liz – it's hard to guess just based on what you've told me what the problem could be. I know that some people on macs have had trouble implementing this as the "return" key on the mac doesn't product the same code as the "return" key on a PC. (It makes a "soft return" on the mac)
One thing you could try…
Make each line look like this: (requires a bit more work unfortunately)
001 = "<!–Applebox–>-<a href=\"http://www.applebox.org/" target=\"_blank\">Applebox</a><br>";
002 = "<!–Ashley–>-<a href=\"http://www.ashley.nu/" target=\"_blank\">Ashley</a><br>";
Here's how the script is working –
it reads that list into an array (each line as ONE element into the array).
It then shuffles the array.
Then it prints the first 10 entries of the array (which should be random because of the shuffling)
There's nothing in there that should make it show the whole list. So the only thing I can think of is that your list is being read like "one line" (which is what might happen if you created it using a mac.)
If you are on a mac – The other thing you can try is using the "ENTER" (on the keypad) key for your returns.
Let me know how if either of those tricks work.
May 20th, 2002 at 9:52 pm
Oh thankie, Jennifer! I will try that tomorrow…makes excellent sense! Thanks!!
May 21st, 2002 at 9:19 am
Works perfectly, I added it to my blog.
Thank you! Y'all rock!
May 20th, 2002 at 8:07 pm
I'm only guessing, but maybe it is because you have your bloglinks file as a stand alone page, with a doctype and extra tags?
Also, unrelated, I notice you have several doctype declarations within your page. You only need one at the top. Perhaps you have it mistakenly in an include?
May 20th, 2002 at 8:18 pm
Simply Sara -
the bloglinks.php file should have nothing else except JUST the links. No other tags. When I load up your bloglinks.php file, I see all this too:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Blog Links</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<P align="center">
etc. etc.
The bloglinks.php file is not supposed to be a regular webpage – it should ONLY be the links. ie. JUST the lines that look like this:
<!– Meegan –><A HREF="http://www.blogland.com/gem">Meegan</A><BR>
What's happening in the script is it randomly pulls lines from that bloglinks.php page, and in your case sometimes it pulls in this: </head> or this: <P align="center">
make sense?
May 14th, 2002 at 7:04 pm
Lynda, thank you for mentioning about the
AddType application/x-httpd-php .html .php .htm I am thrilled to say it worked like a charm!
Now if I could just grasp the whole concept of .php I'd be so much happier
May 20th, 2002 at 4:12 pm
I am having some problems with this script. I have 40 reads, and it is set to show 10 random. I have checked and double checked about an extra space after the
It's displaying 8, 9, 7 each time. Any clues?
The test blog is at: http://www.simplysara.com/testtwo/index.php
I love the script and to have them alphabetized is wonderful, if it would work!
Thanks for all you do~
May 6th, 2002 at 2:56 pm
I meant to say… can you change the extensions on your SITE to .php (instead of .html)
May 6th, 2002 at 3:33 pm
Done (renamed to testblog/index.php), but there was no change after the rebuild; is there anything else that should happen along with changing the file extension? There were instructions in the Blogomania support forums for creating an .htaccess file – is that where you remember them from? Thanks!
May 6th, 2002 at 3:49 pm
Donna can you send me the code to that page? I'll take a look at it tonight.
May 6th, 2002 at 2:54 pm
Donna – checked out your page – it seems that the page is an HTML page. While it is possible to process PHP code on an HTML page – there's a line you have to add in your .htaccess file (there's a thread about that somewhere on this site… gotta set up a search function here…) :O
If you don't have that line set up, then your page is not processing the PHP code.
Can you change the extensions on your page to .php?
April 29th, 2002 at 10:14 am
Kathi – It shouldn't matter that your list wasn't divisible by 10. You might have had an extra line return that it was interpreting as a blank entry (and displaying randomly on your page) – probably adding another link just put an entry on that blank line.
May 6th, 2002 at 2:35 pm
When I tested this, it seemed to appear as a standard include; the whole list appears (perfectly normally) in the sidebar.
See http://www.deliriouscool.org/testblog and
<div class="side">
<?
$array = file("/home/nammer/public_html/includes/bloglinks.php");
shuffle($array);
for ($i=0; $i<10; $i++) {
echo $array[$i];
}
?>
</div>
Are the <div> tags a problem? Thanks for your thoughts!
April 29th, 2002 at 8:40 am
Thanks, goddesses… this worked great for me! I did have a small problem with it returning 9 links sometimes rather than the 10 I thought I had it set for… but it seems to be related to the fact that I had a number of links in my list that was not evenly divisible by 10. I just added one more link
and it seems to give 10 every time now.
April 26th, 2002 at 9:23 pm
Doh! – I thought the line "Make your list of blog links – one blog link per line" explained that… guess not.
Sorry about that.
April 26th, 2002 at 9:27 pm
or do you mean the <BR> tags at the end of your link… oh well… yeah a sample line in the bloglinks would look something like this:
<!– Scriptygoddess –> <a href="http://www.scriptygoddess.com">Scriptygoddess</a><BR>
April 27th, 2002 at 11:46 am
I actually like my random list to not be alphabetized. There was a method to that madness – I had a short link list & a long one of "more links". My short list was so long though, I never got around to reading anyone on my "more" list. So now the page randomly decides for me who I should go read. If they were in alphabetical order and I was short on time, I would only ever see the ones at the top of the list – this way I see the T's, the Z's, the D's … whatever! It's like making my blog help me out with my own indecisiveness! (All of this, of course, has nothing to do with the script and I might just change it to being alpha someday…)
April 28th, 2002 at 1:13 am
The more I looked at my site today – the more I wanted that list in alphabetical order after all. So I implemented Jennifer's code, tweaking my link list (I had other things to clean up, so I just went through my file here.) I learned one thing. When you are inserting the text in there to make them alphabetize, make sure you either capitalize the first word of all or don't capitalize them all. I couldn't figure out why I had some of the "b" items at the end of the list!
April 15th, 2002 at 9:31 am
Yes… that'll do it…
Here's my test area to prove it:
http://www.scriptygoddess.com/testarea/randombloglist.php
A little bit of work up front, but if you do that (consistently put a comment with the name of the blog as shown in my last comment) it will alphabetize it
The code to print that version out:
<?
$array = file("/path/to/your/list/of/blogs/bloglinks.php");
shuffle($array);
for ($i=0; $i<10; $i++) {
$miniarray[$i] = $array[$i];
}
sort($miniarray);
for ($i=0; $i<count($miniarray); $i++) {
echo $miniarray[$i];
}
?>
(FYI – that list in the test area is actually Christine's list – since this was a request by her… Christine, if you want that file with all the comments already in place, let me know… I'll send it over)
April 15th, 2002 at 12:57 pm
The original script doesn't work for me.
Should the path to the bloglinks.php file be in quotation marks or not?
April 15th, 2002 at 1:25 pm
Brian, it should be in quotation marks. What do you have?
Also, make sure it's the server path, not relative path. In other words, if the URL to the file is http://yourserver.com/bloglinks.php then setting the path to /bloglinks.php is not going to work. You need to enter the path as something like /home/username/public_html/bloglinks.php. If you're unsure of the server path, contact your host.
April 15th, 2002 at 3:12 pm
Lynda — I tried it both with and without quotes, and used the full path in both tests, but still nothing.
I know I can use php scripts on my host's server, because I have used them in the past for comments and such.
April 15th, 2002 at 3:12 pm
If you go to my site and look at the source of my index page, you can see how it's set up right now.
April 15th, 2002 at 3:19 pm
Brian,
I think the problem might be that your page doesn't have a php extension.
Make it .php instead of .html
You could alternatively create an .htaccess file to put into your public directory that contains the following:
AddType application/x-httpd-php .html .php .htm
If you're set on having everything .html
April 15th, 2002 at 9:10 pm
I've seen alphabetical lists of random links on other sites. Maybe MT has that built in?
The only example I can think of is down right now, unfortunately
But I think I'll try what you said above… thanks in advance!
April 17th, 2002 at 1:22 pm
I tried adding that MIME type association to my .htaccess file, but it caused a parsing error of some sort.
It would take too much site cleanup to change my index page to .php right now, so maybe I'll save this for another time.
Thanks for all your help.
April 17th, 2002 at 1:48 pm
The parse error was probably something you mistyped in the script.
Hard to tell without seeing it.
I never recommend setting .html files to read as .php files anyway though, always best to have .php.
April 26th, 2002 at 9:19 pm
You know, this wasn't immediately obvious to me, but you need line break tags in the bloglinks.php file.
OK that was probably incredibly obvious, but I didn't do it at first, did I?
April 15th, 2002 at 8:47 am
I'm thinking that if you add a comment before each link that's consistent, doing the sort would work… (still have to test it though)
(line 1:) <!– A life uncommon –> <a href="etc etc.
(line 2:) <!– Blahblahblog –> <a href="etc. etc.
etc
1) So then you do the shuffle (and you turn yourself around… that's what it's all about)
2) feed the select group into a (smaller) array.
3) Sort that smaller array.
4) THEN PRINT the contents of THAT (smaller) array…
That's just a guess… again, I'll have to try it to see if it works… (I'll probably get a chance to try it tonight)
April 15th, 2002 at 7:04 am
Ohhhh, no, that wouldn't work at all. (just tested it)
For something like that we'd need a more complicated script that separated the title from the actual link and then joined them together again before spitting them out.
Maybe there's an easier way.
April 14th, 2002 at 2:25 am
Thank you! Thank you! Thank you! I'll be putting it to use soon!
April 15th, 2002 at 6:59 am
Would adding perhaps a:
sort($array);
somewhere work? perhaps before the echo? Or would that just mix them all up into order again. What about:
sort($array[$i]); ??
Maybe that would work.
I'd go test it, but I'm about to leave for work. heh.
April 15th, 2002 at 4:40 am
Will that put them in alphabetical order, or not?
April 15th, 2002 at 5:37 am
no alphabetical order. (Although there's probably a way to do that… I"ll get back to you on that)
The "shuffle" just shuffles them randomly.
August 21st, 2004 at 12:31 pm
Is there a way modify the code to break a long list (30 links, personally) into blocks of five or ten links?
July 15th, 2007 at 10:55 pm
[...] it's becoming an annoyance more than anything else. I'm simply going to use this cool Random Links script created by the goddesses a while back. ¡Abajo [...]