We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22019
    • 390 Posts
    Noob q, sorry. I’m working my way up to doing proper loops, and I’m playing around with various case studies and tutorials. I’ve created a custom table, schema, model etc, and I’m using getCollection and foreach to list each row within that collection (no error checking or whatever, I know there’s data there).

    When I output the data, it always seems to be prefixed with the word ’Array’ - which I assume means I’m doing something wrong. As far as I can tell I’ve only changed the variable names and copied someone’s example on the forum.

    Code:
     $base_path = !empty($base_path) ? $base_path : $modx->getOption('core_path').'components/ias_newsletter/';
    $modx->addPackage('ias_newsletter',$base_path.'model/');  
    $subscriber = $modx->newObject('ias_newsletter');
    $subscribers = $modx->getCollection('ias_newsletter',array('active'=>"1"));  
      
     echo 'Total active: '.count($subscribers);
      foreach ($subscribers as $z)
    {
        $subscribers .= $z->get('name')."<br />";
      $subscribers .= $z->get('email')."<br />";
      $subscribers .= $z->get('active')."<br />";
    }
    return $subscribers;



    1. So why’s it spitting out ’Array’ before the data I want?
    2. I’m a bit lost as to why the $subscriber object is required, or is it? Nothing seems to happen to it....
      Writer > E-consultant > MODx developer || Salesforce || modx 2.x || PHP 5.2.13 || MySQL client 5.0.86
      • 21257 MODX Staff
      • 730 Posts
      $subscribers is your collection of xPDO objects, but then you’re doing string concatenation on it.

      Maybe you want this:

      $base_path = !empty($base_path) ? $base_path : $modx->getOption('core_path').'components/ias_newsletter/';
      $modx->addPackage('ias_newsletter',$base_path.'model/');  
      //$subscriber = $modx->newObject('ias_newsletter');
      $subscribers = $modx->getCollection('ias_newsletter',array('active'=>"1"));  
      $output = '';
        
      echo 'Total active: '.count($subscribers);
      foreach ($subscribers as $z)
      {
          $output .= $z->get('name')."<br />";
          $output .= $z->get('email')."<br />";
          $output .= $z->get('active')."<br />";
      }
      return $output;


      Also, you’re right, you don’t seem to be doing anything with the $subscriber object, so it’s not needed from what I can see.
        Mike Schell
        Lead Developer, MODX Cloud
        Email: [email protected]
        GitHub: https://github.com/netProphET/
        Twitter: @mkschell
        • 22019
        • 390 Posts
        That works, thanks.
          Writer > E-consultant > MODx developer || Salesforce || modx 2.x || PHP 5.2.13 || MySQL client 5.0.86
          • 22303 MODX Staff
          • 10,725 Posts
          FWIW, another way to write the last part of that code using an array of the fields you want as the parameter for get():
          <?php
          foreach ($subscribers as $z)
          {
              $output .= implode("<br />", $z->get(array('name', 'email', 'active'));
          }
          return $output;
          ?>