We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17035
    • 36 Posts
    Ok scrap that!

    I've got this:

    /**
     
    1) Make sure we have our Modx Object.
    2) Table names - adjust for Evo.
    3) SQL with the fields we need - adjust for Evo.
    4) Get a file handle.
    5) Set the header so it outputs as csv.
    6) Loop the results.
    7) On the first iteration, set the csv headers from the DB field names.
    8) Create csv row.
    9) Output to browser.
     
    */
     
    // 1)
    if($modx) {
     
        // 2)
        $table1 =  $modx->getFullTableName("web_user_attributes");
        $table2 =  $modx->getFullTableName("web_users");
        $table3 =  $modx->getFullTableName("foxycart_pins");
        $table4 =  $modx->getFullTableName("foxycart_subscriptions");
         
        // 3)
        $sql = "SELECT
                `ua`.`fullname`, 
                `ua`.`email`, 
                `ua`.`logincount`, 
                `ua`.`failedlogincount`, 
                `ub`.`pin`, 
                `uc`.`start_date`, 
                `uc`.`exp_date`, 
                `uc`.`frequency`, 
                `uc`.`status`, 
                DATE_FORMAT(FROM_UNIXTIME(`ua`.`thislogin`),'%W, %M %e, %Y @ %h:%i %p') as `last_login`           
                FROM $table1 `ua` 
                JOIN $table3 `ub`, $table4 `uc`, $table2 `u`
                ON (`ua`.`id` = `ub`.`id` = `uc`.`id` = `u`.`id`)
                ORDER BY `ua`.`fullname` ASC";
            
        // You could check/refine the query in PhpMyAdmin...     
        // die($sql);
                 
        $result = $modx->db->query($sql);
        if($modx->db->getRecordCount($result) >= 1) {
     
           // 4)
           $stdout = fopen('php://output', 'w');
            
           // 5)    
           header('Content-type: application/csv');
           header('Content-Disposition: attachment; filename="export_userlogins_' . date("d-m-Y") . '.csv"');
             
           $c = 0;
            
           // 6)
           while($row = $modx->db->getRow($result)) {
            
               // 7)
               if($c == 0) { fputcsv($stdout, array_keys($row)); }
                
               // 8)
               fputcsv($stdout, $row); 
                
               $c++;
           }
     
        } else {
            return "None found";
        }
             
        // 9)
        fclose($stdout); 
        exit; 
     
    } else {
        return "Missing Modx Object";
    }
     
    


    But I'm getting this SQL Error:

    « Execution of a query to the database failed - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ON (`ua`.`id` = `ub`.`id` = `uc`.`id` = `u`.`id`) ORDER BY `ua`.`fu' at line 14 »
          SQL: SELECT `ua`.`fullname`, `ua`.`email`, `ua`.`logincount`, `ua`.`failedlogincount`, `ub`.`pin`, `uc`.`start_date`, `uc`.`exp_date`, `uc`.`frequency`, `uc`.`status`, DATE_FORMAT(FROM_UNIXTIME(`ua`.`thislogin`),'%W, %M %e, %Y @ %h:%i %p') as `last_login` FROM `secretse_mod01`.`modx_web_user_attributes` `ua` JOIN `secretse_mod01`.`modx_foxycart_pins` `ub`, `secretse_mod01`.`modx_foxycart_subscriptions` `uc`, `secretse_mod01`.`modx_web_users` `u` ON (`ua`.`id` = `ub`.`id` = `uc`.`id` = `u`.`id`) ORDER BY `ua`.`fullname` ASC 


    Any ideas? Sorry for all the questions!

    Regards
    Lewis
      • 36906
      • 34 Posts
      Spot on.
      If you specify the ON for each JOIN it should sort it:

      SELECT
                  `ua`.`fullname`, 
                  `ua`.`email`, 
                  `ua`.`logincount`, 
                  `ua`.`failedlogincount`, 
                  `ub`.`pin`, 
                  `uc`.`start_date`, 
                  `uc`.`exp_date`, 
                  `uc`.`frequency`, 
                  `uc`.`status`, 
                  DATE_FORMAT(FROM_UNIXTIME(`ua`.`thislogin`),'%W, %M %e, %Y @ %h:%i %p') as `last_login`           
                  FROM $table1 `ua` 
                  JOIN $table2 `u`
                  ON (`u`.`id` = `ua`.`id`)
                  LEFT JOIN $table3 `ub` 
                  ON (`ub`.`user_id` = `u`.`id`)
                  LEFT JOIN $table4 `uc`, 
                  ON (`uc`.`user_id` = `u`.`id`) 
                  ORDER BY `ua`.`fullname` ASC


      You'll need to fettle the foreign keys so that they have the correct association.

      One thing to think about is that you may need to use a LEFT JOIN to make sure that you still get the user tables even if there isn't a corresponding FoxyCart table row.

      http://dev.mysql.com/doc/refman/5.0/en/join.html
      http://mysqljoin.com/

        • 36906
        • 34 Posts

        Hi,

        Yep its because of the ON statement has too many arguments:
        ON (`ua`.`id` = `ub`.`id` = `uc`.`id` = `u`.`id`)

        Just join on the foreign key...
        ON (`ua`.`id` = `ub`.`id`)

          • 17035
          • 36 Posts
          Awesome! Nailed it!

          I just tried to copy in the code you sent and for some reason it didn't work. So I started again from the base code you sent over and tried adding once table in until it worked, then added the second one in. I've edited the way the dates are formatted and it works like a charm now.

          Just incase anyone wants to see active users on MODx evo in the future, the working code is as follows:

          /**
           
          1) Make sure we have our Modx Object.
          2) Table names - adjust for Evo.
          3) SQL with the fields we need - adjust for Evo.
          4) Get a file handle.
          5) Set the header so it outputs as csv.
          6) Loop the results.
          7) On the first iteration, set the csv headers from the DB field names.
          8) Create csv row.
          9) Output to browser.
           
          */
           
          // 1)
          if($modx) {
           
              // 2)
              $table1 =  $modx->getFullTableName("web_user_attributes");
              $table2 =  $modx->getFullTableName("web_users");
              $table3 =  $modx->getFullTableName("foxycart_pins");
              $table4 =  $modx->getFullTableName("foxycart_subscriptions");
               
              // 3)
              $sql = "SELECT
                      `ua`.`fullname`, 
                      `ua`.`email`, 
                      `ua`.`logincount`, 
                      `ua`.`failedlogincount`, 
                      `ub`.`pin`, 
                      `uc`.`frequency`, 
                      `uc`.`status`, 
                      DATE_FORMAT(FROM_UNIXTIME(`uc`.`start_date`),'%m-%d-%Y') as `start_date`,
                      DATE_FORMAT(FROM_UNIXTIME(`uc`.`exp_date`),'%m-%d-%Y') as `exp_date`,  
                      DATE_FORMAT(FROM_UNIXTIME(`ua`.`thislogin`),'%m-%d-%Y @ %h:%i %p') as `last_login`           
                      FROM $table1 `ua` 
                      JOIN $table2 `u`
                      ON (`u`.`id` = `ua`.`id`)
                      LEFT JOIN ($table3 `ub`)
                      ON (`u`.`id` = `ub`.`id`)
                      LEFT JOIN ($table4 `uc`)
                      ON (`uc`.`user_id` = `u`.`id`)
                      ORDER BY `uc`.`status` ASC";
                  
              // You could check/refine the query in PhpMyAdmin...     
              // die($sql);
                       
              $result = $modx->db->query($sql);
              if($modx->db->getRecordCount($result) >= 1) {
           
                 // 4)
                 $stdout = fopen('php://output', 'w');
                  
                 // 5)    
                 header('Content-type: application/csv');
                 header('Content-Disposition: attachment; filename="export_userlogins_' . date("d-m-Y") . '.csv"');
                   
                 $c = 0;
                  
                 // 6)
                 while($row = $modx->db->getRow($result)) {
                  
                     // 7)
                     if($c == 0) { fputcsv($stdout, array_keys($row)); }
                      
                     // 8)
                     fputcsv($stdout, $row); 
                      
                     $c++;
                 }
           
              } else {
                  return "None found";
              }
                   
              // 9)
              fclose($stdout); 
              exit; 
           
          } else {
              return "Missing Modx Object";
          }
           
          


          I've got it sorted by Status now I can see active users etc.

          Thank you for all your help, you've been fantastic!

          Many thanks
          Lewis