Max
Max

Reputation: 940

Horizontal table instead of vertical with PHP

I created a horizontal html table with php using the suggested solution on this post: Printing out a table horizontal instead of vertical using PHP

And my code is like this:

$sql="SELECT Pr1, Pr2, Pr3, Pr4 FROM Tbdata ORDER BY Date DESC";
$result=mysqli_query($con,$sql);
$row=mysqli_fetch_assoc($result);

$Pr1 = '';
$Pr2 = '';
$Pr3 = '';
$Pr4 = '';

while($row = $result->fetch_assoc())
{

    $Pr4 .= '<td>'.$row['Pr4'].'</td>';
    $Pr3 .= '<td>'.$row['Pr3'].'</td>';
    $Pr2 .= '<td>'.$row['Pr2'].'</td>';
    $Pr1 .= '<td>'.$row['Pr1'].'</td>';
}


echo '
    <table class="table">     
        <tbody>
            <tr>
             <td>'.$Pr4.'</td>
            </tr>
            <tr>
             <td>'.$Pr3.'</td>
            </tr>
             <tr>
             <td>'.$Pr2.'</td>
             </tr>
             <tr>
             <td>'.$Pr1.'</td>
            </tr>
    </tbody>
    </table>
'; 

?>

The code works fine. The only problem is that I extract data with Date DESC in the query. For some reason, the data of the most recent date doesn't appear on the table. What am I missing here? please Thanks.

Upvotes: 0

Views: 809

Answers (2)

user3783243
user3783243

Reputation: 5224

Every fetch call advances the row count 1 position. Only have the while fetch call. Remove the preceding one (or comment it out, as I have to show).

//$row=mysqli_fetch_assoc($result);

$Pr1 = '';
$Pr2 = '';
$Pr3 = '';
$Pr4 = '';

while($row = $result->fetch_assoc())

Upvotes: 1

Nigel Ren
Nigel Ren

Reputation: 57121

You discard the first line...

$sql="SELECT Pr1, Pr2, Pr3, Pr4 FROM Tbdata ORDER BY Date DESC";
$result=mysqli_query($con,$sql);
$row=mysqli_fetch_assoc($result);   // Reads row, comment this out

Comment out that last line.

Also as you wrap each item in <td> tags, you don't need them in...

<td>'.$Pr4.'</td>

So remove the <td> and </td> tags in these.

Upvotes: 1

Related Questions