We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4385
    • 372 Posts
    I finishing up work on a site and burning the candle at both ends. I can seem to fix this simple problem. I have been struggling with way too long with not enough sleep.

    I pulling data from a third-party site and trying to "cache" the data so I do not have to keep calling the API. I am simply calling it and storing the results in an array. I then write that array to a file and include it into my snippet.

    It works fine when I call it once. But when I call multiple times on the page it fails. I can call it uncached but it will not update when the url parameter changes. At first I had a function and I thought that was causing the problem, so I re-wrote it, still no luck. Any ideas?


    displayCategory (snippet)

    <?php
    $catId = $_GET['ProductTypeID'];
    $file = 'assets/files/category_list.php';
    
    if (file_exists($file)) {
      $output = include_once $file;
    } else {
      $output = 'File not found at: '.$file;
    }
    
    $searched = array('ProductTypeId'=>$catId);
    foreach ($myArray as $key => $value) {
      $exists = true;
      foreach ($searched as $skey => $svalue) {
        $exists = ($exists && IsSet($myArray[$key][$skey]) && $myArray[$key][$skey] == $svalue);
      }
      if ($exists) {
        $output = $myArray[$key]['Name'];
      }
    }
    
    return $output;
    ?>
    


    category_list.php (the included array)

    <?php
    
    $myArray = array (
      0 => 
      array (
        'ProductTypeId' => 9,
        'Name' => 'Fruits',
        'ProductCount' => 109,
      ),
      1 => 
      array (
        'ProductTypeId' => 46,
        'Name' => 'Vegetables',
        'ProductCount' => 100,
      ),
      2 => 
      array (
        'ProductTypeId' => 71,
        'Name' => 'Grains',
        'ProductCount' => 10,
      ),
    
    ); ?>
    

      DropboxUploader -- Upload files to a Dropbox account.
      DIG -- Dynamic Image Generator
      gus -- Google URL Shortener
      makeQR -- Uses google chart api to make QR codes.
      MODxTweeter -- Update your twitter status on publish.
      • 3749
      • 24,544 Posts
      It appears that you are setting $output to the contents of the file, then throwing that value away by overwriting it in the loop. Did you mean to use $output .= in the loop?
        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
        • 4385
        • 372 Posts
        No need for the earlier $output, I was using that to make sure I had the file and the path correct.

        $output should return Vegetables, when called index.php?id=7&ProductTypeID=46

        and it does when I have it on page once, when I place the snippet on the page multiple times it does not return anything or I get the cached one depending how I call it.
          DropboxUploader -- Upload files to a Dropbox account.
          DIG -- Dynamic Image Generator
          gus -- Google URL Shortener
          makeQR -- Uses google chart api to make QR codes.
          MODxTweeter -- Update your twitter status on publish.
          • 4041
          • 788 Posts
          I made a few small changes to the above snippet, and this version works multiple times on a page, and also works on cached pages with the snippet call cached or uncached. (note that I tested this in Evolution)
          [!CatDisplay!]

          <?php
          $catId = $_GET['ProductTypeID'];
          $file = MODX_BASE_PATH.'assets/files/category_list.php';
           
          if (file_exists($file)) {
            //$output = include_once $file;
              include $file;
          } else {
            $output = 'File not found at: '.$file;
            return $output;
          }
           
          $searched = array('ProductTypeId'=>$catId);
          foreach ($myArray as $key => $value) {
            $exists = true;
            foreach ($searched as $skey => $svalue) {
              $exists = ($exists && isset($myArray[$key][$skey]) && $myArray[$key][$skey] == $svalue);
            }
            if ($exists) {
              $output = $myArray[$key]['Name'];
            }
          }
           
          return $output;
          ?>
            xforum
            http://frsbuilders.net (under construction) forum for evolution
            • 4385
            • 372 Posts
            I totally missed that from my testing. I had that part working and went to the next section and keep changing that part.

            Thank you breezer!
              DropboxUploader -- Upload files to a Dropbox account.
              DIG -- Dynamic Image Generator
              gus -- Google URL Shortener
              makeQR -- Uses google chart api to make QR codes.
              MODxTweeter -- Update your twitter status on publish.