We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34757
    • 38 Posts
    Hello everyone, after some hours spent trying to solve this problem, i'll remit to you...
    First sorry for my bad english..

    1. i have a parent resource that shows his children via a ditto call
    2. the ditto call uses a chunk to show children
    3. inside this chunk there's a snippet call to get&show youtube/vimeo videos linked to the current child
    4. inside the snippet there's a php function that outputs the right embedding code for the video

    Here's the problem: when ditto iterates with the chunk the children, it calls the snippet (and thus the php function) for each child, and i get the php fatal error "Cannot redeclare function-name()......."

    I simply need to show each children and his videos in a page... it's a normal purpose i think.. Probably there's something wrong in my logic...

    How to make it work?

    Hope i was clear.
    Thanks in advance
      • 9674
      • 170 Posts
      sounds like you need a php expert...
      I'm not...
      I have been in a similar problem with a gallery, and my approach was wrong.
      Someone in the community will certainly be able to help
        • 34757
        • 38 Posts
        thanks for the moral support
          • 4041
          • 788 Posts
          wrap your function like this:

          if(!function_exists(FUNCTION_NAME)){
            function FUNCTION_NAME(){
              stuff here...
            }
          }
            xforum
            http://frsbuilders.net (under construction) forum for evolution
            • 34757
            • 38 Posts
            Quote from: breezer at Nov 05, 2011, 10:38 AM
            wrap your function like this:

            if(!function_exists(FUNCTION_NAME)){
              function FUNCTION_NAME(){
                stuff here...
              }
            }

            if i do this only the first call works
              • 4041
              • 788 Posts
              ok maybe try this:

              1. Remove the function out of your existing snippet which is called in the chunk and add it at the top of the code below.
              2. Take the parameters used in the ditto call on your page and add them to the "param" part of the runsnippet function.
              3. Create a snippet (named whatever you wish) with the modified code below and replace the ditto call on your pages with the snippet call: [!Snippet_Name!]

              <?php
              
              if(!function_exists(FUNCTION_NAME)){
                  function FUNCTION_NAME(){
                      stuff here....
                  }
              }
              
              $output ='';
              $output .=$modx->runSnippet("Ditto",array(
                              "tpl"  => "CHUNK_NAME",
                              "param2"  => "value2",
                              "param3"  => "param3"
                              ));
                              // note no comma after the last param
              
              return $output;
              ?>
                xforum
                http://frsbuilders.net (under construction) forum for evolution
                • 34757
                • 38 Posts
                Hello, first thanks for your reply and sorry for the long period without answering your solution to my problem, i was working on higher priority projects and i've forgotten this thread!

                I've tried your solution this morning, but i get the same error: "Cannot redeclare function-name()..."
                Strange because it passes the !function_exists control...

                At this point i think i made a messy code in the function or a wrong logic path.. don't know...
                If you have some other ideas....

                Thanks
                PS: sorry for my (maybe) bad english..
                  • 3749
                  • 24,544 Posts
                  Did you maybe put a $ at the front of your function name?

                  If you post your full snippet code here, we might see something that would help.
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 34757
                    • 38 Posts
                    Did you maybe put a $ at the front of your function name?
                    Dollar sign?? Maybe i'm wrong, but if i do this the function becomes a variable...

                    However, here's my function:
                    function $embedVideo($video,$giro,$size) {
                      if (strpos($video, "#") !== FALSE) {
                          $cut = explode("#", $video);
                    	  $video = $cut[0];
                      }	
                      if( $size == "small") { 
                          $img_set = "&w=130&h=70&zc=1&fltr[]=wmi|[(base_url)]/assets/templates/luigicinque/img/play_wm.png|68x38|66|50|50|0";
                          $giro = 0;
                      }
                      else {
                        $img_set = "&w=270&h=150&zc=1&fltr[]=wmi|[(base_url)]/assets/templates/luigicinque/img/play_wm.png|135x75|66|85|85|0";
                      }  
                      $rel = "rel=\"shadowbox;width=640;height=385\"";
                      if (is_int($giro/2) == true){
                    	$class = "mrg";
                      }
                      else { $class = ""; }
                      $video = trim($video);
                    
                      if (strpos($video, "youtube") !== FALSE) {
                        $split = explode("&",$video);
                        $split2 = explode("=",$split[0]);
                        $v_id = $split2[1];
                        $img = "[(base_url)]image.php?src=http://img.youtube.com/vi/".$v_id."/0.jpg";
                        echo '<a href="http://www.youtube.com/embed/'.$v_id.'?rel=0&autoplay=1" '.$rel.'><img src="'.$img.$img_set.'" class="gray '.$class.'"></a>'."\n";
                      }
                      if (strpos($video, "vimeo") !== FALSE) { 
                        $split = explode("vimeo.com/",$video); 
                        $v_id = $split[1];
                        $json = @json_decode(file_get_contents('http://vimeo.com/api/oembed.json?url=http://vimeo.com/'.$v_id));
                        $thumb = $json->thumbnail_url;
                        $img = "[(base_url)]image.php?src=".$thumb;
                        echo '<a href="http://player.vimeo.com/video/'.$v_id.'?title=0&byline=0&portrait=0&autoplay=1" '.$rel.'><img src="'.$img.$img_set.'" class="gray '.$class.'"></a>'."\n";
                      }
                      if (strpos($video, "dailymotion") !== FALSE) {
                        $split = explode("video/",$video);
                        $v_id = $split[1];
                        $split = explode("_",$v_id);
                        $v_id = $split[0];
                        $img = "[(base_url)]image.php?src=http://www.dailymotion.com/thumbnail/video/".$v_id;
                        echo '<a href="http://www.dailymotion.com/embed/video/'.$v_id.'?autoPlay=1" '.$rel.'><img src="'.$img.$img_set.'" class="gray '.$class.'"></a>'."\n";
                      }
                    }

                    As you may have guessed, is a function to embed video using the URL... I'm thinking that echoing directly the results is not the right way... Maybe is better using return.. what do you think about it?

                    thanks

                    EDIT: I don't know why but when i post my code, an <a> tag is automatically inserted before all urls that starts whit http:// :
                    $img = "[(base_url)]image.php?src=<a href="http://img.youtube.com/vi/".$v_id."/0.jpg";" target="_blank" rel="nofollow">http://img.youtube.com/vi/".$v_id."/0.jpg";</a>


                    [ed. note: dscvr last edited this post 14 years, 9 months ago.]
                      • 3749
                      • 24,544 Posts
                      Sorry, I wasn't suggesting adding a dollar sign. I was suggesting that you might have put one there by mistake.

                      You definitely want to create a return variable ($output) and return it at the end rather than echoing the results, though I'm not sure that will solve your problem.
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting