ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

PHP and MYSQL DataGrid

Updated on March 27, 2012

DataGrid In PHP

Hi All,

I have created Hub for Php & Mysql datagrid, it is very rare to get combination of these both with Pagination you can download with free of cost.

I have done researched from past one month with this combination, i have got few programs but the function are abstract , to retrieve this function we need to pay.It is not free of cost, we can get only by using Master / Visa cards to retrieve this function.

I have developed this , as a quick reference with this combination hope it will be useful for all.Here we can do come impmentation for Add /Delete /Modify from the database... let me know if you need any so that i can also help you out.

Html page to add a New Record:

<html>
<head>
<title>Add Data</title>
</head>

<body>
<a href="index.php"><font color="#F4A460"><h4>Home</a>
<br/><br/>
<form action="add.php" method="post" name="form1">
<table width="25%" border="0">
<tr>
<td>Role_ID</td>
<td><input type="text" name="Role_ID"></td>
</tr>
<tr>
<td>Role_Name</td>
<td><input type="text" name="Role_Name"></td>
</tr>
<tr>
<td>Created_BY</td>
<td><input type="text" name="Created_BY"></td>
</tr>
<tr>
<td>Created_Datetime</td>
<td><input type="text" name="Created_Datetime"></td>
</tr>
<tr>
<td>Updated_By</td>
<td><input type="text" name="Updated_By"></td>
</tr>
<tr>
<td>Updated_Datetime</td>
<td><input type="text" name="Updated_Datetime"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit" value="Add"></td>
</tr>

</table>

</form>
</body>
</html>

<html>
<head>
<title>Add Data</title>
</head>

<body>

<?php

include_once("config.php");

if(isset($_POST['Submit']))
{

$Role_ID=$_POST['Role_ID'];
$Role_Name=$_POST['Role_Name'];
$Created_BY=$_POST['Created_BY'];
$Created_Datetime=$_POST['Created_Datetime'];
$Updated_By=$_POST['Updated_By'];
$Updated_Datetime=$_POST['Updated_Datetime'];


if(empty($Role_ID) || empty($Role_Name ) || empty($Created_BY) || empty($Created_Datetime) || empty($Updated_By) || empty($Updated_Datetime))
{

if(empty($Role_ID))
{
echo "<font color='red'>Role_ID field is empty.</font><br/>";
}

if(empty($Role_Name))
{
echo "<font color='red'>Role_Name field is empty.</font><br/>";
}

if(empty($Created_BY))
{
echo "<font color='red'>Created_BY field is empty.</font><br/>";
}
if(empty($Created_Datetime))
{
echo "<font color='red'>Created_Datetime field is empty.</font><br/>";
}

if(empty($Updated_By))
{
echo "<font color='red'>Updated_By field is empty.</font><br/>";
}

if(empty($Updated_Datetime))
{
echo "<font color='red'>Updated_Datetime field is empty.</font><br/>";
}

echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
}
else
{

$result=mysql_query("INSERT INTO role(Role_ID,Role_Name,Created_BY,Created_Datetime,Updated_By,Updated_Datetime) VALUES('$Role_ID','$Role_Name','$Created_BY','$Created_Datetime','$Updated_By','$Updated_Datetime')");


echo "<font color='#F4A460'><h4>Data added successfully.";
echo "<br/><a href='index.php'><font color='#F4A460'><h4>View Record</a>";
}
}
?>

</body>
</html>

Delete a Record:

<?php

include("config.php");
$id = $_GET['id'];
$result=mysql_query("DELETE FROM role where id=$id");
header("Location:index.php");
?>

Edit a record:

<?php

include_once("config.php");

if(isset($_POST['update']))
{

$id = $_POST['id'];

$Role_ID=$_POST['Role_ID'];
$Role_Name=$_POST['Role_Name'];
$Created_BY=$_POST['Created_BY'];
$Created_Datetime=$_POST['Created_Datetime'];
$Updated_By=$_POST['Updated_By'];
$Updated_Datetime=$_POST['Updated_Datetime'];


if(empty($Role_ID) || empty($Role_Name ) || empty($Created_BY) || empty($Created_Datetime) || empty($Updated_By) || empty($Updated_Datetime))
{

if(empty($Role_ID))
{
echo "<font color='red'>Name field is empty.</font><br/>";
}

if(empty($Role_Name))
{
echo "<font color='red'>Age field is empty.</font><br/>";
}

if(empty($Created_BY))
{
echo "<font color='red'>Email field is empty.</font><br/>";
}
if(empty($Created_Datetime))
{
echo "<font color='red'>Name field is empty.</font><br/>";
}

if(empty($Updated_By))
{
echo "<font color='red'>Age field is empty.</font><br/>";
}

if(empty($Updated_Datetime))
{
echo "<font color='red'>Email field is empty.</font><br/>";
}
}
else
{

$result=mysql_query("UPDATE role SET Role_ID='$Role_ID',Role_Name='$Role_Name',Created_BY='$Created_BY',Created_Datetime='$Created_Datetime',Updated_By='$Updated_By',Updated_Datetime='$Updated_Datetime' WHERE id=$id");


header("Location: index.php");
}
}
?>
<?php

$id = $_GET['id'];


$result=mysql_query("select * from role where id=$id");

while($res=mysql_fetch_array($result))
{
$Role_ID = $res['Role_ID'];
$Role_Name = $res['Role_Name'];
$Created_BY = $res['Created_BY'];
$Created_Datetime = $res['Created_Datetime'];
$Updated_By = $res['Updated_By'];
$Updated_Datetime = $res['Updated_Datetime'];
}
?>
<html>
<title>Edit Data</title>
<body>
<a href="index.php">Home</a>
<br/><br/>
<form name="form1" method="post" action="edit.php">
<table border="0">
<tr>
<td>Role_ID</td>
<td>
<input type="text" name="Role_ID" value=<?php echo $Role_ID;?>> </td>
</tr>
<tr>
<td>Role_Name</td>
<td>
<input type="text" name="Role_Name" value=<?php echo $Role_Name;?>> </td>
</tr>
<tr>
<td>Created_BY</td>
<td>
<input type="text" name="Created_BY" value=<?php echo $Created_BY;?>> </td>
</tr>
<tr>
<td>Created_Datetime</td>
<td>
<input type="text" name="Created_Datetime" value=<?php echo $Created_Datetime;?>> </td>
</tr>
<tr>
<td>Updated_By</td>
<td>
<input type="text" name="Updated_By" value=<?php echo $Updated_By;?>> </td>
</tr>
<tr>
<td>Updated_Datetime</td>
<td>
<input type="text" name="Updated_Datetime" value=<?php echo $Updated_Datetime;?>> </td>
</tr>
<tr>
<td><input type="hidden" name="id" value=<?php echo $_GET['id'];?>> </td>
<td><input type="submit" name="update" value="Update"></td>
</tr>
</table>
</form>


</body>


</html>

Index Page with paging:

<?php

include_once('config.php');


$sql_statement = 'SELECT * FROM role ORDER BY id DESC';


$num_Array = mysql_query($sql_statement);

$total_records = mysql_num_rows($num_Array);

if(isset($_GET['page']))
$page = $_GET['page'];
else
$page = 1;

$offset = 3;


if ($page){
$from = ($page * $offset) - $offset;
}else{

$from = 0;
}

$sql_statement = 'SELECT * FROM role ORDER BY Role_Id ASC LIMIT ' . $from . ',' . $offset;

//So, here is the result array which will have all the rows to display on to page
$result = mysql_query($sql_statement);


?>


<html>
<head>
<title>Homepage</title>

</head>

<body>
<a href="add.html"><font color="#F4A460"><h4>+Add New Data</a><br/><br/>

<table>
<?php
echo "<table width='80%' border=2>";

echo "<tr bgcolor='#e2e0cb'>";
echo "<td align='center'>Role_ID</td>";
echo "<td align='center'>Role_Name</td>";
echo "<td align='center'>Created_BY</td>";
echo "<td align='center'>Created_Datetime</td>";
echo "<td align='center'>Updated_By</td>";
echo "<td align='center'>Updated_Datetime</td>";
echo "<td align='center'>Update</td>";
echo "</tr>";

while ($res = mysql_fetch_array($result)) {



echo "<tr bgcolor='#FFFFF0' >";
echo "<td>".$res['Role_ID']."</td>";
echo "<td>".$res['Role_Name']."</td>";
echo "<td>".$res['Created_BY']."</td>";
echo "<td>".$res['Created_Datetime']."</td>";
echo "<td>".$res['Updated_By']."</td>";
echo "<td>".$res['Updated_Datetime']."</td>";
echo "<td><a href=\"edit.php?id=$res[id]\">Edit</a> | <a href=\"delete.php?id=$res[id]\"><img src='index2.gif' height=15 width=15 title='Delete record' style='cursor:pointer; vertical-align: middle;'></a></td>";

}
?>
<tr>
<td align="center" colspan="8" class="white">
<?PHP
doPages($offset, 'index.php', '', $total_records);
?>
</td>
</tr>

</table>


<?php
echo "</table>";
?>
</body>
</html>
<?php
function check_integer($which) {
if(isset($_REQUEST[$which])){
if (intval($_REQUEST[$which])>0) {

return intval($_REQUEST[$which]);
} else {
return false;
}
}
return false;
}

function get_current_page() {
if(($var=check_integer('page'))) {
return $var;
} else {
//return 1, if it wasn't set before, page=1
return 1;
}
}

function doPages($page_size, $thepage, $query_string, $total=0) {

$index_limit = 4;

$query='';

if(strlen($query_string)>0){
$query = "&amp;".$query_string;
}

$current = get_current_page();

$total_pages=ceil($total/$page_size);
$start=max($current-intval($index_limit/2), 1);
$end=$start+$index_limit-1;

echo '<div class="paging">';

if($current==1) {
echo '<span class="prn">&lt; Previous</span>&nbsp;';
} else {
$i = $current-1;
echo '<a href="'.$thepage.'?page='.$i.$query.'" class="prn" rel="nofollow" title="go to page '.$i.'">&lt; Previous</a>&nbsp;';
echo '<span class="prn">...</span>&nbsp;';
}

if($start > 1) {
$i = 1;
echo '<a href="'.$thepage.'?page='.$i.$query.'" title="go to page '.$i.'">'.$i.'</a>&nbsp;';
}

for ($i = $start; $i <= $end && $i <= $total_pages; $i++){
if($i==$current) {
echo '<span>'.$i.'</span>&nbsp;';
} else {
echo '<a href="'.$thepage.'?page='.$i.$query.'" title="go to page '.$i.'">'.$i.'</a>&nbsp;';
}
}

if($total_pages > $end){
$i = $total_pages;
echo '<a href="'.$thepage.'?page='.$i.$query.'" title="go to page '.$i.'">'.$i.'</a>&nbsp;';
}

if($current < $total_pages) {
$i = $current+1;
echo '<span class="prn">...</span>&nbsp;';
echo '<a href="'.$thepage.'?page='.$i.$query.'" class="prn" rel="nofollow" title="go to page '.$i.'">Next &gt;</a>&nbsp;';
} else {
echo '<span class="prn">Next &gt;</span>&nbsp;';
}


if ($total != 0){
//prints the total result count just below the paging
echo '<p id="total_count"><font color="#F4A460"<h4>(Total '.$total.' Records)</h></p></div>';
}

}

?>


Table:

create table Role( id int(11) NOT NULL auto_increment,Role_ID int NOT NULL,Role_Name varchar(100) NOT NULL,Created_BY varchar(100) NOT NULL,Created_Datetime datetime NOT NULL,Updated_By varchar(100),Updated_Datetime datetime,PRIMARY KEY (id));

NOTE: select an image for Delete option.




DataGrid

working

This website uses cookies

As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://corp.maven.io/privacy-policy

Show Details
Necessary
HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
LoginThis is necessary to sign in to the HubPages Service.
Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
AkismetThis is used to detect comment spam. (Privacy Policy)
HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
Features
Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
MavenThis supports the Maven widget and search functionality. (Privacy Policy)
Marketing
Google AdSenseThis is an ad network. (Privacy Policy)
Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
Index ExchangeThis is an ad network. (Privacy Policy)
SovrnThis is an ad network. (Privacy Policy)
Facebook AdsThis is an ad network. (Privacy Policy)
Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
AppNexusThis is an ad network. (Privacy Policy)
OpenxThis is an ad network. (Privacy Policy)
Rubicon ProjectThis is an ad network. (Privacy Policy)
TripleLiftThis is an ad network. (Privacy Policy)
Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
Statistics
Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
ClickscoThis is a data management platform studying reader behavior (Privacy Policy)