0

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?

7
  • Your html is printing correct for me.no issues. see jsfiddle.net/Ag4Et Commented Jan 29, 2014 at 11:44
  • Are you sure this is the code of table? Commented Jan 29, 2014 at 11:46
  • @ViniciusMonteiro yes, I copied/pasted the source code of the page Commented Jan 29, 2014 at 11:47
  • @user2936213 also to me. It all seems correct but when displayed on the page, the table headings are shifted to the right. Commented Jan 29, 2014 at 12:00
  • can you provide the link of this page? Commented Jan 29, 2014 at 12:02

2 Answers 2

1

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">
Sign up to request clarification or add additional context in comments.

1 Comment

Looks like the id of header was picking up some styling from templates/yoo_nano2/styles/lines/css/layout.css .
0

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>

1 Comment

No, "Desvio" is there, in total 9 columns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.