0

Could anyone tell me how to adapt or change this code to print multiple rows from a database table into an html table using PHP? Every user will have a different number of records in the table so I will need to use some kind of loop to print out the rows.

<?php

                    include_once 'dbconfig.php';

                    $statement = $db_con->prepare("SELECT * FROM entry WHERE user_id=:uid");
                    $statement->execute(array(":uid"=>$_SESSION['user_session']));
                    $result=$statement->fetch(PDO::FETCH_ASSOC);

                    ?>

                    <table>
                        <tr>
                        <th>Entry</th>
                            <th>Date</th>
                            <th>Weight</th>
                            <th>BMI</th>
                            <th>Calories Consumed</th>
                            <th>Calories Burned</th>
                            <th>Calorific Deficit</th>
                        </tr>

                    <?php

 while($row = mysql_fetch_array($result)) { 

        $entry = $row["entry_id"];           
        $date = $row["date"]; 
        $weight = $row["weight"];
        $bmi = $row["bmi"]; 
        $consumed = $row["calories_consumed"]; 
        $burned = $row["calories_burned"];
        $deficit = $row["calorie_deficit"]; 

        echo "<tr>
        <td>$entry</td>
        <td>$date</td>
        <td>$weight</td>
        <td>$bmi</td>
        <td>$consumed</td>
        <td>$burned</td>
        <td>$deficit</td>
        </tr>";

}

                    ?>

                        </table>

This currently works fine but it only prints out the first row relevant to the particular user rather than looping through and printing a row for each record.

Updated to include while loop

3
  • 2
    while(something is true) { do this } Commented Mar 29, 2016 at 15:07
  • I have updated my answer with what I have now but for some reason it isn't printing any rows after the header row Commented Mar 29, 2016 at 15:42
  • You're mixing PDO with mysql_, you can't do that. Commented Mar 29, 2016 at 15:45

1 Answer 1

0
while ($row =$statement->fetch(PDO::FETCH_ASSOC)) {
            $title1 = $row["title1"]; 
            $title2 = $row["titl2"]; 
            $title3 = $row["title3"]; 

            echo "<tr> 
            <td>$title1</td>
            <td>$title2</td>
            <td>$title3</td>
             </tr>";
    }
Sign up to request clarification or add additional context in comments.

3 Comments

I tried something similar to this earlier and now tried it this way. For some reason it isn't returning any data into the table.
I've tried this code and it does seem to be an improvement as the relevant rows have been added when I check using the inspector tools. However the data still isn't being added to each cell
Fixed! I changed the for the while loop to while ($row = $statement->fetch(PDO::FETCH_ASSOC)) and this solved my problem. Thanks for your help Draviya!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.