We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18959
    • 6 Posts
    (my current php page code at end of page) (using latest MODx Revolution)

    I have a problem with converting a working PHP page into snippets, or generally MODx. As far as I know, snippets can handle PHP easily, and so far I converted all the echoes to returns and tested it out pretty much the same, and it didn’t work (it was called correctly as [[mysnippet]] but nothing shows up in the content area). I’ve tested out the page itself and it works. I actually have no clue how to convert this page to snippets/MODx, even after reviewing the docs on MODx.

    My working php page in action (really basic right now): http://www.hammerhead226.org/flickrtest/example.php

    My php code: (with echoes intact)
    <?php
    //flickr variables
    $flickrApiKey = 'b0e9db9193f9d4bddb2d42a81c7b351f';
    $flickrUserId = '57059624@N06';
    
    function flickrCall($params) {
    $encoded_params = array();
    
    foreach ($params as $k => $v){
    
    $encoded_params[] = urlencode($k).'='.urlencode($v);
    }
    
    
    #
    # call the API and decode the response
    #
    
    $url = "http://api.flickr.com/services/rest/?".implode('&', $encoded_params);
    
    $rsp = file_get_contents($url);
    
    $rsp_obj = unserialize($rsp);
    
    if($rsp_obj['stat']=='ok') {
    return($rsp_obj);
    }
    else{
    return(false);
    }
    }
    
    
    
    // *** detailed photo view ***
    if($photo = $_GET['photo']) {
    //poll flickr data
    $params = array(
    'api_key' => $flickrApiKey,
    'user_id' => $flickUserId,
    'format' => 'php_serial',
    'method' => 'flickr.photos.getInfo',
    'photo_id' => $photo
    );
    if($photo_info = flickrCall($params)) {
    $params = array(
    'api_key' => $flickrApiKey,
    'user_id' => $flickUserId,
    'format' => 'php_serial',
    'method' => 'flickr.photos.getSizes',
    'photo_id' => $photo
    );
    $photo_sizes = flickrCall($params);
    $photo_info[photo][sizes] = $photo_sizes[sizes][size];
    unset($photo_sizes);
    //print_r($photo_info);
    }
    else {
    print "Flickr API Error!";
    }
    
    // display code
    return('<h1>'.$photo_info[photo][title][_content].'</h1>
    ');
    return('<img src="'.$photo_info[photo][sizes][4][source].'" width="'.$photo_info[photo][sizes][4][width].'" height="'.$photo_info[photo][sizes][4][height].'" />
    ');
    return('<div id="description">.'.$photo_info[description][_content].'.</div>');
    // end display
    }
    
    
    
    // *** photo list (for photoset ***
    
    elseif($pset = $_GET['pset']){
    return('<ul class="photoflow">
    ');
    
    //poll flickr data
    $params = array(
    'api_key' => $flickrApiKey,
    'user_id' => $flickUserId,
    'format' => 'php_serial',
    'method' => 'flickr.photosets.getPhotos',
    'photoset_id' => $pset
    );
    if($photos = flickrCall($params)) {
    //print_r($photos);
    foreach($photos[photoset][photo] as &$photo) {
    $params = array(
    'api_key' => $flickrApiKey,
    'user_id' => $flickrUserId,
    'format' => 'php_serial',
    'method' => 'flickr.photos.getSizes',
    'photo_id' => $photo[id],
    );
    $photo_sizes = flickrCall($params);
    $photo[thumbSrc] = $photo_sizes[sizes][size][1][source];
    $photo[thumbW] = $photo_sizes[sizes][size][1][width];
    $photo[thumbH] = $photo_sizes[sizes][size][1][height];
    
    //display each photo
    return(' <li><a href="?photo='.$photo[id].'"><img src="'.$photo[thumbSrc].'" width="'.$photo[thumbW].'" height="'.$photo[thumbH].'" /></a></li>
    ');
    
    }
    unset($photo);
    //print_r($photos);
    }
    else {
    print "Flickr API Error!";
    }
    
    
    
    // end display
    return('</ul>
    ');
    
    
    }
    
    
    
    // *** photoset list ***
    
    else {
    
    return('<ul class="photoflow">
    ');
    
    
    //poll flickr data
    $params = array(
    'api_key' => $flickrApiKey,
    'user_id' => $flickrUserId,
    'format' => 'php_serial',
    'method' => 'flickr.photosets.getList',
    );
    if($photosets = flickrCall($params)) {
    //print_r($photosets);
    
    foreach ($photosets['photosets']['photoset'] as &$photoset) {
    
    $params = array(
    'api_key' => $flickrApiKey,
    'user_id' => $flickUserId,
    'format' => 'php_serial',
    'method' => 'flickr.photosets.getPhotos',
    'photoset_id' => $photoset[id]
    );
    if($photos = flickrCall($params)) {
    //print_r($photos);
    $photoset['thumbPhotoId'] = $photos['photoset']['photo'][0]['id'];
    
    $params = array(
    'api_key' => $flickrApiKey,
    'user_id' => $flickrUserId,
    'format' => 'php_serial',
    'method' => 'flickr.photos.getSizes',
    'photo_id' => $photoset['thumbPhotoId'],
    );
    $photo_sizes = flickrCall($params);
    $photoset['thumbSrc'] = $photo_sizes['sizes']['size'][1]['source'];
    $photoset['thumbW'] = $photo_sizes['sizes']['size'][1]['width'];
    $photoset['thumbH'] = $photo_sizes['sizes']['size'][1]['height'];
    //print_r($photo_sizes);
    
    //display each photoset
    return(' <li><a href="?pset='.$photoset['id'].'"><img src="'.$photoset['thumbSrc'].'" width="100px" height="100px" /><span>'.$photoset['title']['_content'].'</span></a></li>
    ');
    
    }
    else {
    print "Flickr API Error!";
    }
    }
    unset($photoset);
    }
    else {
    print "Flickr API Error!";
    }
    
    
    // end display
    return('</ul>
    ');
    }
    
    ?>
      • 3749
      • 24,544 Posts
      You have some PHP syntax errors in the code -- mainly misplaced curly braces.

      Also, you’ve converted the echos to return statements, so the code will end when it hits the first one and the rest of the code won’t execute.

      I pasted it in to a code editor and had to make some wild guesses about the structure, but this should be closer to what you want:

      <?php
      //flickr variables
      $flickrApiKey = 'b0e9db9193f9d4bddb2d42a81c7b351f';
      $flickrUserId = '57059624@N06';
      
      function flickrCall($params) {
          $encoded_params = array();
      
          foreach ($params as $k => $v) {
      
              $encoded_params[] = urlencode($k) . '=' . urlencode($v);
          }
      
      
          #
          # call the API and decode the response
          #
      
          $url = "http://api.flickr.com/services/rest/?" . implode('&', $encoded_params);
      
          $rsp = file_get_contents($url);
      
          $rsp_obj = unserialize($rsp);
      
          if ($rsp_obj['stat'] == 'ok') {
              return ($rsp_obj);
          } else {
              return (false);
          }
      }
      
      // *** detailed photo view ***
      if ($photo = $_GET['photo']) {
          //poll flickr data
          $params = array(
              'api_key' => $flickrApiKey,
              'user_id' => $flickUserId,
              'format' => 'php_serial',
              'method' => 'flickr.photos.getInfo',
              'photo_id' => $photo
          );
          if ($photo_info = flickrCall($params)) {
              $params = array(
                  'api_key' => $flickrApiKey,
                  'user_id' => $flickUserId,
                  'format' => 'php_serial',
                  'method' => 'flickr.photos.getSizes',
                  'photo_id' => $photo
              );
              $photo_sizes = flickrCall($params);
              $photo_info[photo][sizes] = $photo_sizes[sizes][size];
              unset($photo_sizes);
              //print_r($photo_info);
          }
          else {
              print "Flickr API Error!";
          }
      
          // display code
          $output .= ('<h1>' . $photo_info[photo][title][_content] . '</h1>
      ');
          $output .= ('<img src="' . $photo_info[photo][sizes][4][source] . '" width="' . $photo_info[photo][sizes][4][width] . '" height="' . $photo_info[photo][sizes][4][height] . '" />
      ');
          $output .= ('<div id="description">.' . $photo_info[description][_content] . '.</div>');
          // end display
      }
      
      
          // *** photo list (for photoset ***
      
      elseif ($pset = $_GET['pset']) {
          $output .= ('<ul class="photoflow">
      ');
      
          //poll flickr data
          $params = array(
              'api_key' => $flickrApiKey,
              'user_id' => $flickUserId,
              'format' => 'php_serial',
              'method' => 'flickr.photosets.getPhotos',
              'photoset_id' => $pset
          );
          if ($photos = flickrCall($params)) {
              //print_r($photos);
              foreach ($photos[photoset][photo] as &$photo) {
                  $params = array(
                      'api_key' => $flickrApiKey,
                      'user_id' => $flickrUserId,
                      'format' => 'php_serial',
                      'method' => 'flickr.photos.getSizes',
                      'photo_id' => $photo[id],
                  );
                  $photo_sizes = flickrCall($params);
                  $photo[thumbSrc] = $photo_sizes[sizes][size][1][source];
                  $photo[thumbW] = $photo_sizes[sizes][size][1][width];
                  $photo[thumbH] = $photo_sizes[sizes][size][1][height];
      
                  //display each photo
                  $output .= (' <li><a href="?photo=' . $photo[id] . '"><img src="' . $photo[thumbSrc] . '" width="' . $photo[thumbW] . '" height="' . $photo[thumbH] . '" /></a></li>
      ');
      
              }
              unset($photo);
              //print_r($photos);
          } else {
              $output =  "Flickr API Error!";
          }
      }
      
      return $output;


        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
        • 18959
        • 6 Posts
        Thank a lot. grin

        I’ll try it in a little bit later but that makes sense.

        Edit: Didn’t work =( But thanks anyways, put me on the right track
          • 32098
          • 31 Posts
          MDragon, like you, I am struggling. There are times when Modx seems like so much esoterica, the truth of which is hidden at the end of some long, tortuous process of initiation that few will be able to bear. I am just hoping that someone like Bob Ray writes a nice fat book (a really fat book) filled with easy to follow step by step instructions on how to get all this snippet action going successfully, making things like XPDO seem as tasty as jam doughnuts.

          But let me stick my neck out and presume to offer advice about how to get over that return issue that Mr Ray mentioned. You need to get the snippets working with chunks. You know the song about the horse and carriage? Well, in Modx the equivalent is snippets and chunks (although it doesn’t quite fit the music).

          Take a look at this bit of your code:

          return('<h1>'.$photo_info[photo][title][_content].'</h1>
          ');
          

          The initiated would say you need to create a chunk (lets say you call it myfirstchunk) that looks like this:

          <h1>[[+placeholder]]</h1>


          Then you change the relevant part of your snippet so that it looks a bit more Modxey, like so:

          $var = $photo_info[photo][title][_content];
              $output = $modx->getChunk('myfirstchunk', array(
                 'placeholder' => $var,
                     ));


          Once all the outputs have been strung together, use return $output;

          Voila!