• We just launched and are currently in beta. Join us as we build and grow the community.

Creating and Populating table with MySQL Data using Twitter Bootstrap framework

bunty1925

Long-Tail Keyword Pro
Divine
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
34
Likes
32
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this tutorial, I’m going to discuss how to use the Twitter Bootstrap to create a table and populate this table with records from MySQL database using PHP. Using Bootstrap framework, you can save more time when working with different browsers and or devices. And Bootstrap contains HTML and CSS based design templates that you can use this to improve the quality of your web application.

First you need to download Twitter Bootstrap framework.

Then we’ll create a folder named "bootstraptable" and extract the bootstrap framework, then copy and paste the "css" folder and its content, same with the "fonts" folder. And finally, we will put the "bootstraptable" folder inside "htdocs" folder.

Next, we'll create "list.php" file and add the following code:

In this code, we just added a link to our "bootstrap.css" and this is located inside "css" folder. And we are presenting a tabular table here. Since we’re creating a table, the hierarchy should be like this:

.

And in our case, we are using class="table".
  1. <!

    DOCTYPE html>

  2. <

    html>
  3. <

    head>
  4. <

    meta charset=

    "utf-8"

    >
  5. <

    meta content=

    "width=device-width, initial-scale=1.0"

    name=

    "viewport"

    >
  6. <

    meta content=

    ""

    name=

    "description"

    >
  7. <

    meta content=

    ""

    name=

    "author"

    >
  8. <

    link

    href=

    ""

    rel=

    "shortcut icon"

    >

  9. <

    title>

    List

    of Student</

    title><!--

    Bootstrap core CSS -->
  10. <

    link

    href=

    "css/bootstrap.css"

    rel=

    "stylesheet"

    >
  11. </

    head>

  12. <

    body>
  13. <

    div class

    =

    "container"

    >
  14. <

    table class

    =

    "table"

    >
  15. <

    thead>
  16. <

    tr>
  17. <

    th>

    Firstname</

    th>

  18. <

    th>

    Lastname</

    th>

  19. <

    th>

    Email</

    th>

  20. <

    th>

    Phone</

    th>
  21. </

    tr>
  22. </

    thead>

  23. <

    tbody>
  24. <

    tr>
  25. <

    td>

    sdsd</

    td>
  26. </

    tr>
  27. </

    tbody>

  28. <

    tbody></

    tbody>
  29. </

    table>
  30. </

    div><!--

    /

    container -->
  31. </

    body>
  32. </

    html>

And it looks like as shown below.

table1.png


Next, we’re going to populate our table with a record from MySQL database. In the table below

, add the following code.
  1. <?php
  2. //set up mysql connection
  3. mysql_connect

    (

    "localhost"

    ,

    "root"

    ,

    ""

    )

    or die

    (

    mysql_error

    (

    )

    )

    ;
  4. //select database
  5. mysql_select_db

    (

    "studentdb"

    )

    or die

    (

    mysql_error

    (

    )

    )

    ;
  6. // Retrieve all the data from the "tblstudent" table
  7. $result

    =

    mysql_query

    (

    "SELECT * FROM tblstudent"

    )

    or die

    (

    mysql_error

    (

    )

    )

    ;
  8. // store the record of the "tblstudent" table into $row

  9. while

    (

    $row

    =

    mysql_fetch_array

    (

    $result

    )

    )

    {
  10. // Print out the contents of the entry
  11. echo

    '<tr>'

    ;
  12. echo

    '<td>'

    .

    $row

    [

    'firstname'

    ]

    .

    '</td>'

    ;
  13. echo

    '<td>'

    .

    $row

    [

    'lastname'

    ]

    .

    '</td>'

    ;
  14. echo

    '<td>'

    .

    $row

    [

    'email'

    ]

    .

    '</td>'

    ;
  15. echo

    '<td>'

    .

    $row

    [

    'phone'

    ]

    .

    '</td>'

    ;
  16. }
  17. ?>

And the out output should look like as shown below.
table2.png


This time, we’re going to use a "zebra-stripped" CSS class defined in the associated bootstrap css file.
Now let's modify our code. Instead of using the table class we change it into a table-striped class. And our code now will be same as shown below.
  1. <!DOCTYPE html>

  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta content="width=device-width, initial-scale=1.0" name="viewport">
  6. <meta content="" name="description">
  7. <meta content="" name="author">
  8. <link href="" rel="shortcut icon">

  9. <title>List of Student</title><!-- Bootstrap core CSS -->
  10. <link href="css/bootstrap.css" rel="stylesheet">
  11. </head>

  12. <body>
  13. <div class="container">
  14. <table class="table table-striped">
  15. <thead>
  16. <tr>
  17. <th>Firstname</th>

  18. <th>Lastname</th>

  19. <th>Email</th>

  20. <th>Phone</th>
  21. </tr>
  22. </thead>

  23. <tbody>
  24. <?php
  25. //set up mysql connection
  26. mysql_connect

    (

    "localhost"

    ,

    "root"

    ,

    ""

    )

    or die

    (

    mysql_error

    (

    )

    )

    ;
  27. //select database
  28. mysql_select_db

    (

    "studentdb"

    )

    or die

    (

    mysql_error

    (

    )

    )

    ;
  29. // Retrieve all the data from the "tblstudent" table
  30. $result

    =

    mysql_query

    (

    "SELECT * FROM tblstudent"

    )

    or die

    (

    mysql_error

    (

    )

    )

    ;
  31. // store the record of the "tblstudent" table into $row

  32. while

    (

    $row

    =

    mysql_fetch_array

    (

    $result

    )

    )

    {
  33. // Print out the contents of the entry
  34. echo

    '<tr>'

    ;
  35. echo

    '<td>'

    .

    $row

    [

    'firstname'

    ]

    .

    '</td>'

    ;
  36. echo

    '<td>'

    .

    $row

    [

    'lastname'

    ]

    .

    '</td>'

    ;
  37. echo

    '<td>'

    .

    $row

    [

    'email'

    ]

    .

    '</td>'

    ;
  38. echo

    '<td>'

    .

    $row

    [

    'phone'

    ]

    .

    '</td>'

    ;
  39. }
  40. ?>

  41. </tbody>

  42. <tbody></tbody>
  43. </table>
  44. </div><!-- /container -->
  45. </body>
  46. </html>

Then the output should look like as shown below.
table3.png


That's it for now folks, thanks for reading.

If you want to see more of my works, new Source Code or Application and Tutorials Just click here.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top