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

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      Udhayakumar G K 5 years ago

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

    • profile image

      Babudhavam 5 years ago

      Hi da.........

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

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

    • profile image

      K.Latha 5 years ago

      Abstract is good and really useful.

    • profile image

      Arun kumar MCA 5 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

      Sadiq 5 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

      Ben Siemens J 5 years ago

      Excellent. I was exactly Looking for this. Thanks.

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

    • profile image

      Farahee 5 years ago

      Where is config file code ?

    • DivaICS profile image
      Author

      DivaICS 5 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

      MdcLo_1009 4 years ago

      Excelente aportacion! =)

    • DivaICS profile image
      Author

      DivaICS 4 years ago from Chennai

      Thanks MdcLo_1009

    • profile image

      Francisfrm 4 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

    • profile image

      Ali Arshad 4 years ago

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

    • DivaICS profile image
      Author

      DivaICS 4 years ago from Chennai

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

    • profile image

      bhaskarecraft 4 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 4 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 4 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 4 years ago from Chennai

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

    • profile image

      murali 4 years ago

      finally people like u wins

    • profile image

      Jean 4 years ago

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

      From Italy

    • DivaICS profile image
      Author

      DivaICS 4 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

      Vern Mollicone 3 years ago

      [quote]8192 characters left.[/quote]

      Seriously tons of awesome information!.

      Truly tons of beneficial info.

    • DivaICS profile image
      Author

      DivaICS 3 years ago from Chennai

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

    • Somashekhark profile image

      Somashekhark 3 years ago from Bangalore, India

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

    • DivaICS profile image
      Author

      DivaICS 3 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..

    • profile image

      somashekhark 3 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

    • profile image

      shraddha 3 years ago

      Excellent scripting with no errors

    • DivaICS profile image
      Author

      DivaICS 3 years ago from Chennai

      @shraddha: Thanks a lot...

    Click to Rate This Article