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.

 

Stuck on Custom Metamod PHP

Stuck on Custom Metamod PHP

I'm not so experienced with php, and I would appreciate any advice any of you would give me. I have a homepage video, and I'm trying to create a situation where different modules show depending on the circumstance. First of all, if the device is mobile, show a certain module. If the device is not mobile, show either module A or B depending on whether the person has visited the site before via cookie. The code I have below is not working, probably for obvious reasons that are not obvious to me. Thanks for reading.

$c = JRequest::getInt('SeenIt', 0, 'cookie'); /*Check for cookie */
if (($core_genius->check("categorizr != mobile")) && ($c)) return 125; /* If cookie exists and the device is not mobile, show module 125 */
if (($core_genius->check("categorizr = mobile")) && ($c)) return 131; /* If cookie exists and the device is mobile, show module 131 */
setcookie( 'SeenIt', 1, time()+60*60*24*365*10, '/' ); /* Otherwise, set the cookie */
return 129; /*If cookie does not exist, show module 129*/

I'm running Joomla 3.6.2 and Metamod 3.27
The code I am using is partly based on the following recipe:
http://www.metamodpro.com/metamod/recip … odule-once

mchmaster
Guest
useravatar
Offline
Administrator has disabled public posting

Re: Stuck on Custom Metamod PHP

Hi mchmaster,

at first glance your code looks ok to me, so I wonder if the rest of your setup is ok.

But first could you just confirm exactly what does happen when you use the code above in MetaMod:
1 - no module appears at all
2 - modules 125, 131 and 129 appear but the layout is weird?
3 - a PHP error occurs
4 - you get a blank screen
5 - you're getting 2x copies of some of the modules 125, 131 and 129.

Basic setup:
Check to see you have the following in place:
1 - MetaMod is published, is assigned to some or all pages, and assigned to a valid module position. You can test this out by switching on MetaMod's "debug mode" and see that you get some debug output on every page where you want the MetaMod to be making decisions.
2 - modules 125, 131 and 129 exist and are published OR are unpublished but set to a non-existent module position. This is so that they do not show up on the page until/unless the MetaMod includes them.

Hope that helps,
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
3329 Posts
User info in posts
Administrator has disabled public posting

Re: Stuck on Custom Metamod PHP

Thanks for the detailed reply and suggestions.

I'm glad that the code looks good. To answer your questions:

When I open the page, module 125 appears every time. It looks fine, but the other modules don't appear when I want them to.

I checked that Metamod is published, set to display on the homepage where I want it, and is assigned to a valid position.

On turning on Debug mode, I get the following:

MetaMod debug info:
Module ID: 130
$option: com_content
$view: category
$id: 10
$Itemid: 101
$timezone: America/Denver
$language: en-us
$language_code: en
$language_region: us
Including modules: 125

This seems to indicate the module is loading correctly. All three modules are unpublished but set to existing module positions

mchmaster
Guest
useravatar
Offline
Administrator has disabled public posting

Re: Stuck on Custom Metamod PHP

I don't know if I did something different or if it was always this way, but on closer inspection, the mobile module is actually loading when it is supposed to. It is just the cookied module that is not working.

mchmaster
Guest
useravatar
Offline
Administrator has disabled public posting

Re: Stuck on Custom Metamod PHP

Hi,

so are you saying that 125 appears all the times when it is supposed to (anything other than mobile device, and NOT on the 1st page load but on all subsequent page loads)?

... but module 131 is NOT appearing at all, even when it's a mobile device and it's on a subsequent page load?

In that case it may be that categorizr is mis-categorising mobile devices. Could I ask you do try the following as the 1st line of the PHP code:

echo "User agent is: " . htmlentities($_SERVER['HTTP_USER_AGENT']) . "<br>";

and reply to this thread with the results? I'll test to see how/why categorizr is not picking it up correctly.

Thanks,
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
3329 Posts
User info in posts
Administrator has disabled public posting

Re: Stuck on Custom Metamod PHP

Not quite.

What is happening is that module 131 is appearing exactly when it should: when a mobile device is viewing the page.
When not in mobile, module 125 always shows. My expectation is that module 125 only shows the first time a page is loaded, and then all subsequent loads show module 129, but that doesn't work for some reason.

mchmaster84
Beginner Modder
ranks
useravatar
Offline
5 Posts
Administrator has disabled public posting

Re: Stuck on Custom Metamod PHP

Hi mchmaster84,

I think it's a logic thing.
At present, the code does this:

- if 1st page load (irrespective of mobile/desktop) display 129.
- if NOT mobile, and 2nd+ page load, display 125
- if mobile and 2nd+ page load, display 131

You're saying that you want 125 to show for mobile the first time, and 129 on 2nd+ time. So you could just swap those two module numbers in the code.
However, this will also mean that non-mobile will see 125 the first time. Is that what you want?

Or perhaps we can do it this way so you can control exactly who sees what:

// edit the module numbers as appropriate
$mobile_firsttime = 125;
$mobile_subsequent = 129;
$nonmobile_firsttime = 125; // or maybe a different module one?
$nonmobile_subsequent = 131;

$c = JRequest::getInt('SeenIt', 0, 'cookie'); /*Check for cookie */
if (($core_genius->check("categorizr != mobile")) && ($c)) return $nonmobile_subsequent; /* If cookie exists and the device is not mobile */
if (($core_genius->check("categorizr = mobile")) && ($c)) return $mobile_subsequent; /* If cookie exists and the device is mobile */
setcookie( 'SeenIt', 1, time()+60*60*24*365*10, '/' ); /* Otherwise, set the cookie */
// if we get here, then it's the 1st page load.
if ($core_genius->check("categorizr = mobile")) return $mobile_firsttime;
if ($core_genius->check("categorizr != mobile")) return $nonmobile_firsttime;

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
3329 Posts
User info in posts
Administrator has disabled public posting

Re: Stuck on Custom Metamod PHP

Thank you so very much. I changed the module id's to the way I wanted it to be, and it basically worked perfectly. As you said, my php logic was off.  Here is my final code for anyone else to might be hepful to:

$mobile_firsttime = 131;
$mobile_subsequent = 131;
$nonmobile_firsttime = 125;
$nonmobile_subsequent = 129;

$c = JRequest::getInt('SeenIt', 0, 'cookie'); /*Check for cookie */
if (($core_genius->check("categorizr != mobile")) && ($c)) return $nonmobile_subsequent; /* If cookie exists and the device is not mobile */
if (($core_genius->check("categorizr = mobile")) && ($c)) return $mobile_subsequent; /* If cookie exists and the device is mobile */
setcookie( 'SeenIt', 1, time()+60*60*24*365*10, '/' ); /* Otherwise, set the cookie */
// if we get here, then it's the 1st page load.
if ($core_genius->check("categorizr = mobile")) return $mobile_firsttime;
if ($core_genius->check("categorizr != mobile")) return $nonmobile_firsttime;

mchmaster84
Beginner Modder
ranks
useravatar
Offline
5 Posts
Administrator has disabled public posting

Re: Stuck on Custom Metamod PHP

Hey that's great - glad it's working for you.

Best regards,
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
3329 Posts
User info in posts
Administrator has disabled public posting

Board Info

Board Stats:
 
Total Topics:
1683
Total Polls:
6
Total Posts:
5940
Posts this week:
11
User Info:
 
Total Users:
5142
Newest User:
zaki98040
Members Online:
0
Guests Online:
189

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