Snippet: WbRandomSection

mad_nico

Hi,

I use this snippet for different projects. When I use this snippet together with other modules, I get an PHP error:

Type   E_NOTICE
Message   Undefined variable: types
Line:File   48 : .../modules/wbrandomsection/include.php

I know, this is not a real error, just an notice, but even if I set the PHP messages to E_ALL^E_NOTICE, this mesage still apperars at the web-frontend. Has anybody an idea, how to stop this messages?

cheers, Nico

WebBird

Updated version 0.3 uploaded to AMASP.

mr-fan

for WB 2.8 there was a missing index.php file in the zip...

attached file with index.php

regards martin (thanks to doc - he figured this out...)



[gelöscht durch Administrator]

WebBird

With the WbRandomSection snippet you can, but you can't give a list of sections to choose from. Maybe in the next version. :-D

crnogorac081

cool.

It is important that it can use any section :)

cheers
Web developer

WebBird

#10
The original RandomSection module only handles WYSIWYG.

WbRandomSection handles any section/module type.

I can add the option to give a list of section id's to choose from, but I think you could do it as a droplet.

Try this code (untested) in a code section:


global $database;
$sections = array();

// get random section id
$rand = rand( 0, sizeof($sections) );
$section_id = $sections[$rand];

// get module type from DB
$result = $database->query( "SELECT module FROM ".TABLE_PREFIX."sections WHERE section_id='$section_id'" );

if ( $result ) {

    $data   = $result->fetchRow();
    $module = $data['module'];
   
    $file = WB_PATH.'/modules/'.$module.'/view.php';
    if ( file_exists ($file) ) {
      ob_start(); // Output Buffer start
      require($file);
      $foo = ob_get_contents(); // put Output Buffer in $foo
      ob_end_clean();           // end and clean the Output Buffer
     
      $output .= '<div class="randomsection">'
            .  $foo
            .  '</div>';
           
      return $output;

}

return false;


In $sections = array();, insert the comma seperated section IDs between the (...).

Stefek

Quote from: crnogorac081 on August 04, 2009, 04:20:47 PM
I was thinking maybe to have in array section id like (5,10,20,21) and then to chose random between those 4..

Does it metter which module is it, or any module can work ?

I think this is the module you're looking for.
But notice, that it handles WYSIWYG only.

Regards,
Stefek
[i]"Gemeinsam schafft man mehr."[/i]

[b][url=http://duden.de/rechtschreibung/gemeinsam#Bedeutung1]gemeinsam[/url][/b]
1. mehreren Personen oder Dingen in gleicher Weise gehörend, eigen
2. in Gemeinschaft [unternommen, zu bewältigen]; zusammen, miteinander
#Duden

crnogorac081

I was thinking maybe to have in array section id like (5,10,20,21) and then to chose random between those 4..

Does it metter which module is it, or any module can work ?
Web developer

WebBird

Hum? Do you want to give a list of sections to choose from? If so, you don't need the snippet. :-D

By default, a random section will be chosen from ALL active sections on ALL pages.

crnogorac081

I ment if sections are on different pages..

But if there is already exist cool :)

cheers
Web developer

Stefek

Hello Ivan,


All you need to do is: name your pageID.
The snippet returns the sections of this pageID in random.

So you do not have to call the sections in a array.
But f you need it that way, there is another module for this at AMASP.

Regards,
Stefek
[i]"Gemeinsam schafft man mehr."[/i]

[b][url=http://duden.de/rechtschreibung/gemeinsam#Bedeutung1]gemeinsam[/url][/b]
1. mehreren Personen oder Dingen in gleicher Weise gehörend, eigen
2. in Gemeinschaft [unternommen, zu bewältigen]; zusammen, miteinander
#Duden

crnogorac081

can you put sections id in array ?
Web developer


Stefek

Thanks again WebBird.

Thats a very handy Snippet.

Regards,
Stefek
[i]"Gemeinsam schafft man mehr."[/i]

[b][url=http://duden.de/rechtschreibung/gemeinsam#Bedeutung1]gemeinsam[/url][/b]
1. mehreren Personen oder Dingen in gleicher Weise gehörend, eigen
2. in Gemeinschaft [unternommen, zu bewältigen]; zusammen, miteinander
#Duden

WebBird

#1
Due to a request in the German board, I created a RandomSection Snippet. I am aware of the existence of "RandomSection", but I think it is too restrictive, for it limits the sections to choose from to WYSIWYG.

So, I created a Snippet called "WbRandomSection" (where Wb is for Webbird :-D), which gives you more freedom.

For download, see the AMASP page: http://www.websitebakers.com/pages/modules/listings/section-pages/wbrandomsection.php?lang=EN

Usage instruction can be found in the readme.txt:

Quote
USAGE
=====

1. In a Code section

* Add a code section to the page
* Insert the following code (the two lines between --- and ---):

---
include_once WB_PATH.'/modules/wbrandomsection/include.php';
show_random( array( <Option> ) );
---

2. In a template

* Open the index.php of the template
* Insert the code mentioned above where the random section(s) shall occur


3. As a droplet

* Name: WBRandomSection
* Code:

---
include_once WB_PATH.'/modules/wbrandomsection/include.php';

$options = array();

if ( isset( $page_id ) ) {
   $options['page_id'] = $page_id;
}

if ( isset( $types ) ) {
   $options['types'] = explode( ',', $types );
}

if ( isset( $limit ) ) {
   $options['limit'] = $limit;
}

ob_start();
show_random( $options );
$return = ob_get_clean();

return $return;

---

* Comment:
 [[WBRandomSection?page_id=<ID>;types=<Type(s)>;limit=<Int>]]


Examples:

[[WBRandomSection?page_id=5]]
[[WBRandomSection?types=wysiwyg,imagegallery,code]]
[[WBRandomSection?page_id=5;types=wysiwyg,imagegallery,code]]


OPTIONS
=======

There are several options to control the snippet's behaviour:

page_id => <Number>

   A page number to choose the sections from. By default, the section
   is chosen from all sections of all pages.

types => '<Moduletype>' | array( 'List', 'of', 'Types' )

   Module types to choose from. May be a string or a list. By default,
   the section is chosen from all available module types.
   
limit => <Number>

   Number of sections to show. Default is 1.
 


EXAMPLES
========

* All default; choose a (=one) random section from all available pages

show_random();

* Show 2 random sections from page with ID 5, but only of type
"wysiwyg" or "imagegallery".

show_random(
   array(
       'page_id' => 5,
       'limit'   => 2,
       'types'   => array( 'wysiwyg', 'imagegallery' )
   )
);


STYLES
======

To style your Random Sections, two CSS classes are provided:

div.randomsection

   Each section is wrapped into a <div> of class "randomsection". To
   add a gray border to each section, for example, add this code to
   your frontend.css (of the template you are using):
   
   div.randomsection { border: 1px solid #ccc; }
   
   To add a margin on the bottom of each section, try this:
   
   div.randomsection { margin-bottom: 15px; }

div.randomsectionwrap

   Wraps the complete RandomSection part. That is, if you include more
   than one section, the complete list of rendered sections is
   wrapped into a <div> of class "randomsectionwrap".
   
   You can use this to "float" the complete RandomSection block:
   
   div.randomsectionwrap { width: 100px; float: right; }