We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11678
    • 160 Posts
    Hi; I want to change the header image on many different pages and I did not want to have to have a different template for every page. I thought that possibly a php case statement would work as a snippet but it fails. huh This is what I tried -

    <?php
    // Change top header image based on page ID
    $curPageNum = $modx->documentIdentifier;
    switch($curPageNum){
    case "1":div#branding background: url(images/logo1.jpg)no-repeat center;
    case:"2":div#branding background: url(images/logo2.jpg)no-repeat center;
    case:"3":div#branding background: url(images/logo3.jpg)no-repeat center;
    case:"4":div#branding background: url(images/logo4.jpg)no-repeat center;
    }
    ?>

    I’m doing something wrong here. Would it be easier to use PHx or?
    Any help would be appreciated. grin
    Hal D
      "Today’s headlines are nothing more than whispers of history"
      • 24495
      • 407 Posts
      I miss a return statement. The cases should set first a variable. And this should second be returned.
        • 12652
        • 228 Posts
        You could also approach this as a background image, then control it with CSS,

        or create a TV with radio buttons to select the header you want to use, which can also be combined with @INHERIT to maintain the same header automatically for sections.
          | Identity Developments delivers SEO focused web design and web presence services
          - it&#39;s not about websites, it&#39;s about your identity. |
          • 11678
          • 160 Posts
          Hmmmm!

          I tried this and it doesn’t work either;
          <?php
          // Change top header image based on page ID
          $curPageNum = $modx->documentIdentifier;
          switch($curPageNum){
          case:"1’:
          echo ’div#branding background: url(images/logo1.jpg)no-repeat center;"
          case:"65’:
          echo "div#branding background: url(images/logo2.jpg)no-repeat center;"
          case:"3":
          echo "div#branding background: url(images/logo3.jpg)no-repeat center;"
          case:"4":
          echo "div#branding background: url(images/logo4.jpg)no-repeat center;"
          }
          ?>

          I put the call to the [[imagereplace]] right after body in the html.
          Must be a better way to do this. My php is rather rusty.
          Hal D
            "Today’s headlines are nothing more than whispers of history"
            • 11678
            • 160 Posts
            I think I just found part of my answer in a different thread.

            TobyL responded to someone with this bit;

            "One thing to remember is that your code should not use echo or print to output anything but instead should return this as a value. Use output buffering if needed. There’s more ways of doing this off course..."

            Which I guess, is where I have been going wrong using echo. Do I need to put it this into a php function? And what would the return look like?

            cheers,
            HalD
              "Today’s headlines are nothing more than whispers of history"
              • 24495
              • 407 Posts
              Try this:
              case:"1":
                    $brandimg == "yourcss1";
              case:"2":
                    $brandimg == "yourcss2";
              ...
              }
              return $brandimg;
                • 7923
                • 4,213 Posts
                Many things are wrong with that. Switch statement syntax is wrong, you mix single quotes with double quotes, css syntax is wrong, do not use echo, etc...

                Try something like this:
                <?php
                // Change top header image based on page ID
                $curPageNum = $modx->documentIdentifier;
                switch($curPageNum){
                	case 1:
                		$output='div#branding { background: url(images/logo1.jpg)no-repeat center;}';
                		break;
                	case 65:
                		$output='div#branding { background: url(images/logo2.jpg)no-repeat center;}';
                		break;
                	case 3:
                		$output='div#branding { background: url(images/logo3.jpg)no-repeat center;}';
                		break;
                	case 4:
                		$output='div#branding { background: url(images/logo4.jpg)no-repeat center;}';
                		break;
                	default: // if you want a default header?
                		$output='div#branding { background: url(images/default.jpg)no-repeat center;}';
                }
                return $output;
                ?>
                


                And instead of returning the output and calling the snippet inside <style> tags, you could use regClientCSS function to register the css:

                replace:

                return $output;

                with:

                $modx->regClientCSS(’<style type="text/css">\n’.$output.’\n</style>’);
                return;


                  "He can have a lollipop any time he wants to. That's what it means to be a programmer."
                  • 11678
                  • 160 Posts
                  Doze; I thank you for your help. In the instance where I used regClientCSS, where would it be called? In the head section of the html or ?
                  Thanks again for your help,
                  Hal D
                    "Today’s headlines are nothing more than whispers of history"
                    • 7923
                    • 4,213 Posts
                    if you use regClientCSS, it doesn’t matter where the snippet is called, it makes modx to put the css to the <head> section always


                      "He can have a lollipop any time he wants to. That's what it means to be a programmer."
                      • 11678
                      • 160 Posts
                      Doze; It doesn’t work either way, and there is no sense wasting more time on it. I’ll just use different templates.
                      Thanks anyway,
                      Hal D
                        "Today’s headlines are nothing more than whispers of history"