Page 1 of 1

if neither are met

Posted: Fri Apr 09, 2010 2:47 pm
by phplearner123
first off the dreaded noob strikes again, I am a designer moving his way into php.
I am currently working with the modx cms platform and I am creating an area for clients. when I have client A logged in it shows a custom chunk of html for them, when I have client B logged in it shows a custom chunk for them, that I have resolved, the problem I am finding is if the client is NEITHER A or B, how do I show another chunk of html?

Yes, I have gone to modx forums, but it's time I get my proverbial cr*p together and really get educated on how to properly use and implement PHP by asking the big boys. (hope you guys won't be too brutal like the Linux forums back in the day) :lol:

Code: Select all

<?php
$output ='';

$doc_id = $modx->documentIdentifier;

$doc_array_1 = array('54');
$doc_array_2 = array('107');

$username = isset($_SESSION['webShortname']) ? $_SESSION['webShortname'] : '';

if($username =='dave'){
    if(in_array($doc_id, $doc_array_1)){
        $output .='{{dave}}';
    }elseif(in_array($doc_id, $doc_array_2)){
        $output .='{{davespanish}}';
    }else{
        $output .='{{dave}}';

if($username =='bill'){
    if(in_array($doc_id, $doc_array_1)){
        $output .='{{pablo}}';
    }elseif(in_array($doc_id, $doc_array_2)){
        $output .='{{pablospanish}}';
    }else{
        $output .='{{pablo}}';

?>
the {{chunks}} shown above are custom html for those two clients in two languages, but let's say a new client logs on, i would like them to see a chunk for a new user in both languages.
any help would be enormously appreciated.

Re: if neither are met

Posted: Fri Apr 09, 2010 2:54 pm
by AbraCadaver
This looks like it will become unwieldy fairly quickly unless those are the only two users that you'll ever do this for. But to answer your question, based on your current code you could add this at the end:

Code: Select all

if(empty($output)) {
    //do whatever
}
Or you could convert this into a switch statement:

Code: Select all

switch($username){

    case 'dave':
       if(in_array($doc_id, $doc_array_1)){
           $output .='{{dave}}';
       }elseif(in_array($doc_id, $doc_array_2)){
           $output .='{{davespanish}}';
       }else{
           $output .='{{dave}}';
       }
       break;

    case 'bill':
       if(in_array($doc_id, $doc_array_1)){
           $output .='{{pablo}}';
       }elseif(in_array($doc_id, $doc_array_2)){
           $output .='{{pablospanish}}';
       }else{
           $output .='{{pablo}}';
       }
       break;

    default:
       //do something
       break;
}

Re: if neither are met

Posted: Fri Apr 09, 2010 3:00 pm
by AbraCadaver
Added a switch.

Re: if neither are met

Posted: Fri Apr 09, 2010 3:17 pm
by phplearner123
thanks for the super fast reply AbraCadaver !!

the first code didn't work and inserted this into the default area of your switch suggestion

Code: Select all

     if(in_array($doc_id, $doc_array_1)){
        $output .='{{newclient}}';
    }elseif(in_array($doc_id, $doc_array_2)){
        $output .='{{newclientspanish}}';
    }else{
        $output .='{{newclient}}';
but I couldn't get an output.

I know about it become a hassle as more client join, but I am nowhere near the level of hacking my login php to create a chunk of html and inserted in the appropriate place yet, I am a few years away to say the least, but maybe as my company get's bigger I can go back to designing, photography and messing with autodesk only and hire a PHP and JavaScript guru. Right now the way I have the custom html chunks laid out, its a fifteen minute operation of personalizing links to my site's sandbox, so I am hoping I won't be pulling my hair out with it (fingers crossed)

Re: if neither are met

Posted: Fri Apr 09, 2010 3:24 pm
by AbraCadaver
Show all the new code with the switch.

Re: if neither are met

Posted: Fri Apr 09, 2010 3:28 pm
by phplearner123
here it is:

Code: Select all

<?php
$output ='';

$doc_id = $modx->documentIdentifier;

$doc_array_1 = array('54');
$doc_array_2 = array('107');

$username = isset($_SESSION['webShortname']) ? $_SESSION['webShortname'] : '';

switch($username){
    case 'dave':
    if(in_array($doc_id, $doc_array_1)){
        $output .='{{dave}}';
    }elseif(in_array($doc_id, $doc_array_2)){
        $output .='{{davespanish}}';
    }else{
        $output .='{{dave}}';
    break;

    case 'bill':
    if(in_array($doc_id, $doc_array_1)){
        $output .='{{bill}}';
    }elseif(in_array($doc_id, $doc_array_2)){
        $output .='{{billspanish}}';
    }else{
        $output .='{{bill}}';
    break;

    default:
        if(in_array($doc_id, $doc_array_1)){
        $output .='{{newclient}}';
    }elseif(in_array($doc_id, $doc_array_2)){
        $output .='{{newclientspanish}}';
    }else{
        $output .='{{newclient}}';
    break;
}
?>

Re: if neither are met

Posted: Fri Apr 09, 2010 3:44 pm
by AbraCadaver
You're missing the closing } after your three else statements.

Re: if neither are met

Posted: Fri Apr 09, 2010 4:08 pm
by phplearner123
that did the trick! told you I was a one of "those" noobs, but a closing a statement? that's pretty bad...thanks guys, but you may be too good for your own good, cause now I will be back! :lol: :lol:

all kidding aside, I really appreciate communities that really take the time to help and empower people! thank you so much!