Recent Topics

1 Apr 12, 2007 00:53    

My b2evolution Version: Not Entered

I've read a few old posts in the forums about being able to display a random post, but I could not find any info about a completed hack.

I have a blog of quotes, and I want to create a random "Quote of the Moment" at the very top of the Blog that's generated several times a day, not necessarily for every visitor.

Has anyone used or even heard of something like this?

www.moralquotes.com

2 Apr 14, 2007 07:32

Blogger4832 wrote:

My b2evolution Version: Not Entered

I've read a few old posts in the forums about being able to display a random post, but I could not find any info about a completed hack.

I have a blog of quotes, and I want to create a random "Quote of the Moment" at the very top of the Blog that's generated several times a day, not necessarily for every visitor.

Has anyone used or even heard of something like this?

www.moralquotes.com

You can do it in either javascript on the client side, or in PHP on the server side.

Here's a javascript snippet of two quotes:

<script type="text/javascript">
var commentNum=0;

commentArray=new Array();
//Testimonial gallery
commentArray[commentNum++]="What a great difference! <br />.";
commentArray[commentNum++]="We can't believe how much improved our business has been since you re-did our website. <br />";

var totalComments = commentArray.length;

function randNum(range) {return Math.floor(Math.random() * range);}
</script><!-- .....Then when it's time to display it you can execute code something like: -->

<script type="text/javascript">
index=randNum(totalComments);document.write(commentArray[index]);
</script>

3 Apr 14, 2007 07:50

It's very easy at the server to do it for each visitor. Each page load really because a visitor might load the same page several times. Anyway if you're okay with that I'll show you what I do. Otherwise I've no idea how to do it based on time of day.

5 Apr 15, 2007 06:29

It's very easy at the server to do it for each visitor. Each page load really because a visitor might load the same page several times. Anyway if you're okay with that I'll show you what I do. Otherwise I've no idea how to do it based on time of day.

It doesn't have to be time of day, the random quote could change anytime. Or, as you said, a different one for each visitor will be fine also.

I'd be interested in knowing how to set it up.

Thanks all for your replies.

6 Apr 15, 2007 09:24

Here's what I use to display a random poem on my blog :-

	$sql = 'select post_ID from T_posts where post_main_cat_ID = 50';

	$poems = $DB->get_results( $sql, ARRAY_A );

	$temp = $poems[ rand( 0,( count( $poems ) - 2 ) ) ];

	$poem = new ItemList( '31', array(), $temp['post_ID'] );

	$Poem = $ItemCache->get_by_ID( $temp[ 'post_ID' ] );

All my poems are in category 50 ( you'd need to change that number to your quotes category ), and $Poem now contains the random poem and is exactly the same as a normal $Item ( ie/ you can do $Poem->title(), $Poem->content() etc )

¥

7 May 15, 2007 01:07

Thanks for the tip.

This code worked for getting a quote to display, but I keep getting the same quote, even after leaving the site and erasing all cookies, etc. The only thing I changed was the category number.

Also, can't seem to get the categories for that quote to display.

First I put this line: <?php $Poem->title(); ?> which displays a quote.

Then this one: <?php $Poem->categories(); ?>

I get the following errors with the 2nd line:

Notice: Undefined index: 947 in /home/moralquo/public_html/inc/MODEL/items/_item.class.php on line 736

Warning: Invalid argument supplied for foreach() in /home/moralquo/public_html/inc/MODEL/items/_item.class.php on line 739

Thanks in advance for your help.

8 May 15, 2007 01:39

hmm... I should start compiling these cool mods and post it here.

Unless we have one already :p
found a couple these past few weeks... and the separate topics tend to be forgotten :p

Like this one.. I never thought about this... random stuff are awesome.

9 May 15, 2007 10:14

Try changing the 31 in this line to either 1 or the blog id that your quotes are in :-

$poem = new ItemList( '31', array(), $temp['post_ID'] );

The item categories get built by some arcane magic that I haven't quite worked out yet, so I just don't use them on my quotes :p

¥

10 May 15, 2007 20:37

I changed the '31' to '1' and nothing changed.

Then I changed the previous line:

$temp = $poems[ rand( 0,( count( $poems ) - 2 ) ) ];

I changed the '2' to a '1'.

After that I got a new quote to appear, but just like the previous time, it didn't change when the page was reloaded.

I then tried to change the numbers again, and oddly the quote that was displayed the first time I made this mod came back! And no matter what I change the numbers to now, a new quote doesn't appear.

A new quote only appears if I change the category ID.

11 May 15, 2007 20:42

and all of your quotes are in the same category?

if not then you'll need to change the code a tad :-

$foo = array( 1,2,3,4);// comma seperated list of the categories
 $sql = 'select post_ID from T_posts where post_main_cat_ID in( '.implode(',', $foo).')';

¥

12 May 16, 2007 00:03

I have a main category for authors (14) and then subcategories for different authors.

I had changed the category to 14.

I will try the new code tonight and see if it makes a difference. Thanks so much for all of your suggestions!

13 May 17, 2007 18:28

This seems to have worked!

Thanks so much for your help. :D


Form is loading...