0

i'm new into the PHP programming, and i would need some help from you, how can i do a single table for the information that's received from the data base, or how can i retrieve all the data from the data base at once, without that table being rewrited for each line of information? This is my code and an image:

<?php
  include_once 'includes/dbh.inc.php';
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <meta charset="utf-8">
    <title>mysql connect</title>
  </head>
  <body>
    <?php
      $sql = "SELECT * FROM users;";
      $results = mysqli_query($conn, $sql);
      $resultCheck = mysqli_num_rows($results);

      if ($resultCheck > 0){
        while ($row = mysqli_fetch_assoc($results)) {


          echo '<center><table class="table table-dark"><thead><tr><th scope="col">#</th><th scope="col">Nume</th></tr></thead><tbody><tr><th scope="row">#</th><td>'.$row['user_last']."</td></tr></tbody></table></center> ";

        }
      }
    ?>

  </body>
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</html>

The Problem

1
  • 1
    Don't echo the whole table inside the while loop, only the table rows inside the body that repeat. Commented Aug 23, 2018 at 18:39

2 Answers 2

1

You need to loop only the tr not the whole table. like below

<?php
      include_once 'includes/dbh.inc.php';
    ?>
  <!DOCTYPE html>
  <html lang="en" dir="ltr">

  <head>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <meta charset="utf-8">
    <title>mysql connect</title>
  </head>

  <body>
    <table class="table table-dark">
      <thead>
        <tr>
          <th scope="col">#</th>
          <th scope="col">Nume</th>
        </tr>
      </thead>
      <tbody>
        <?php
          $sql = "SELECT * FROM users;";
          $results = mysqli_query($conn, $sql);
          $resultCheck = mysqli_num_rows($results);

          if ($resultCheck > 0){
           $i=1;
            while ($row = mysqli_fetch_assoc($results)) {
              echo '<tr><th scope="row">'.$i.'</th><td>'.$row['user_last']."</td></tr>";
              $i++;
            }
          }
        ?>
      </tbody>
    </table>
  </body>
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

  </html>
Sign up to request clarification or add additional context in comments.

Comments

0

Under your while loop try this

<th>#</th>
<th>firstname</th>

<?php 
 Echo "<tr>
               <td>{$row['id']}</td>
                <td>{$row['firstname']}</td>
             </tr>";

?>

If this does not work try to put semi colon after the index of id and firstname. Hope this will help

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.