Need extra help with your Joomla site? Consider paid Joomla support by the developer of Chameleon and MetaMod.
- Index
- » MetaMod Pro
- » MetaMod Pro General Support
- » [solved] Recipe: 3 random modules on...
[solved] Recipe: 3 random modules on the same page without duplicates.
[solved] Recipe: 3 random modules on the same page without duplicates.
Stephen,
I'm looking for a recipe to have 3 random modules out of 8, display on the same page, but without duplicates.
I'm using this one with 3 instances of MetaMod on the same page, but of course I'm getting duplicates.
Code:
$r = mt_rand(1,8); // get a random number: 1-8 inclusive
if ($r == 1) return 101;
if ($r == 2) return 102;
if ($r == 3) return 103;
if ($r == 4) return 104;
if ($r == 5) return 105;
if ($r == 6) return 106;
if ($r == 7) return 107;
if ($r == 8) return 108;
I think the one below will work for me, but I'm unsure how to set it up.
Code:
session_start();
/* set a different "instance name" for each module that you want to show only once */
$instance_name = "module_counter1";
/* use as many or as few as you like, or "0" for no module */
$modules = array(119,120,121,122,123,124,125);
if (!isset($_SESSION[$instance_name]) ||
$_SESSION[$instance_name] == "") {
$_SESSION[$instance_name] = rand(0,(count($modules)));
}
$index = $_SESSION[$instance_name] % count($modules);
$_SESSION[$instance_name]++;
$module_no = $modules[$index];
if ($module_no > 0) return $module_no;
Thanks for the help,
Bryan
Re: [solved] Recipe: 3 random modules on the same page without duplicates.
Ok, here's an idea. You will set up 3 MetaMods on the page, all with the same code in them. Whichever is the 1st one to run will create the list and display the last one on the list. The next MetaMod will display the next last one, and the last MetaMod will display the next-next-last one.
global $my_module_list = null;
if ($my_module_list == null) {
// create the list and scramble it
$my_module_list = array(45, 46, 47, 48, 49, 50, 51, 53, 1223, 1235); // as many as you like
shuffle($my_module_list);
}
// get the last one on the list, shorten the list by one, and return that module id.
return array_pop($my_module_list);
Clever isn't it?!
Cheers,
Stephen
Re: [solved] Recipe: 3 random modules on the same page without duplicates.
Hi Bryan,
d'oh, my mistake. Error in the 1st line. Just replace the 1st line with this:
global $my_module_list;
(you can't set it to null on the same line as the "global" statement", and it's not necessary to do that).
That should work now.
Best regards,
Stephen
- Index
- » MetaMod Pro
- » MetaMod Pro General Support
- » [solved] Recipe: 3 random modules on...
Board Info
- Board Stats:
- Total Topics:
- 1689
- Total Polls:
- 6
- Total Posts:
- 5943
- Total Posts Today:
- 1
- User Info:
- Total Users:
- 7638
- Newest User:
- moner86658
- Members Online:
- 0
- Guests Online:
- 166
- Online:
- There are no members online
Forum Legend:
Topic
New
Locked
Sticky
Active
New/Active
New/Locked
New Sticky
Locked/Active
Active/Sticky
Sticky/Locked
Sticky/Active/Locked