Gga
Gga

Reputation: 4421

Weird single character output from function that turns array to HTML table in PHP

I've hit this a few time and was just wondering if anyone might know why this happens.

The array i'm passing is from the WHM/cPanel API, and of the form (var_dump()ed):

array (size=14)
  '_diskquota' => string '262144000' (length=9)
  '_diskused' => string '31459' (length=5)
  'diskquota' => string '250' (length=3)
  'diskused' => string '0.03' (length=4)
  'diskusedpercent' => string '0' (length=1)
  'diskusedpercent20' => string '0' (length=1)
  'domain' => string 'xxxxxxx.co.uk' (length=20)
  'email' => string '[email protected]' (length=25)
  'humandiskquota' => string '250Â MB' (length=7)
  'humandiskused' => string '30.72Â KB' (length=9)
  'login' => string '[email protected]' (length=25)
  'mtime' => string '1347964089' (length=10)
  'txtdiskquota' => string '250' (length=3)
  'user' => string 'info' (length=4)

I'm turning it into an HTML table with the below function:

public function formatEmailAccountsArrayToTable( $email_accounts )  {
    $returnHTML = '';
    $topentag   =   '<table class="email_accounts_table">';
    $theader    =   '<thead><tr>
                        <th>Email</th><th>User</th><th>Domain</th><th>Disk Quota</th><th>Disk Used</th>
                        </tr></thead><tbody>';
    $tclosetag  =   '</tbody></table>';

    $returnHTML .= $topentag . $theader;

    foreach( $email_accounts as $v ) {
        $returnHTML .= '
            <tr>
                <td>' . $v['email'] . '</td>
                <td>' . $v['user'] . '</td>
                <td>' . $v['domain'] . '</td>
                <td>' . $v['diskquota'] . '</td>
                <td>' . $v['diskused'] . '</td>
            </tr>';
    }
    $returnHTML .= $tclosetag;

    return $returnHTML;
}

The output is as follows:

Email   User    Domain  Disk Quota  Disk Used
2   2   2   2   2
3   3   3   3   3
2   2   2   2   2
0   0   0   0   0
0   0   0   0   0
0   0   0   0   0
b   b   b   b   b
i   i   i   i   i
2   2   2   2   2
3   3   3   3   3
i   i   i   i   i
1   1   1   1   1
2   2   2   2   2
i   i   i   i   i

I can't see why any of the values would be truncated/transformed to the values that are output in the table.

Does anyone know why this might be happening as the processes should be very straightforward but is behaving strangely?

Upvotes: 0

Views: 109

Answers (2)

billyonecan
billyonecan

Reputation: 20250

Your $email_accounts array is a two-dimensional array, which you're iterating over in a foreach loop, at which point $v represents a single property of that array.

Ideally you need to restructure your array in order for the function to work correctly, a quickfix would be to do something like:

$email_accounts = array($email_accounts);

Which would give you:

array
  0 => 
    array
      '_diskquota' => string '262144000' (length=9)
      '_diskused' => string '31459' (length=5)
      'diskquota' => string '250' (length=3)

Upvotes: 3

Night2
Night2

Reputation: 1153

Your $email_accounts array structure is like this:

array(
    'x1' => 'y', 
    'x2' => 'y'
)

It should be like this to work in your code:

array(
    0 => array(
        'x1' => 'y', 
        'x2' => 'y'
    ), 
    1 => array(
        'x1' => 'y', 
        'x2' => 'y'
    )
)

Upvotes: 3

Related Questions