Boost is great, but as most site have banners on them that change constantly how could i implement some solution that would show different banners? Guess i could use adsense but what if i want to show banners from drupal node? Can i get them somehow with ajax maybe? For example, i would just show randomly one node(banner) from a view. How to do this?

Comments

mikeytown2’s picture

Ajax is the way to do it; here are the directions on how to do it. In short ajax load a block
http://groups.drupal.org/node/24825

Let me know if you hit any snags, I'll help you along the way.

Marko B’s picture

Just what i needed, thanx man. :-)

I would need some help with understanding part where u described it in more complicated way. Ok there are 2 divs, but what do i do with PHP code, and JS code? How does JS code call PHP code? Or how does drupal_add_js() line call code below it? Please explain this to me as i dont see connections between and and confused by this. thanx

mikeytown2’s picture

Get it working the simple way first once you have that down; shifting the code into the more complicated way will be fairly straight forward. If your having trouble with the simple way let me know and I can help you figure it out. First step is to get what you want to display inside a block. Once there see if you can get the simple directions to work in your case.

Marko B’s picture

I did get it working simple way. But i have 3 banners on page so i would like to have one bootstrap not 3. So please some notes on complex way :-) thanx

mikeytown2’s picture

whats the code inside each .php?

Marko B’s picture

i have 3 blocks

$region="some_var";

print views_embed_view('banneri','block_3',$region);
print views_embed_view('banneri','block_2',$region);
print views_embed_view('banneri','block_1',$region);

and i output them like this. So ok, i can put this into your php code, and change # for divs, but how do i call this php to put banners into this divs? where to put JS and how does this JS call PHP?

  include_once './includes/bootstrap.inc';
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

  // Return Data
  $json = array();

  $block = views_embed_view('banneri','block_1',$region);	
  $json = array_merge($json, array('#banner1' => $block));

  $block = views_embed_view('banneri','block_2',$region);	
  $json = array_merge($json, array('#banner2' => $block));

  $block = views_embed_view('banneri','block_3',$region);	
  $json = array_merge($json, array('#banner3' => $block));


  // Send JSON Back
  if (!empty($json)) {
    echo json_encode($json);
  }
  exit;
mikeytown2’s picture

Create 3 Blocks. In first block's content place

<div id="banner1"></div>

Repeat for the next 2. Or place these divs in your template; in short we need some empty div's to target.

In one of the blocks or somewhere else (your custom module, ect...) add in this PHP code. This will call the external php file, which will load the json data into the DOM. This only needs to happen once.

  $filename = 'ajax_blocks.php';
  $js_code = <<<EOT
$.getJSON(Drupal.settings.basePath + "$filename", {nocache: "1"}, function(response) {
  $.each(response, function(id, contents) {
    if (contents == 'NULL') {
      $(id).parent().parent().hide();
    }
    else {
      $(id).html(contents);
    }
  });
});
EOT;

drupal_add_js($js_code, 'inline', 'footer');

Finally we need to create the ajax_blocks.php file. Place this in your webroot or change filename variable above to match its location. This is a cleaner way of doing this in your case.

  include_once './includes/bootstrap.inc';
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

  $region = "some_var";

  // Return Data
  $json = array();
  $json['#banner1'] = views_embed_view('banneri', 'block_1', $region);
  $json['#banner2'] = views_embed_view('banneri', 'block_2', $region);
  $json['#banner3'] = views_embed_view('banneri', 'block_3', $region);

  // Send JSON Back
  drupal_json($json);
  exit;

I'm assuming that the blocks contents are being sent back when you call views_embed_view(). Let me know how this works for you. Doing a selective drupal bootstrap is something to look into if this takes too long to generate the code; another way is to cache the html in the variables table (what you can do with core stats block if enabled with boost); thus only requiring a database bootstrap and no render time (regenerate the blocks content on cron then).

Marko B’s picture

Status: Active » Needs review

I think i get it now, will try it today probably, just what is this ETO stuff? :-)
Selective bootstrap sounds good way to go, will look further into it, if u know some links with custom bootstrap that i could use, please send :-)

thanx a lot Mike!

mikeytown2’s picture

theres not a lot in terms of rolling your own boot strap process; I use this as my guide...
http://api.drupal.org/api/function/_drupal_bootstrap/6

ETO is heredoc syntax; looks like it should be EOT
http://php.net/types.string#language.types.string.syntax.heredoc

Marko B’s picture

thanx

mikeytown2’s picture

does this work for your case?

Marko B’s picture

Still not tested but will soon for sure. Having problem with boost and Fupload module. Stops working when Boost is on. Did disable JS and ajax caching but still problem occur when many images are uploaded. Will try to isolate this problem first then make this adjustment.

One question, was thinking that it would be nice to load banners when page is loaded, like on some onLoad handled if possible (not expert on JS and Ajax just guessing), what do u think about that idea, is it doable and does it have any sense?

mikeytown2’s picture

u try it yet? js is added at the footer so the dom should almost be fully loaded at this time.

Flying Drupalist’s picture

This is great, but there should be an UI option to do this. Would be a killer feature imo.

mikeytown2’s picture

@Flying Drupalist
This should be it's own module; call it ajax blocks. It doesn't depend on Boost and other page caches like Varnish and Core could use it. Just need to do a hook form alter of the block configuration page & add in a checkbox to ajax load this block.

Flying Drupalist’s picture

@mikeytown2 if you can make this module I will kiss your feet. :)

Marko B’s picture

I never managed to try it out. Had many other problems with site i was hopeing to try this that i didnt try it so far :-(
But what u suggested seems good and should work :-) Hope i get to try it and turn whole site into boost before summer :-)

reynaldio’s picture

I've tried #7 and it works like charms. but can we add a loading icon when ajax load the content? maybe a loading text. and is it possible to automatically load .js and .css required by contents?

Thanks,
Reynaldi

mikeytown2’s picture

What ever you place in this div <div id="banner1"></div> will be replaced by the ajax content so a loading icon or text could easily be put in there.

Quick example

<div id="banner1"><img src="/misc/progress.gif" /></div>

Loading the extra js and css is a lot harder to do

reynaldio’s picture

hi, can you please put some example code on how to use selective bootstrap?

Thanks,
Reynaldi

mikeytown2’s picture

Selective bootstrapping is useful for a couple of things...
1. You have what you need in the memcache/apc/database pre-rendered.
2. You want to run a small bit of code from a module & you know what the requirements are.
3. You wish to write data to memcache/apc/database

This is what I use as my guide
http://api.drupal.org/api/function/_drupal_bootstrap/6

Example of 1 + 3 is in boost_stats.php

mikeytown2’s picture

Version: 6.x-1.17 » 6.x-1.x-dev

here's a better way ;)

In one of the blocks or somewhere else (your custom module, ect...) add in this PHP code. This will call the external php file, which will load the json data into the DOM. This only needs to happen once.

  $filename = 'ajax_blocks.php';
  $js_code = <<<EOT
$.getJSON(Drupal.settings.basePath + "$filename", {nocache: "1"}, function(response) {
  $.each(response, function(id, contents) {
    if (contents == 'NULL') {
      $(id).parent().hide();
    }
    else {
      $(id).html(contents);
    }
  });
});
EOT;

drupal_add_js($js_code, 'inline', 'footer');

We need to create the ajax_blocks.php file. Place this in your webroot or change filename variable above to match its location. This is an even better way of doing this in comparison to #7.

  include_once './includes/bootstrap.inc';
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

  // Return Data
  $json = array();
  $json['#block-views-Tweet-block_1 .block-inner .content'] = views_embed_view('Tweet', 'block_1');
  $json['#block-views-recent_posts_domain-block_1 .block-inner .content'] = views_embed_view('recent_posts_domain', 'block_1');
  $json['#block-views-ad_sponsors-block_1 .block-inner .content'] = views_embed_view('ad_sponsors', 'block_1');
  $json['#block-views-ad_sponsors-block_2 .block-inner .content'] = views_embed_view('ad_sponsors', 'block_2');
  $json['#block-views-ad_sponsors-block_3 .block-inner .content'] = views_embed_view('ad_sponsors', 'block_3');
  $json['#block-views-ad_sponsors-block_4 .block-inner .content'] = views_embed_view('ad_sponsors', 'block_4');

  // Send JSON Back
  drupal_json($json);
  exit;

So what I have here are 3 different views with 6 different blocks getting dynamically replaced. This will "re-render" the block updating it to the latest version direct from drupal.

What I'm thinking of doing next is hiding the block until its been updated & then displaying it. Which is similar to how #7 works; just this way it doesn't require a new element to be in the DOM, uses what's already there.

mikeytown2’s picture

Status: Needs review » Fixed
Marko B’s picture

Thanx Mikey, i will probably try it in few months in redesign of site i was working on before as i would like to use boost with it. Thanx for pointing it out.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

srahul07’s picture

Hi Following is my code:

<?php
 
	include_once './includes/bootstrap.inc';
	drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
	
	$block = module_invoke('tuts', 'block', 'view', 0);
	print $block['content'];
?>

I have created a block named: Items in cart which is having following code:

<div id="cart-items"></div>
<script>
$('#cart-items').load('/cart_items.php');
</script>

Also the file cart_items.php executes a custom block as seen above named: tuts_block which is as follows:

<?php
/**
 * Implementation of hook_block
 */
function tuts_block($op = 'list', $delta = 0) {
	$block = array();
	switch ($op) {
		case 'list':
			$block[0]['info'] = t('Get the number of items in cart');
			return $block;
		case 'view':
			$block['content'] = number_of_cart_items();
			return $block;
	}
}

/**
 * Implementation of number_of_cart_items
 */
function number_of_cart_items() {
	
	$cart_items = ''; 
	$number_of_items = uc_cart_get_total_qty();
	if(!empty($number_of_items)) {
		$cart_items = '<a id="cart-items" href="' . base_path() . 'cart">';
		if($number_of_items == 1) {
			$items_text = ' item';
		}
		else {
			$items_text = ' items';
		}
		$cart_items .= $number_of_items . $items_text . '</a>';
	}
	return $cart_items;
}
?>

If I execute the cart_items.php file from browser it does show me the number of items in cart but in block Items it is showing blank. When I use Inspect elements in browser I can see that block is loaded and the above script call is coming as is as follows:

<div id="cart-items"></div>
<script>
$('#cart-items').load('/cart_items.php');
</script>

Please let me know where I am going wrong.

srahul07’s picture

Status: Closed (fixed) » Active

Not sure how it got posted twice and now could not delete second one. It does show me that its cloed and fixed where as opened it again.

Please let me know about the issue expianed above: http://drupal.org/node/657826#comment-5580816

bgm’s picture

Do you have an URL we can see to test? It will be difficult to debug otherwise.

bgm’s picture

Status: Active » Closed (fixed)

(re-Closing issue, open a new one if necessary, with a reference to this one)