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)

57 Responses to “random blog list”

  1. Christine Says:

    Thank you! Thank you! Thank you! I’ll be putting it to use soon!

  2. Row Says:

    Will that put them in alphabetical order, or not?

  3. Jennifer Says:

    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.

  4. Lynda Says:

    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. :)

  5. Lynda Says:

    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.

  6. Jennifer Says:

    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)

  7. Jennifer Says:

    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)

  8. Brian Says:

    The original script doesn’t work for me.

    Should the path to the bloglinks.php file be in quotation marks or not?

  9. Lynda Says:

    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.

  10. Brian Says:

    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.

  11. Brian Says:

    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.

  12. Lynda Says:

    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

  13. Row Says:

    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!

  14. Brian Says:

    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.

  15. Lynda Says:

    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.
    :)

  16. Row Says:

    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? :D

  17. Jennifer Says:

    Doh! - I thought the line “Make your list of blog links - one blog link per line” explained that… guess not. :( Sorry about that.

  18. Jennifer Says:

    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>

  19. Christine Says:

    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…)

  20. Christine Says:

    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!

  21. kathi Says:

    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.

  22. Jennifer Says:

    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.

  23. Donna Says:

    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!

  24. Jennifer Says:

    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?

  25. Jennifer Says:

    I meant to say… can you change the extensions on your SITE to .php (instead of .html)

  26. Donna Says:

    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!

  27. Jennifer Says:

    Donna can you send me the code to that page? I’ll take a look at it tonight.

  28. Gina Says:

    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

  29. Simply Sara Says:

    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~

  30. Row Says:

    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?

  31. Jennifer Says:

    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?

  32. Simply Sara Says:

    Oh thankie, Jennifer! I will try that tomorrow…makes excellent sense! Thanks!! :)

  33. Simply Sara Says:

    Works perfectly, I added it to my blog. :)
    Thank you! Y’all rock!

  34. Liz Says:

    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

  35. Jennifer Says:

    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.

  36. Simply Sara Says:

    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?

  37. Liz Says:

    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

  38. Jennifer Says:

    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.

  39. Liz Says:

    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

  40. Simply Sara Says:

    Jennifer thank you! :)

  41. Catherine Says:

    Thanks so much for the code!!

  42. - Says:

    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… =)

  43. ian Says:

    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… =)

  44. pixelkitty Says:

    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!

  45. pupet Says:

    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

  46. heather Says:

    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 ;-)

  47. Maggie Says:

    Absolutely love this script Jennifer!! Thank you so much!!!

  48. jane Says:

    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?

  49. Donna Says:

    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!

  50. Katia Says:

    Thankyou for this marvellous script! I have been looking for this everywhere (I should have known to come here first). :)

  51. Jake Says:

    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?

  52. marc Says:

    Thanks! Very cool PHP random list HTML tag generator.

  53. norm Says:

    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?

  54. Alberto Says:

    You’d use natecasesort() rahter than sort.
    ciao
    Alberto
    http://www.unitedscripters.com/

  55. michael Says:

    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

  56. Marie Says:

    Is there a way modify the code to break a long list (30 links, personally) into blocks of five or ten links?

  57. The Weblog @ Betizuka.com » Sucky Blogroll Says:

    [...] 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 [...]