Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Using Jquery DataGrid with PHP and MySql as a Datagrid Part 2

Jquery Datatables has lots of options that allows fine control over the display of the table. The options can be provided as parameters at the time of initialization. Some of the options are
?

  
01
02
03
04
05
06
07
08
09
10
11
<script type="text/javascript">
$(document).ready(function(e) {
    $('.data-grid').dataTable(
     {
          "paging":   false,
          "ordering": false,
          "info":     false
     });
});
</script>

  
<script type="text/javascript">
$(document).ready(function(e) {
    $('.data-grid').dataTable(
     {
          "paging":   false,
          "ordering": false,
          "info":     false

     });
});
</script> 
 
 
To change the number of records displayed in a table, change the "pageLength" option. For example 
 
"pageLength": 50 
 
A full list of options is available at https://datatables.net/reference/option/ 
 
 

Using Jquery DataGrid with PHP and MySql as a Datagrid Part 1

Datagrid is a major requirement in any Database application. Not many free datagrids are available for PHP. Those that are available lack in features. In this series we are going to use Jquery DataTable along with PHP and MySql to create a Sortable, Filterable, Customizable datagrid.

 Simple MySql Recordset Display

Step 1: First Lets Add All the Required Libraries to the Page. 
?
1
2
3
<script src="https://code.jquery.com/jquery-1.12.4.js" type="text/javascript"></script>
<script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css" rel="stylesheet" />

Step 2: Get the data from the database and display it in an Html Table

?

  
1
2
3
<table class="data-grid">
<thead>
<tr>

  
<table class="data-grid">
<thead>
<tr>

?

  
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
// Define Database Connection Information
define("HOSTNAME","localhost");
define("USERNAME","root");
define("PASS","");
define("DB","world");
// Make a Connection
$connection=new mysqli(HOSTNAME,USERNAME,PASS,DB);
if(!$connection){
echo "Sorry No Connection";
exit;  
}
// Get Column Names of the Table
$results=$connection->query("SHOW COLUMNS from city");
// Print the Table
while($headers=mysqli_fetch_array($results,MYSQLI_NUM)){
echo "<th>".$headers[0]."</th>";
}
echo "</tr>";
echo "</thead><tbody>";
// Get Table Data from Database
$output=$connection->query("SELECT * from city");
//Output the Data
while($rs=mysqli_fetch_array($output,MYSQLI_NUM)){
echo "<tr>";
for($i=0;$i<count($rs);$i++){
    echo "<td>".$rs[$i]."</td>";
}
echo "</tr>";
}
?>
</tbody>
</tr>
</table>

  
<?php
// Define Database Connection Information
define("HOSTNAME","localhost");
define("USERNAME","root");
define("PASS","");
define("DB","world");

// Make a Connection
$connection=new mysqli(HOSTNAME,USERNAME,PASS,DB);

if(!$connection){
echo "Sorry No Connection";
exit;   
}
// Get Column Names of the Table
$results=$connection->query("SHOW COLUMNS from city");
// Print the Table
while($headers=mysqli_fetch_array($results,MYSQLI_NUM)){
echo "<th>".$headers[0]."</th>";

}
echo "</tr>";
echo "</thead><tbody>";
// Get Table Data from Database
$output=$connection->query("SELECT * from city");

//Output the Data
while($rs=mysqli_fetch_array($output,MYSQLI_NUM)){
echo "<tr>";

for($i=0;$i<count($rs);$i++){
    echo "<td>".$rs[$i]."</td>";
}
echo "</tr>";
}
?>
</tbody>
</tr>
</table>

Step 3: Finally initialize the datatable .

4
?

  
1
2
3
4
5
<script type="text/javascript">
$(document).ready(function(e) {
    $('.data-grid').dataTable();
});
</script>

  
<script type="text/javascript">
$(document).ready(function(e) {
    $('.data-grid').dataTable();
});
</script>

Save the File in your virtual directory. Load it in your browser and you should see


The Source File can be downloaded from here.


In the next installment we will look into loading data into the Table using Ajax.

CDN Link for All Jquery Ui Themes


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!-- CDN COPIES OF STANDARD JQUERY THEMES (all of these are available in v1.8.16) -->
<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" type="text/css" rel="stylesheet" /> -->
<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/black-tie/jquery-ui.css" type="text/css" rel="stylesheet" /> -->
<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/blitzer/jquery-ui.css" type="text/css" rel="stylesheet" /> -->
<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/cupertino/jquery-ui.css" type="text/css" rel="stylesheet" /> -->

<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/dark-hive/jquery-ui.css" type="text/css" rel="stylesheet" /> -->
<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/dot-luv/jquery-ui.css" type="text/css" rel="stylesheet" /> -->
<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/eggplant/jquery-ui.css" type="text/css" rel="stylesheet" /> -->
<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/excite-bike/jquery-ui.css" type="text/css" rel="stylesheet" /> -->
<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/flick/jquery-ui.css" type="text/css" rel="stylesheet" /> -->
<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/hot-sneaks/jquery-ui.css" type="text/css" rel="stylesheet" /> -->
<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/humanity/jquery-ui.css" type="text/css" rel="stylesheet" /> -->
<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/le-frog/jquery-ui.css" type="text/css" rel="stylesheet" /> -->
<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/mint-choc/jquery-ui.css" type="text/css" rel="stylesheet" /> -->

<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/overcast/jquery-ui.css" type="text/css" rel="stylesheet" /> -->
<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/pepper-grinder/jquery-ui.css" type="text/css" rel="stylesheet" /> -->
<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/redmond/jquery-ui.css" type="text/css" rel="stylesheet" /> -->
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/smoothness/jquery-ui.css" type="text/css" rel="stylesheet" />
<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/south-street/jquery-ui.css" type="text/css" rel="stylesheet" /> -->
<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/start/jquery-ui.css" type="text/css" rel="stylesheet" /> -->
<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/sunny/jquery-ui.css" type="text/css" rel="stylesheet" /> -->
<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/swanky-purse/jquery-ui.css" type="text/css" rel="stylesheet" /> -->
<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/trontastic/jquery-ui.css" type="text/css" rel="stylesheet" /> -->

<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/ui-darkness/jquery-ui.css" type="text/css" rel="stylesheet" /> -->
<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/ui-lightness/jquery-ui.css" type="text/css" rel="stylesheet" /> -->
<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/vader/jquery-ui.css" type="text/css" rel="stylesheet" /> -->

Jquery Datatables + Table using Mysql

<script type='text/javascript' src='https://code.jquery.com/jquery-2.1.4.min.js'></script>
<Script type="text/javascript" src='//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js'>
</script>

<link rel='stylesheet' href='//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css' />

<script src='//cdn.datatables.net/plug-ins/1.10.7/integration/jqueryui/dataTables.jqueryui.js'></script>
    <link rel='stylesheet' href='http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/themes/cupertino/jquery-ui.css' />
    <link rel='stylesheet' href='//cdn.datatables.net/plug-ins/1.10.7/integration/jqueryui/dataTables.jqueryui.css' />

<style>

body { font-size: 140%; }
</style>

  
<script>

  

$(document).ready(function() {
    $('#example').dataTable( {
        //"processing": true,
        //"serverSide": true,
        //"ajax": "jsonencode.php"
    } );
} );

</script>

</head>



</head>
<body>
<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>ID</th>
                <th>Vehcile ID</th>
                <th>KM Driven</th>
                <th>Date of Reading</th>
                <th>Date of Entry</th>
                <th>Entry User</th>
            </tr>
        </thead>
<script type='text/javascript' src='https://code.jquery.com/jquery-2.1.4.min.js'></script>
<Script type="text/javascript" src='//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js'>
</script>

<link rel='stylesheet' href='//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css' />

<script src='//cdn.datatables.net/plug-ins/1.10.7/integration/jqueryui/dataTables.jqueryui.js'></script>
    <link rel='stylesheet' href='http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/themes/cupertino/jquery-ui.css' />
    <link rel='stylesheet' href='//cdn.datatables.net/plug-ins/1.10.7/integration/jqueryui/dataTables.jqueryui.css' />

<style>

body { font-size: 140%; }
</style>

   
<script>

   

$(document).ready(function() {
    $('#example').dataTable( {
     
    } );
} );

</script>

</head>



</head>
<body>
<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>ID</th>
                <th>Vehcile ID</th>
                <th>KM Driven</th>
                <th>Date of Reading</th>
                <th>Date of Entry</th>
                <th>Entry User</th>
            </tr>
        </thead>
 <?php
$db = new mysqli('localhost', 'user', 'password', 'database');

if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}

$sql="select * from tbl_readings";

if(!$result = $db->query($sql)){
    die('There was an error running the query [' . $db->error . ']');
}
$json = array( );

while( $row = $result->fetch_assoc() ) {
   echo "<tr>";
   echo "<td> ".$row['id']."</td>";
   echo "<td> ".$row['vehicle_id']."</td>";
   echo "<td> ".$row['km_driven']."</td>";
   echo "<td> ".$row['dt_of_reading']."</td>";
   echo "<td> ".$row['dt_of_entry']."</td>";
   echo "<td> ".$row['entry_user']."</td>";
   echo "</tr>";
}

?>
        <tfoot>
            <tr>
                                <th>ID</th>
                <th>Vehcile ID</th>
                <th>KM Driven</th>
                <th>Date of Reading</th>
                <th>Date of Entry</th>
                <th>Entry User</th>
            </tr>
        </tfoot>
    </table>
</body>
</html>
        <tfoot>
            <tr>
                                <th>ID</th>
                <th>Vehcile ID</th>
                <th>KM Driven</th>
                <th>Date of Reading</th>
                <th>Date of Entry</th>
                <th>Entry User</th>
            </tr>
        </tfoot>
    </table>
</body>
</html>

Javascript: Remove Spaces

<script type="text/javascript">
function removeSpaces(){
var txt=document.getElementById("t1").value;


var someText = txt.replace(/(\r\n|\n|\r)/gm," ");
someText = someText.replace(/\s+/g," ");
document.getElementById("t2").value=someText;
}

</script>

Unleashing the Power of NumPy Arrays: A Guide for Data Wranglers

Ever feel like wrestling with data in Python using clunky loops? NumPy comes to the rescue! This blog post will unveil the magic of NumPy a...