user882670
user882670

Reputation:

Produce HTML table from mysql data using PHP not working

I want to create an HTML table using data from a mysql table. This is the php code I'm using:

while ($row = $result->fetch())
{
  $registos[] = array('id'=>$row['id'], 'data'=>$row['data'], 'conta'=>$row['conta_id'], 
    'categoria'=>$row['categoria_id'], 'entidade'=>$row['entidade_id'], 'orcamento'=>$row['orcamento'],
    'atual'=>$row['atual'],'user'=>$row['user_key'], 'desvio'=>$row['desvio']);
}

echo 
    '<table border="1" cellspacing="0" cellpadding="0" class="tabela" style="margin:auto;">
    <tbody>
    <tr valign="middle" id="header">
        <th>id</th>
        <th>Data</th>
        <th>Conta_id</th>
        <th>Categoria_id</th>
        <th>Entidade_id</th>
        <th>Orçamento</th>
        <th>Atual</th>
        <th>User_key</th>
        <th>Desvio</th>
    </tr>';

foreach ($registos as $registo){
    //print ($registo['conta']);
    echo '<tr>';
    echo '<td>'.$registo['id'].'</td>';
    echo '<td>'.$registo['data'].'</td>';
    echo '<td>'.$registo['conta'].'</td>';
    echo '<td>'.$registo['categoria'].'</td>';
    echo '<td>'.$registo['entidade'].'</td>';
    echo '<td>'.$registo['orcamento'].'</td>';
    echo '<td>'.$registo['atual'].'</td>';
    echo '<td>'.$registo['user'].'</td>';
    echo '<td>'.$registo['desvio'].'</td>';
    echo '</tr>';
};

echo 
    '</tbody></table>';

This is producing the following HTML:

<table border="1" cellspacing="0" cellpadding="0" class="tabela" style="margin:auto;">
    <tbody>
    <tr valign="middle" id="header">
        <th>id</th>
        <th>Data</th>
        <th>Conta_id</th>
        <th>Categoria_id</th>
        <th>Entidade_id</th>
        <th>Orçamento</th>
        <th>Atual</th>
        <th>User_key</th>
        <th>Desvio</th>
    </tr><tr><td>1</td><td>2014-01-21</td><td>3</td><td>4</td><td>5</td><td>10000.00</td><td>1800.00</td><td>1</td><td>-8200.00</td></tr><tr><td>2</td><td>2014-01-23</td><td>4</td><td>4</td><td>5</td><td>500.00</td><td>400.00</td><td>1</td><td>-100.00</td></tr><tr><td>3</td><td>2014-01-13</td><td>5</td><td>3</td><td>4</td><td>50.00</td><td>150.00</td><td>1</td><td>100.00</td></tr><tr><td>4</td><td>2014-01-08</td><td>3</td><td>4</td><td>3</td><td>500.00</td><td>900.00</td><td>1</td><td>400.00</td></tr><tr><td>5</td><td>2014-01-15</td><td>6</td><td>4</td><td>4</td><td>80.00</td><td>90.00</td><td>1</td><td>10.00</td></tr><tr><td>6</td><td>2014-01-15</td><td>5</td><td>3</td><td>4</td><td>1800.00</td><td>1650.00</td><td>1</td><td>-150.00</td></tr></tbody></table>

Which apparently is correct, but the page is displaying the table like:

table

What is wrong with this code?

Upvotes: 0

Views: 193

Answers (2)

John Lay
John Lay

Reputation: 301

I could only reproduce the error in Chrome, but you have a number of validation errors in your markup. Specifically you have 2 element id's called header. change the id of the tr to something else.

Change:

<tr valign="middle" id="header">

to this (or similar):

<tr valign="middle" id="tableHeader">

Upvotes: 1

richie
richie

Reputation: 801

You seem to be missing values for "Desvio"

<td>-8200.00</td></tr><tr>

Should be:

<td>-8200.00</td><td>&nbsp;</td></tr><tr>

Upvotes: 0

Related Questions