Showing posts with label save. Show all posts
Showing posts with label save. Show all posts

Monday, 24 January 2022

PHP MySQL Inline Editing using Vanilla JavaScript with Bootstrap 5


Do you know Inline Update data is a one type of Web Development feature in which We can change Web page content on the same page without going to another page. This Inline Editing feature which cut the edit data without navigate edit action.

With Inline updation of data feature, The web developers do not design more UI for editing of dynamic MySQL table data.

In this tutorial, we will not use contenteditable attribute but here we will use dark-editable JavaScript library. This dark-editable library is a new rewritten version of the x-editable jQuery plugin or it an alternative of x-editable plugin which has been enables dynamic inline editing of data on the web page and this is purely written in JavaScript.

This dark-editable library is has been support Bootstrap 5 library and moment.js for datetime data handling. If you want to use this dark-editable editable library in your web application project, then you have to required following library in your working folder.

  • Bootstrap 5
  • Propper
  • Moment.js




If you have use dark-editable library then this library has been support following input type for inline editing of data without refresh of web page.

  • text
  • textarea
  • select
  • date
  • datetime
  • password
  • email
  • url
  • tel
  • number
  • range
  • time

With the help of dark-editable JavaScript library we can easily inline edit or update DIV content or tabular data. So for edit data, we have to click on content which we want to edit and after we have done edit then it will send update or edit data requests to PHP via Ajax for perform backend update MySQL data operation. This Inline Edit or In-place Edit is a very nice and required feature in the Web Development.

Let us create an example in which we will implement inline editing or updation of data using dark-editable library and how we can update or edit data at backend using this dark-editable library of in-place editing of data.


PHP MySQL Inline Editing using Vanilla JavaScript


Source Code


MySQL Table


In this tutorial, first we want some dynamice data in our MySQL database. So for create table with some data, we have to run following .sql script which will create tbl_sample table with data.


--
-- Database: `testing`
--

-- --------------------------------------------------------

--
-- Table structure for table `tbl_sample`
--

CREATE TABLE `tbl_sample` (
  `id` int(11) NOT NULL,
  `first_name` varchar(250) NOT NULL,
  `last_name` varchar(250) NOT NULL,
  `gender` enum('Male','Female') NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `tbl_sample`
--

INSERT INTO `tbl_sample` (`id`, `first_name`, `last_name`, `gender`) VALUES
(1, 'John', 'Smith', 'Male'),
(2, 'Peter', 'Parker', 'Male'),
(4, 'Donna', 'Huber', 'Female'),
(5, 'Anastasia', 'Peterson', 'Female'),
(6, 'Ollen', 'Donald', 'Male'),
(10, 'Joseph', 'Stein', 'Male'),
(11, 'Wilson', 'Parker', 'Female'),
(12, 'Lillie', 'Kirst', 'Female'),
(13, 'James', 'Whitchurch', 'Male'),
(14, 'Timothy', 'Brewer', 'Male'),
(16, 'Sally', 'Martin', 'Male'),
(17, 'Allison', 'Pinkston', 'Male'),
(18, 'Karen', 'Davis', 'Female'),
(19, 'Jaclyn', 'Rocco', 'Male'),
(20, 'Pamela', 'Boyter', 'Male'),
(21, 'Anthony', 'Alaniz', 'Male'),
(22, 'Myrtle', 'Stiltner', 'Male'),
(23, 'Gary', 'Hernandez', 'Male'),
(24, 'Fred', 'Smith', 'Male'),
(25, 'Ronald', 'John', 'Male'),
(26, 'Stephen', 'Mohamed', 'Male'),
(28, 'Michael', 'Dyer', 'Male'),
(29, 'Betty', 'Beam', 'Male'),
(30, 'Anny', 'Peterson', 'Female'),
(31, 'Peter', 'Stodola', 'Male'),
(32, 'Rose', 'Johnson', 'Female');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `tbl_sample`
--
ALTER TABLE `tbl_sample`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `tbl_sample`
--
ALTER TABLE `tbl_sample`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=36;


database_connection.php


This file we will use for make MySQL database connection.


<?php

//database_connection.php

$connect = new PDO("mysql:host=localhost; dbname=testing", "root", "");

?>





index.php


In this file we have included Bootstrap 5 library, dark-editable library and Moment.js library link.

In this file first we have fetch data from tbl_sample and display on web page in HTML table format.

For enable dark-editable inline editing of data, first we have to add class attribute and id attribute in place of content.

After this for initialize dark-editable library on any content we have to use DarkEditable() method. So this method will enable inplace editing feature.

Next for send data to PHP script via Ajax, we have to add data-type, data-pk, data-url and data-name attribute. So this attribute value will be send to PHP script via Ajax request.


<?php 

//index.php

include 'database_connection.php';

$query = "
SELECT * FROM tbl_sample 
ORDER BY id DESC
";

$result = $connect->query($query);

?>

<!doctype html>
<html lang="en">
    <head>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <!-- Bootstrap CSS -->
        <link href="library/bootstrap-5/bootstrap.min.css" rel="stylesheet" />
        <script src="library/bootstrap-5/bootstrap.bundle.min.js"></script>
        <script src="library/moment.js"></script>
        <link rel="stylesheet" href="library/dark-editable/dark-editable.css" />
        <script src="library/dark-editable/dark-editable.js"></script>

        <title>Inline Editing with Bootstrap 5 & Vanilla JavaScript using PHP / How to Load Data in Select2 with Bootstrap 5 using Ajax PHP</title>
    </head>
    <body>

        <div class="container">
            <h1 class="mt-5 mb-5 text-center text-primary">Inline Editing using Vanilla JavaScript with PHP MySQL Bootstrap 5</h1>
            <div class="card">
                <div class="card-header">Data</div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-striped table-bordered">
                            <tr>
                                <th>First Name</th>
                                <th>Last Name</th>
                                <th>Gender</th>
                            </tr>
                            <?php 
                            foreach($result as $row)
                            {
                                echo '
                                <tr>
                                    <td><a href="#" class="first_name" id="first_name_'.$row["id"].'" data-type="text" data-pk = "'.$row["id"].'" data-url="process.php" data-name="first_name">'.$row["first_name"].'</a></td>
                                    <td><a href="#" class="last_name" id="last_name_'.$row["id"].'" data-type="text" data-pk="'.$row["id"].'" data-url="process.php" data-name="last_name">'.$row["last_name"].'</a></td>
                                    <td><a href="#" class="gender" id="gender_'.$row["id"].'" data-type="select" data-pk="'.$row["id"].'" data-url="process.php" data-name="gender">'.$row["gender"].'</a></td>
                                </tr>
                                ';
                            }
                            ?>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>

<script>

//For First Name Table Column Data

const first_name = document.getElementsByClassName('first_name');

for(var count = 0; count < first_name.length; count++)
{
    const first_name_data = document.getElementById(first_name[count].getAttribute('id'));

    const first_name_popover = new DarkEditable(first_name_data);
}


//For Last Name Table Column Data

const last_name = document.getElementsByClassName('last_name');

for(var count = 0; count < last_name.length; count++)
{
    const last_name_data = document.getElementById(last_name[count].getAttribute('id'));

    const last_name_popover = new DarkEditable(last_name_data);
}


//For Gender Table column Data

const gender = document.getElementsByClassName('gender');

for(var count = 0; count < gender.length; count++)
{
    const gender_data = document.getElementById(gender[count].getAttribute("id"));

    const gender_popover = new DarkEditable(gender_data, {
        source :[
            {
                value : 'Male',
                text : 'Male'
            },
            {
                value : 'Female',
                text : 'Female'
            }
        ]
    });
}


</script>


process.php


This file has been receive data via Ajax request for update data in MySQL database. This is common dynamic PHP script for all table column data for updation of data.


<?php

//process.php

include 'database_connection.php';

if(isset($_POST["pk"]))
{
	$query = "
	UPDATE tbl_sample 
	SET ".$_POST['name']." = '".$_POST["value"]."' 
	WHERE id = '".$_POST["pk"]."'
	";

	$connect->query($query);

	echo 'done';
}

?>


At the last with the help of this dark-editable library you can implement inline editing of any DIV or table data content without refresh of web page. The one benefits of this library is that when we have make any changes then only it will send ajax request to PHP script.

Additionally Hope you have enjoy this Vanilla JavaScript learning with PHP for live inline editing of data and here you can find complete source of this tutorial also.





Friday, 15 February 2019

Make and Download Zip File using Codeigniter



In this post, We will discuss how can we generate zip file of multiple file, after this save in folder, lastly download generated zip file by using Codeigniter framework. Codeigniter is one of the best PHP framework, which will boost you web development process and there many web developer has been used this framework for their web application development, because it is widely popular PHP framework. It has provide many built in helper and library which will help you to develop your web application very fast. This framework will helps to do many task very easily. So, here also we have one task like how to make zip file in Codeigniter framework.

Codeigniter Zip Encoding Class


If you have use Codeigniter framework, then it is very easy for you to create Zip file. Because Codeigniter has it's own built in Zip library. This class will used for generate Zip archives, and that Zip archives we can download in our local computer also. Now how to initialze this zip class. For this we have to write following code.


$this->load->library('zip');


By using above code will can initialize Codeigniter Zip class, after this we can use different method of this class by calling $this->zip object. Now here we want to generate zip file of selected images from list of images. For this here we will use following method of this zip library.

read_file() - This method will read the content of a file and it will add into Zip.

add_data() - This method will add data to Zip file. In this define path of the file and data of this file.

archive() - This method will write file to the specified directory, in this method we have to define file name with complete path.

download() - This method will download created Zip file in our local computer, in this method we have to define zip file name.

Above all method of Codeigniter Zip class will help us to generate Zip file, save in folder and download in our local computer. Below you can find complete source code of this tutorial.




Make and Download Zip File using Codeigniter



Source Code


Zip_file.php(Controllers)



<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Zip_file extends CI_Controller {
 
 function index()
 {
  $directory = 'download';
  $data["images"] = glob($directory . "/*.jpg");
  $this->load->view('zip_file', $data);
 }

 function download()
 {
  if($this->input->post('images'))
  {
   $this->load->library('zip');
   $images = $this->input->post('images');
   foreach($images as $image)
   {
    $this->zip->read_file($image);
   }
   $this->zip->download(''.time().'.zip');
  }
 }

}

?>





zip_file.php



<html>
<head>
    <title>How to Create ZIP File in CodeIgniter</title>
    
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    
</head>
<body>
 <div class="container box">
  <h3 align="center">How to Create ZIP File in CodeIgniter</h3>
  <br />
  <form method="post" action="<?php echo base_url(); ?>zip_file/download">
  <?php
  foreach($images as $image)
  {
   echo '
   <div class="col-md-2" align="center" style="margin-bottom:24px;">
    <img src="'.base_url().''.$image.'" class="img-thumbnail img-responsive" />
     <br />
    <input type="checkbox" name="images[]" class="select" value="'.$image.'" />
   </div>
   ';
  }
  ?>
  <br />
  <div align="center">
   <input type="submit" name="download" class="btn btn-primary" value="Download" />
  </div>
  </form>
 </div>
</body>
</html>

<script>
$(document).ready(function(){
 $('.select').click(function(){
  if(this.checked)
  {
   $(this).parent().css('border', '5px solid #ff0000');
  }
  else
  {
   $(this).parent().css('border', 'none');
  }
 });
});
</script>