ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming

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

Comments

    0 of 8192 characters used
    Post Comment

    • DivaICS profile image
      Author

      DivaICS 4 years ago from Chennai

      @shraddha: Thanks a lot...

    • profile image

      shraddha 4 years ago

      Excellent scripting with no errors

    • profile image

      somashekhark 4 years ago

      Notice: Undefined variable: result in C:\xampp\htdocs\grid1\index.php on line 24

      Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\xampp\htdocs\grid1\index.php on line 24

    • DivaICS profile image
      Author

      DivaICS 4 years ago from Chennai

      Hi soma: thanks for your comment..sorry somu, i can't able to post the code..becoz it wont allow html tags..

    • Somashekhark profile image

      Somashekhark 4 years ago from Bangalore, India

      how to add list box with values or combo box with table values

    • DivaICS profile image
      Author

      DivaICS 4 years ago from Chennai

      @Vern: thanks a lot..All of ur support it happens..

    • profile image

      Vern Mollicone 4 years ago

      [quote]8192 characters left.[/quote]

      Seriously tons of awesome information!.

      Truly tons of beneficial info.

    • DivaICS profile image
      Author

      DivaICS 5 years ago from Chennai

      hi jean...i have already make a check point for every separate program..

      ex..add a record, delete a record, page design etc..

      plese let me know for any future issues.

    • profile image

      Jean 5 years ago

      Can you help me to divide by file your code ? I am not espert of HTML or PHP, Thanks

      From Italy

    • profile image

      murali 5 years ago

      finally people like u wins

    • DivaICS profile image
      Author

      DivaICS 5 years ago from Chennai

      its k no issues....u can use java script alert, it's a easy nut....

    • profile image

      bhaskarecraft 5 years ago

      thanks lott i done it and how to create an alert box when i am deleting a record from the grid view

    • DivaICS profile image
      Author

      DivaICS 5 years ago from Chennai

      k thanks bhaskare....let me explain the code..

      step 1:create config .php file with your database details...

      step 2:save the html file separately as add,delete etc.

      step 3:save the main html file as index.php.

      please let me know for any issues...

    • profile image

      bhaskarecraft 5 years ago

      hi thanks for the code but i am not able to do that can u plz explain me step by step bec i am beganner in this codings plz don't mind bro

    • DivaICS profile image
      Author

      DivaICS 5 years ago from Chennai

      Thanks ali...its going on with advanced features...

    • profile image

      Ali Arshad 5 years ago

      Its GOod but if you make some search field in it its will Rock More

    • profile image

      Francisfrm 6 years ago

      Cela fait un bon moment maintenant, j’etais a l'aguet d’information visant la bourse en ligne.

      Bien plus qu’une maniere quelconque pour gagner de l’argent, je pense que le monde des finances placement et indice boursier ma invariablement plu. Comment s’y prendre etait la premiere question qui me passais par la tete et cela me semblais toujours impossible n’ayant jamais été veritablement fort en mathematique, je me sentais pas jongler avec mes sous sur une chose de risque mais aussi et surtout de mal maitrise. Du coup je n’avais jamais tenté de peur de me faire arnaquer par le premier escroc du coin.

      Etant un gros joueur dans l’ame, j’ai essaye diverse chose du style comme le Casino en ligne astuce dite de la Martingual), d'autre choses comme le P.T.C et P.T.R et l'unique chose que je puisse vous avouer c’est que je me suis fait totalement pigeonner par les belles paroles de sois-disant experts dans le domaine qui ont du gagner un peu d’argent sur mes côtes. Cela aura eu au moin la vertue d eme servir de lecon ...

      Beaucoup d’arnaque, astuce méthode et/ou technique qui pourrais vous couter cher si jamais vous ne faisiez pas assez attention ou n'etes pas assez mefiant. Depuis cette experience et diverse conversation, je deconseille veritablement ce type de pratique et je n’en est assimilé les ficelles que bien trop tardivement.

      Apres beaucoup de recherche, je suis quand même sur quelque chose de valable à cette adresse : http://lesoptionsbinaires.blogspot.fr/

      Tout y est tres bien expliqué de maniere simple et concise et permet de mieux assimiler et developper ce qui peu marcher ou ce qui est fait pour vous truander …

      Amicalement ;)

      Tags : methode argent, bourse en ligne, option binaire

    • DivaICS profile image
      Author

      DivaICS 6 years ago from Chennai

      Thanks MdcLo_1009

    • profile image

      MdcLo_1009 6 years ago

      Excelente aportacion! =)

    • DivaICS profile image
      Author

      DivaICS 6 years ago from Chennai

      hi farahee,

      Just copy the below coding to a file and saved as config.php,...change according to your database...thank you

      $conn = mysql_connect("localhost","USERNAME","PASSWORD") or die("cannot connected");

      mysql_select_db("DATABASE NAME",$conn);

    • profile image

      Farahee 6 years ago

      Where is config file code ?

    • profile image

      Ben Siemens J 6 years ago

      Excellent. I was exactly Looking for this. Thanks.

      Pretty Good. Nalla varuveenga thambi......

    • profile image

      Sadiq 6 years ago

      It is really a good Combination on these Both.. It gives a new idea to take it forward !! thanks for that.. looking more on it !!!!

    • profile image

      Arun kumar MCA 6 years ago

      Hi Diva..

      Thanks for sharing... i have been searching the above code for the past 2 months.. finally i got it here.. it helped me to complete my project.. i m expecting more innovative ideas from u ... thanks again...

      All s well

    • profile image

      K.Latha 6 years ago

      Abstract is good and really useful.

    • profile image

      Babudhavam 6 years ago

      Hi da.........

      Really good thought....keep it.....

      all the best.........

    • profile image

      Udhayakumar G K 6 years ago

      That's awesome! I got the key what i m exactly looking for. Thanks Men.

    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://hubpages.com/privacy-policy#gdpr"

    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)