1

I have this code:

   include "xmlapi.php"; 

$pass  = 'testing1234'; 
$auser = 'testing'; 

$server = "0.0.0.0"; 
$port = 2087; 

$remote_api = new xmlapi($server); 

$remote_api->password_auth($auser, $pass); 
$remote_api->set_port($port); 
$remote_api->set_output('json'); 

$json_list = $remote_api->xmlapi_query('listaccts', array( 'api.version'=> 1)); 
$list = json_decode($json_list, true); 

if ( ! is_array($list) 
    || ! array_key_exists('data', $list) 
    || ! is_array($list['data']) 
    || ! array_key_exists('acct', $list['data']) 
) { 

    die("Invalid response!"); 
} 

$email_list = array(); 

foreach ($list['data']['acct'] as $acct) { 
    $username = $acct['user']; 
    $json_emails = $remote_api->api2_query($username, 'Email', 'listpopswithdisk', array()); 
    $acct_emails = json_decode($json_emails, true); 

    if ( is_array($acct_emails) 
        && array_key_exists('cpanelresult', $acct_emails) 
        && is_array($acct_emails['cpanelresult']) 
        && array_key_exists('data', $acct_emails['cpanelresult']) 
        && is_array($acct_emails['cpanelresult']['data']) 
    ) { 
        foreach ($acct_emails['cpanelresult']['data'] as $an_email) { 
            array_push( 
                $email_list, 
                array( 
                    'cpanel_account' => $username, 
                    'domain'         => $an_email['domain'], 
                    'email'          => $an_email['user'], 
                    'full_email'     => $an_email['email'],  
                    'diskused'     => $an_email['diskused'],  
                ) 
            ); 

            echo $an_email['email'] . ' - ';
            echo $an_email['diskused'] . ' - ';
            echo $an_email['user'] . '<br>';
        } 
    } 
} 

I used this code to connect to my cpanel and list all the email on the server. At first I just use var_dump to see if it works. And tried to use foreach loop but not success. How can I do a loop to echo all the data, so I can put the data in a table or something like that.

Please help me with this, thank you.

Sao Ho
  • 139
  • 2
  • 22
  • What is `$an_email`? Please post a [complete](https://stackoverflow.com/help/mcve) example and your desired result. – ggorlen Sep 10 '18 at 01:43
  • Please revisit the link above. This code can't be run by anyone trying to verify your problem and most of it isn't relevant to your question. It's not clear what the `$an_email` array contains. – ggorlen Sep 10 '18 at 01:58

1 Answers1

0

You need to parse the array containing the email accounts and print into a <table>

Try to adapt this code:

$email_list = [
  ['email' => 'first@email.com', 'diskused' => 'the diskused', 'user' => 'the first user'],
  ['email' => 'second@email.com', 'diskused' => 'the diskused', 'user' => 'the second user'],
  ['email' => 'third@email.com', 'diskused' => 'the diskused', 'user' => 'the third user'],
];

if (count($email_list)) {
  print '<table>';

  /* Set the header fields to print and Titles */
  $headers = [
    'email' => 'Email',
    'diskused' => 'Diskused',
    'user' => 'User'
  ];

  /* Print table headers */
  print '<tr>';
  foreach ($headers as $headerTitle) {
    print '<th>'.$headerTitle.'</th>';
  }
  print '</tr>';

  /* Parse all rows */
  foreach ($email_list as $row) {
    print '<tr>';

    /* Print all values */
    foreach (array_keys($headers) as $headerKey) {
      print '<td>'.$row[$headerKey].'</td>';
    }
    print '</tr>';
  }

  print '</table>';
}
Nomad Webcode
  • 816
  • 5
  • 9