1

I am trying to display my mysql rows to html bootstrap table. Database connection is working, displaying data is working, but it is not fancy as i want. I'd like to maybe save in arrayData maybe and then print this arrayData in html tag. Please any suggestions much appreciated. I want to do this easiest way and most convenient for editing later on. php code to display data :

    if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. " " . $row["lastName"]. "<br>"; /* and so on..*/
    }
} else {
    echo "0 results";
}

and this is my html code for bootstrap

    <table class="table table-striped">                     
<div class="table responsive">

  <thead>
    <tr>
      <th>#</th>
      <th>Name</th>
      <th>Last Name</th>
      <th>Number</th>
       <th>Info</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">1</th>
      <td>ales</td>
      <td>king</td>
      <td></td>
        <td></td>
    </tr>
    <tr>
      <th scope="row">2</th>
      <td>love</td>
      <td>2</td>
      <td>code</td>
        <td></td>
    </tr>

  </tbody>
        </div>
   </table>

EDIT: i want this, but loaded from database not manually typed :)!

enter image description here

14
  • 3
    what is the expected result supposed to look like? You didn't say much in your question regarding this. Commented May 1, 2016 at 14:52
  • sorry added picture my bad Commented May 1, 2016 at 14:57
  • oh, so you're looking for pagination then, is that it? Commented May 1, 2016 at 14:58
  • no. i want mysql data to display in this table. pagination is next! Commented May 1, 2016 at 15:00
  • 3
    oh, I see. Well it's not that hard really. Just place your <table></table> tags outside the loop and use the <tr></tr> and <td>$var</td> tags. HTML 101 stuff ;-) You can further your research by looking for "mysql html formatted table". Commented May 1, 2016 at 15:02

2 Answers 2

3

You can use a php loop this way

<table class="table table-striped">                     
    <div class="table responsive">
        <thead>
            <tr>
              <th>#</th>
              <th>Name</th>
              <th>Last Name</th>
              <th>Number</th>
               <th>Info</th>
            </tr>
        </thead>
        <tbody>
<?php 

....

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {


        echo '<tr>
                  <td scope="row">' . $row["id"]. '</td>
                  <td>' . $row["name"] .'</td>
                  <td> '.$row["lastName"] .'</td>
                </tr>';





    }
} else {
    echo "0 results";
} 
?>
       </tbody>
    </div>
</table>
Sign up to request clarification or add additional context in comments.

3 Comments

thanks works, i have one more question, if i have link in database how could i use <a href to it and add name LINK to display in table thank you!
<td> <a href='".$row['info']."'>Link</a></td> it doesn't work with <a href?
i fixed it a href without " "
1

I would do something like this: ( but you should really read some basic PHP )

<?php

echo "<table>";

// table header
echo "<tr><th>id</th><th>Name</th><th>Lastname</th></tr>";

// output data of each row
while($row = $result->fetch_assoc()) {

    echo "<tr><td>".$row["id"]."</td><td>".$row["name"]."</td><td>".$row["lastName"]."</td></tr>";
}

// table footer

echo "</table>";

?>

4 Comments

thanks works, i have one more question, if i have link in database how could i use <a href to it and add name LINK to display in table thank you!
Same as before: echo "<a href='".$row['linkfromdb']."'>Link</a>";
i copied your code and i get error 500 <td> <a href='".$row['info']."'>Link</a></td>
i fixed it a href without " "

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.