user3838557
user3838557

Reputation:

How to use bootstrap css tables to display data from MySQL tables?

I'm trying to make it so that when a button is clicked on my main page, a php script takes action and fetches the information from the SQL tables, and displays it in a HTML/CSS table.

Here's the code of my main page -

<form id="myForm" action="select.php" method="post">
<button type="submit" class="btn btn-info" >
<span class="glyphicon glyphicon-tint"></span>  View
</button>
<br /> <span class="badge alert-info"> Find out what is currently in the database. </span><br />
</form>
<br />
<br />  

And here's what I currently have in my select.php -

<?php
/*** mysql hostname ***/
$hostname = '192.xx.xxx.xx';

/*** mysql username ***/
$username = 'Mitchyl';

/*** mysql password ***/
$password = 'root1323';

/*** database name ***/
$dbname = 'test';

try {
      $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
    /*** The SQL SELECT statement ***/
    $sql = "SELECT * FROM increment";

}
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
?>

I just don't know how to take the data from the SQL query and put it inside a HTML table.

Any suggestions would be great! Thanks!

Upvotes: 2

Views: 23677

Answers (2)

Milan and Friends
Milan and Friends

Reputation: 5610

Try this

<?php
  $hostname = '192.xx.xxx.xx';
  $username = 'Mitchyl';
  $password = 'root1323';
  $dbname = 'test';

  try {
    $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);

    $sql = $dbh->prepare("SELECT * FROM increment");

    if($sql->execute()) {
       $sql->setFetchMode(PDO::FETCH_ASSOC);
    }
  }
  catch(Exception $error) {
      echo '<p>', $error->getMessage(), '</p>';
  }

?>

  <div id="content">

    <table>
      <?php while($row = $sql->fetch()) { ?>
      <tr>
        <td><?php echo $row['column1_name']; ?></td>
        <td><?php echo $row['column2_name']; ?></td>
        <td><?php echo $row['column3_name']; ?></td>

        ...etc...

      </tr>
      <?php } ?>
    </table>

   </div>

Upvotes: 3

Marios Fakiolas
Marios Fakiolas

Reputation: 1545

Let's say your sql returns an array named $data and its format is sth like $data = [['name' => 'name1'], ['name' => 'name2'], ...];.

//First declare your data array
$data = [];

// Then execute the query
$result = $mysqli->query($sql)

// Then read the results and create your $data array
while($row = $result->fetch_array())
{
    $data[] = $row;
}

Now that you have your data check if it is empty or not and then use foreach to present the results.

<?php if(empty($data)): ?>
    <h1>No results were found!</h1>
<?php else: ?>
    <h1><?= count($data) ?> results were found!</h1>
    <table class="table">
        <thead>
            <th>#</th>
            <th>Name</th>
        </thead>
        <tbody>
        <?php foreach ($data as $key => $value): ?>
            <tr>
                <td><?= ++$key ?></td>
                <td><?= $value['name'] ?></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
<?php endif; ?>

Of course you can add any class you like to the table apart from the default one that bootstrap uses (.table).

Upvotes: 2

Related Questions