• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

Display text element error

mattsh

Member
Hi!

I need to show message and use a Display text element (Eval activated) if the user has win 7 as OS.
Have tested this this borrowed code, but the message isn't shown (no errors):
PHP:
$user_agent     =   $_SERVER['HTTP_USER_AGENT'];

function getOS() {

    global $user_agent;

    $os_array       =   array(
             '/windows nt 6.1/i'     =>  'My message....'
                        );

    foreach ($os_array as $regex => $value) {

        if (preg_match($regex, $user_agent)) {
            $os_platform    =   $value;
        }
    }
    return $os_platform;
}
$user_os        =   getOS();

print_r ($device_details);

What do I miss?

The code it self is working fine when I test it in a separate php-page.

Regards
Matt
 
First issue is that you are doing "print_r($device_details)" when $device_details has not been set. Presumably you should use $user_os instead.

I would also suggest that you move the $user_agent inside the getOS function and not use global - this is bad practice when you don't need it.

Then, you would probably be better getting rid of the function and making the code inline - if for any reason this code is executed twice, it might try to define getOS twice and create an unwanted warning message.

Finally, the Wiki documentation for the Display Text element (http://fabrikar.com/forums/index.php?wiki/display-text-element/) doesn't say it, but I suspect that if you check the Eval option (which you have presumably done in order to execute the php) then you need to use "return" instead of "print_r".

If I was writing this, my code (which I have not tested) would therefore be:
PHP:
$os_array = array(
    '/windows nt 6.1/i'   =>'My message....'
);

$user_agent = $_SERVER['HTTP_USER_AGENT'];

foreach ($os_array as $regex => $value) {
    if (preg_match($regex, $user_agent)) {
        return $value;
    }
}

Hope this helps.

P.S. If it works, perhaps you could amend the wiki entry to say that the eval needs to return the text you want to display.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top