Recent Topics

1 Jun 05, 2011 13:31    

My b2evolution Version: 3.3.2

I have a widget set up on my blog so that it shows a random post. However, I'd like to be able to renew the random entry without having to reload the entire page every time.

The widget is set up in an array of dom tabs (using javascript along with it), and so I'd like it if every time the visitor clicks on the 'random post' tab a new post appears. From a quick google, I can see a number of tutorials for reloading a single div - instead of the entire page - however these require that the contents of that div are placed in a separate file from the rest of the page's html/php. Obviously that doesn't quite work when the contents are drawn from the database.

My question is: can I put a sidebar file into my template and call the widget container from there, so that the javascript will reload the random post without reloading the whole page; and does anyone have a suggestion of how to do the reload in another way?

You can see the template under construction here:
http://www.schoolofpuppetry.com.au/generator.php

Thanks in advance!

2 Jun 05, 2011 16:47

And the following code at the very top of your /skin/index.main.php, right after this string

if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );

function get_random_posts( $limit = 10 )
{
	global $Blog;
	
	$RandomList = new ItemList2( $Blog, NULL, 'now', $limit );
	$RandomList->set_filters( array(
			'order'			=>	'RAND',
			'unit'			=>	'posts',
			'types'			=>	'1',
		), false ); // Don't memorize settings
	
	// Run the query:
	$RandomList->query();
	
	if( $RandomList->result_num_rows != 0 )
	{
		while( $rItem = & $RandomList->get_item() )
		{
			$r[] = '<li><a href="'.$rItem->get_permanent_url().'">'.strmaxlen($rItem->title, 30).'</a></li>';
		}
		
		if( !empty($r) )
		{
			return '<ul>'.implode( "\n", $r ).'</ul>';
		}
	}
	return false;
}

if( $rand_limit = param('getrandomposts', 'integer') )
{
    if( $rand_limit < 20 && $rand_posts = get_random_posts($rand_limit) )
	{
		$GLOBALS['Hit']->logged = true; // don't log this hit
		
		echo $rand_posts;
		exit;
	}
}

You can get a list of random posts with AJAX request on this address

http://yourblog/index.php?getrandomposts=15&redir=no

Of course it would be a lot better to create a plugin for this because otherwise you spend a lot more resources on pulling out unnecessary posts from the database. This is the easiest method to get what you want.

4 Jun 06, 2011 19:07

Try to move this code from index.main.php to posts.main.php

The code above generates a list like this

<ul>
<li>Post title 1</li>
<li>Post title 2</li>
</ul>

Once you get it working, on yourblog/index.php?getrandomposts=15&redir=no, create a Free HTML widget with javascript displaying these results and refreshing every few seconds.

<script type="text/javascript">
jQuery.ajax({
  url: "yourblog/index.php?getrandomposts=15&redir=no",
  cache: false,
  success: function(html){
    jQuery("#posts_inserted_here").append(html);
  }
});
</script>
<div id="posts_inserted_here"></div>

5 Jun 07, 2011 09:07

Ok, sorry I should have been clearer: I don't want it refreshing every X seconds. I want the random post to refresh every time someone clicks the 'random post' link.

Anyway, moving the code to posts.main.php gives me an error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'RAND, post_ID RAND LIMIT 15' at line 2(Errno=1064)

Your query: ItemList2::Query() Step 1: Get ID list

SELECT DISTINCT post_ID

FROM evo_items__item INNER JOIN evo_postcats ON post_ID = postcat_post_ID
INNER JOIN evo_categories ON postcat_cat_ID = cat_ID

WHERE ( cat_blog_ID = 15 )

AND (post_ptyp_ID IN (1))

AND (post_datestart <= '2011-06-07 17:23:40')

AND ( ( ( post_status = 'private'

AND post_creator_user_ID = 1 )

OR post_status IN ('published','protected') ) )

ORDER BY post_order RAND, post_ID R

AND

LIMIT 15

6 Jun 07, 2011 19:29

I guess I should have tested the code before posting...

7 Jun 08, 2011 04:24

:) That's ok - it didn't cause an error when I put it in index.main.php, so I didn't pick it up til now.

8 Jun 08, 2011 04:48

replace

'order'            =>    'RAND', 


with

'orderby'            =>    'RAND',

You don't have to reload every few seconds, it's totally up to you how to display the list. The main thing is how to get these posts in the first place.

9 Jun 09, 2011 03:46

Thanks, that solved the problem. One last question: how do I call a particular blog? I want to make sure the random posts come from one blog only, not whatever blog the code happens to be installed on.

10 Jun 09, 2011 04:24

Replace

global $Blog;
    
$RandomList = new ItemList2( $Blog, NULL, 'now', $limit );


with

$blg = param( 'blg', 'integer', true );

$BlogCache = & get_BlogCache();
$Blg = & $BlogCache->get_by_ID( $blg );

$RandomList = new ItemList2( $Blg, NULL, 'now', $limit );

not tested!

index.php?getrandomposts=15&blg=22&redir=no

11 Jun 09, 2011 07:40

I get the following error:

Fatal error: Call to undefined function get_BlogCache() in public_html/skins/2011home/posts.main.php on line 22


Form is loading...