Welcome to the MetaMod and Chameleon Support Forums.

Before posting, please check out the FAQs.

helpme

 

Need extra help with your Joomla site? Consider paid Joomla support by the developer of Chameleon and MetaMod.

 

Show a module if count of modules in position 2..

Show a module if count of modules in position 2..

is less than or equal to x.

I have some modules which display based on dates. Help for which I received here some time ago - thank you. If no modules are displayed on a day then the design of the site looks a bit unbalanced.
I would therefore like to display an additional module in that position if the module count is less than a certain value.
I could not find a recipe for this or anything in this forum.
Any advice or hints about another way to tackle in Joomla this would be appreciated.

pd4pbyc
Beginner Modder
ranks
useravatar
Offline
9 Posts
Administrator has disabled public posting

Re: Show a module if count of modules in position 2..

Hi,

this is related to the recipe found here:
http://www.metamodpro.com/metamod/recip … not-appear

However that recipe is based on examining the database for how many modules are assigned to a given page in the module manager and won't take into account any additional modules that MetaMod may have put onto the page.

So when you talk about the number of modules in position 2, does this include only modules that MetaMod includes, or could there be a variable number of additional modules assigned to the page using the module manager as well?

If modules are only put onto those pages using MetaMod, then it should be an easy addition to your MetaMod PHP code to put in an "else" clause. Essentially, if it's a certain time of day then show module X, ELSE show module Y.

If you also need to take into account modules that have been assigned using the module manager, then you'll need to include a database query similar to the one in the recipe page linked above, and use the result of that as part of the logic in the PHP rule.

Either way, I can help you structure some PHP if you can give some more details about the exact setup and requirements.

Cheers,
Stephen

Stephen Brandon
MetaMod / Chameleon developer
If you use MetaMod or Chameleon, please post a rating and a review at the Joomla! Extensions Directory: Chameleon | MetaMod

metamodguy
useravatar
Offline
3314 Posts
User info in posts
Administrator has disabled public posting

Re: Show a module if count of modules in position 2..

Thanks for getting back to me Stephen.

There would be as standard 3 modules in position 2 and additionally between certain dates a module or 2 would be published.

The module is a flyer for a members event (a small lake sailing race) and is published 14 days before the event and removed two days after the event. Sometimes there are two events in a time period which needs two flyers.

All modules in position 2 could be controlled by Metamod or the current standard module date variables which ever is easiest.

The modules are shown only on the front page. When there is no flyer published I would like to show a nice photo to keep the column balance.

Simplest is best because someone without any understanding of what is running does the publishing of the flyers and has from time-to-time messed things up completely for me to then fix. One time he decided to use some cracked software on his computer and managed to put a virus on the website which pleased the public no end!

Anyhow any help is appreciated and I will donate again.

pd4pbyc
Beginner Modder
ranks
useravatar
Offline
9 Posts
Administrator has disabled public posting

Re: Show a module if count of modules in position 2..

Hi pd4pbyc,

in terms of the easiest way to handle this, it's a bit of a toss-up. If you know that there are always going to be 3 modules statically assigned to position 2, then you can just hard code that into your calculation in the MetaMod and avoid the cost of a database lookup. Or, you could just include the static modules into the MetaMod code instead of assigning them with the module manager, and count them there, so if/when you do change those modules the calculation will still be done.

But I think the easiest way to manage all this is to use the standard module manager to assign the static modules and the timed modules, and just use MetaMod to "make up the difference". I'm assuming that you're not using MetaMod in that module position to add any other modules.

So try this:

$date = JFactory::getDate();
$now = $date->toSql();
$nullDate = $db->getNullDate();
$sql = 'SELECT count(*) as c from #__modules m, #__modules_menu mm ' .
  'WHERE mm.moduleid = m.id ' .
  'and m.published = 1 ' .
  'and (mm.menuid = ' .  (int)$Itemid . ' or mm.menuid = 0) ' .
  'and (m.publish_up = ' . $db->q($nullDate) . ' OR m.publish_up <= ' . $db->q($now) . ') ' .
  'and (m.publish_down = ' . $db->q($nullDate) . ' OR m.publish_down >= ' . $db->q($now) . ') ';

$db->setQuery( $sql, 0, 1 );
$result = $db->loadResult() - 1; // remember the MetaMod counts as 1
if ($result < 5) return 123; // if there are less than 5 modules in the position, add module 123 as well.

Stephen Brandon
MetaMod / Chameleon developer
If you use MetaMod or Chameleon, please post a rating and a review at the Joomla! Extensions Directory: Chameleon | MetaMod

metamodguy
useravatar
Offline
3314 Posts
User info in posts
Administrator has disabled public posting

Re: Show a module if count of modules in position 2..

Thanks for that. It is not quite working yet. As the module didn't appear when expected I added "echo $result" and found that the module count was 37 although I can find only 11 modules in position user2 and of those 5 are not published. Changing the statement "if($result <39)" worked perfectly. Obviously I could work around the problem with this, but I don't know how stable the calculation would be!
Is there anything obviously incorrect?
*******
MetaMod Debug-Information:
Module ID: 204
$option: com_content
$view: article
$id: 11
$Itemid: 101
$timezone: UTC
$language: en-us
$language_code: en
$language_region: us
modules 37 total Eingebundene Module: 159

pd4pbyc
Beginner Modder
ranks
useravatar
Offline
9 Posts
Administrator has disabled public posting

Re: Show a module if count of modules in position 2..

Thanks for getting back to me. I just realised that I didn't include the module position into the query, so it counts up all modules that are assigned to "all" pages or the current page in particular, irrespective of position.

So try this, substituting in the correct position name as appropriate:

$date = JFactory::getDate();
$now = $date->toSql();
$nullDate = $db->getNullDate();
$sql = 'SELECT count(*) as c from #__modules m, #__modules_menu mm ' .
  'WHERE mm.moduleid = m.id ' .
  'and m.published = 1 ' .
  "and m.position = 'position 2' " .
  'and (mm.menuid = ' .  (int)$Itemid . ' or mm.menuid = 0) ' .
  'and (m.publish_up = ' . $db->q($nullDate) . ' OR m.publish_up <= ' . $db->q($now) . ') ' .
  'and (m.publish_down = ' . $db->q($nullDate) . ' OR m.publish_down >= ' . $db->q($now) . ') ';

$db->setQuery( $sql, 0, 1 );
$result = $db->loadResult() - 1; // remember the MetaMod counts as 1
if ($result < 5) return 123; // if there are less than 5 modules in the position, add module 123 as well.

Stephen Brandon
MetaMod / Chameleon developer
If you use MetaMod or Chameleon, please post a rating and a review at the Joomla! Extensions Directory: Chameleon | MetaMod

metamodguy
useravatar
Offline
3314 Posts
User info in posts
Administrator has disabled public posting

Re: Show a module if count of modules in position 2..

Thank you so much for your help it works perfectly. I promised to contribute and I have just purchased MetaModPro. I may never need it, but your friendly support is absolute tops!

pd4pbyc
Beginner Modder
ranks
useravatar
Offline
9 Posts
Administrator has disabled public posting

Re: Show a module if count of modules in position 2..

:-) You're welcome

Stephen

Stephen Brandon
MetaMod / Chameleon developer
If you use MetaMod or Chameleon, please post a rating and a review at the Joomla! Extensions Directory: Chameleon | MetaMod

metamodguy
useravatar
Offline
3314 Posts
User info in posts
Administrator has disabled public posting

Re: Show a module if count of modules in position 2..

Haha! So I tried to be a clever-dick after reading a couple of threads, but all I managed to do with the code below was crash the website.!!
Any grammar/syntax help appreciated...

if ($result =< 5) return 288,243: // if there are less than 5 modules in the position, add module 243 as well
elseif ($result ==6) return 288; // if there are 6 modules in the position, add module 288
elseif ($result ==7) return 289; // if there are 7 modules in the position, add module 289

pd4pbyc
Beginner Modder
ranks
useravatar
Offline
9 Posts
Administrator has disabled public posting

Re: Show a module if count of modules in position 2..

Hi,

if you need to return more than 1 module, you need to put the list in double quotes. Then you need to end the statement (before the comment) with a semicolon not a colon. Also use "<=" rather than "=<".

if ($result <= 5) return "288,243"; // if there are less than 5 modules in the position, add module 243 as well

Try that and see how you get on.

Stephen Brandon
MetaMod / Chameleon developer
If you use MetaMod or Chameleon, please post a rating and a review at the Joomla! Extensions Directory: Chameleon | MetaMod

metamodguy
useravatar
Offline
3314 Posts
User info in posts
Administrator has disabled public posting

Board Info

Board Stats:
 
Total Topics:
1685
Total Polls:
6
Total Posts:
5912
Total Posts Today:
4
User Info:
 
Total Users:
11504
Newest User:
toko solo
Members Online:
0
Guests Online:
151

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