PHP: Showing MySQL Data through Bootstrap Model Box and JSON

Showing MySQL Data through Bootstrap Model Box

1. Download Bootstarp API and keep in your directory


2. Create a PHP file with Bootstrap JS and CSS file [commonCSS.php]

<script type="text/javascript" src="bootstrap-3.3.5-dist/js/ajax.js"></script>
<link rel="stylesheet" type="text/css" href="bootstrap-3.3.5-dist/css/bootstrap.min.css"/>
<script type="text/javascript" src="bootstrap-3.3.5-dist/js/bootstrap.min.js"></script>

3. Now Create another PHP file for showing Menu on all pages [commonMenu.php]

<nav class="navbar navbar-inverse">
  <div class="container-fluid">
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-2">
      <ul class="nav navbar-nav">
        <li><a href="index.php"> <img src="images/softaid_logo.png" width="490" height="99" alt="logo" />  </a></li>
                <li><a href="index.php">  <button  class="btn btn-success"> Home</button> </a></li>
                <li><a href="list.php">  <button  class="btn btn-warning"> List</button> </a></li>
                <li><a href="about.php">  <button  class="btn btn-danger"> About</button> </a></li>

               <li><a href="arr_loop.php">  <button  class="btn btn-info"> Test</button> </a></li>
      <ul class="nav navbar-nav navbar-right">
            <li class="dropdown">
                <a class="dropdown-toggle" data-toggle="dropdown" href="#"><span class="glyphicon glyphicon-user"></span> User
                    <span class="caret"></span></a>
                <ul class="dropdown-menu">
                    <li><a href="login.php">Logout</a></li>


4. Now Create another PHP file for making connection with database [commonDB.php]

$host = 'localhost';
$user = 'root';
$pass = '';
$dbname = 'shop';
$db = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

5. Finally Create PHP file [list.php] for showing data

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>



<!--<a href="index.php">  <button  class="btn btn-success"> Home</button> </a>
<a href="list.php"><button  class="btn btn-warning"> List</button> </a>
<a href="about.php"><button  class="btn btn-danger"> About</button> </a> -->

<table style="margin: auto; width: 60%" class="table table-hover">
    $sql="SELECT * FROM product ORDER by pid";

    while($row = $r->fetch())
    echo "<tr><td>".$row[0]."</td><td>".$row[1]."</td><td>".'&#8377;'.$row[2]."</td><td>
	<button class='btn btn-info detail' data-toggle='modal' data-target='#myModal'  data-id='".json_encode($row)."'>   Detail</button>
catch(PDOException $e)
    echo $e->getMessage();


<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      <div class="modal-body">
      <table class="table table-responsive">

                            <td id="pid"></td>
                            <td id="name"></td>
                            <td id="price"></td>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>

<!-- <script type = "text/javascript"  src = ""></script> -->

<script type = "text/javascript" language = "javascript">


			var id = $(this).attr('data-id');
			var result = JSON.parse(id);




6. Here is your output


7. click on Detail Button


Posted in PHP