We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 35662
    • 12 Posts
    I’m pretty new to modx, I’m having to pick up where someone else left off. Can anyone tell me how change this code so it will work with Revolution 2.1

    $res = $modx->db->select('*', $table_name);
    $rowcount = $modx->db->getRecordCount($res);
    
    if($rowcount > 0) {
    
    	header("Content-type: application/csv");
    	header("Content-Disposition: attachment; filename=brochure-request-$d.csv");
    	header("Pragma: no-cache");
    	header("Expires: 0");
    
    	while($row = $modx->db->getRow($res)) {
    		
    		if($c == 0) {
    			$head = array_keys($row);
    			$out .= implode(',', $head) . "\n";
    		}
    		
    		$vals = array_values($row);
    		$out .= implode(',', $vals) . "\n"; 
    	
    		$c++;
    	}
    
      • 22303 MODX Staff
      • 10,725 Posts
      Something like this:

      <?php
      $stmt = $modx->query("SELECT * FROM {$table_name}");
      if ($stmt) {
      	header("Content-type: application/csv");
      	header("Content-Disposition: attachment; filename=brochure-request-$d.csv");
      	header("Pragma: no-cache");
      	header("Expires: 0");
      	$c = 0;
      	while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
      		if($c == 0) {
      			$head = array_keys($row);
      			$out .= implode(',', $head) . "\n";
      		}
      		
      		$vals = array_values($row);
      		$out .= implode(',', $vals) . "\n"; 
      	
      		$c++;
          }
      }

        • 35662
        • 12 Posts
        Boom! Thanks Jason!