Showing posts with label Angularjs CSV Import and Insert Core PHP Mysql. Show all posts
Showing posts with label Angularjs CSV Import and Insert Core PHP Mysql. Show all posts

Angularjs push list elements to an array

Angularjs push list elements to an array


Hi Guys Today Discussed Angularjs push list elemetns to an array view list form data push array same method used ionic framework. controller data array push element angularjs push list elements to an array follows code.

Angularjs push list elements to an array
Angularjs push list elements to an array

var app = angular.module('angularjs-starter', []);

Controller

app.controller('MainCtrl', function($scope) {
   $scope.submitForm = function () {
       alert($scope.choicesMade);
   };

  $scope.radioValue = {};
   
  $scope.choicesMade = [];
  
  $scope.wordsPresent=['King', 'Garden', 'America', 'Sky', 'Potato', 'Germany', 'Rose', 'Whisky', 'Mumbai', 'Walk'];

  $scope.pushToArray = function(item) {
    if ($scope.choicesMade.indexOf($scope.radioValue[item]) > -1) {
      alert("already in choices");
      return;
    }
    $scope.choicesMade.push($scope.radioValue[item]);
  }

  $scope.removeFromArray = function(item) {
    
    var index = $scope.choicesMade.indexOf($scope.radioValue[item].substring(1));
    if (index > -1) {
      $scope.choicesMade.splice(index, 1);  
    }
    

  }
});
html page

<!DOCTYPE html>
<html ng-app="angularjs-starter">
  
  <head lang="en">
    <meta charset="utf-8">
    <title>Custom Plunker</title>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
    <link rel="stylesheet" href="style.css">
    <script>
      document.write('<base href="' + document.location + '" />');
    </script>
    <script src="app.js"></script>
  </head>
  
  <body ng-controller="MainCtrl">
          <div ng-repeat="item in wordsPresent">
            <b>{{ item }}</b><br>
            <label><input type="radio" name="name_{{item}}" ng-change="pushToArray(item)" ng-model="radioValue[item]" value="{{item}}"/> Yes</label>
           <label><input type="radio" name="name_{{item}}" ng-change="removeFromArray(item)" ng-model="radioValue[item]" value="_{{item}}"/> No</label>
          </div>
           <pre>{{choicesMade}}</pre>
  </body> 

</html>


How to Import and Export CSV Files Using PHP and MySQL

How to Import and Export CSV Files Using PHP and MySQL


Hi Today discussed PHP Server side Scripting Language How to Import and Export CSV Files using PHP and mysql. Database normally insert and update and delete queries and bulk insert it's means csv file same column file data csv file data insert mysql. code follows.


How to Import and Export CSV Files Using PHP and MySQL
How to Import and Export CSV Files Using PHP and MySQL



First Create database and then create table.

CREATE DATABASE employee
Create table

CREATE TABLE employeedetils(
eid VARCHAR(50) UNSIGNED PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
create_date VARCHAR(50)
)
dbconnect

<?php
function getdb(){
$servername = "localhost";
$username = "root";
$password = "";
$db = "employee";

try {
   
    $conn = mysqli_connect($servername, $username, $password, $db);
     //echo "Connected successfully"; 
    }
catch(exception $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
    return $conn;
}
?>
PHP File Import function phpfiles.php

<?php


 if(isset($_POST["Import"])){
  
  $filename=$_FILES["file"]["tmp_name"];  


   if($_FILES["file"]["size"] > 0)
   {
     $file = fopen($filename, "r");
         while (($getData = fgetcsv($file, 10000, ",")) !== FALSE)
          {


            $sql = "INSERT into employeedetils(eid,firstname,lastname,email,create_date)                    values ('".$getData[0]."','".$getData[1]."','".$getData[2]."','".$getData[3]."','".$getData[4]."')";
                   $result = mysqli_query($con, $sql);
    if(!isset($result))
    {
     echo "<script type=\"text/javascript\">
       alert(\"Invalid File:Please Upload CSV File.\");
       window.location = \"index.php\"
        </script>";  
    }
    else {
       echo "<script type=\"text/javascript\">
      alert(\"CSV File has been successfully Imported.\");
      window.location = \"index.php\"
     </script>";
    }
          }
   
          fclose($file); 
   }
 }  


 ?>

View Page design bootstrap lib include

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" crossorigin="anonymous"></script>
View Page


<!DOCTYPE html>
<html lang="en">

<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" crossorigin="anonymous">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" crossorigin="anonymous">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" crossorigin="anonymous"></script>

</head>

<body>
    <div id="wrap">
        <div class="container">
            <div class="row">

                <form class="form-horizontal" action="phpfiles.php" method="post" name="upload_excel" enctype="multipart/form-data">
                    <fieldset>

                        <!-- Form Name -->
                        <legend>Form Name</legend>

                        <!-- File Button -->
                        <div class="form-group">
                            <label class="col-md-4 control-label" for="filebutton">Select File</label>
                            <div class="col-md-4">
                                <input type="file" name="file" id="file" class="input-large">
                            </div>
                        </div>

                        <!-- Button -->
                        <div class="form-group">
                            <label class="col-md-4 control-label" for="singlebutton">Import data</label>
                            <div class="col-md-4">
                                <button type="submit" id="submit" name="Import" class="btn btn-primary button-loading" data-loading-text="Loading...">Import</button>
                            </div>
                        </div>

                    </fieldset>
                </form>

            </div>
            <?php
               getdbrecords();
            ?>
        </div>
    </div>
</body>

</html>
view php records

function getdbrecords(){
    $con = getdb();
    $Sql = "SELECT * FROM employeedetils";
    $result = mysqli_query($con, $Sql);  


    if (mysqli_num_rows($result) > 0) {
     echo "<div class='table-responsive'><table id='myTable' class='table table-striped table-bordered'>
             <thead><tr><th>EMP ID</th>
                          <th>First Name</th>
                          <th>Last Name</th>
                          <th>Email</th>
                          <th>Registration Date</th>
                        </tr></thead><tbody>";


     while($row = mysqli_fetch_assoc($result)) {

         echo "<tr><td>" . $row['eid']."</td>
                   <td>" . $row['firstname']."</td>
                   <td>" . $row['lastname']."</td>
                   <td>" . $row['email']."</td>
                   <td>" . $row['create_date']."</td></tr>";        
     }
    
     echo "</tbody></table></div>";
     
} else {
     echo "you have no records";
}
}

Export MySQL to CSV With PHP

<div>
            <form class="form-horizontal" action="phpfiles.php" method="post" name="upload_excel"   
                      enctype="multipart/form-data">
                  <div class="form-group">
                            <div class="col-md-4 col-md-offset-4">
                                <input type="submit" name="Export" class="btn btn-success" value="export to excel"/>
                            </div>
                   </div>                    
            </form>           
 </div>
Export Functions

if(isset($_POST["Export"])){
   
      header('Content-Type: text/csv; charset=utf-8');  
      header('Content-Disposition: attachment; filename=data.csv');  
      $output = fopen("php://output", "w");  
      fputcsv($output, array('ID', 'First Name', 'Last Name', 'Email', 'Joining Date'));  
      $query = "SELECT * from employeedetils ORDER BY emp_id DESC";  
      $result = mysqli_query($con, $query);  
      while($row = mysqli_fetch_assoc($result))  
      {  
           fputcsv($output, $row);  
      }  
      fclose($output);  
 }  

Angularjs CSV Import and Insert Core PHP Mysql

Angularjs CSV Import and Insert Core PHP Mysql

Hi, Today Discussed Angularjs CSV Import and Insert Core PHP Mysql backend database. Frontend Angularjs CSV or other file Upload  get this formdata backend post and insert mysql database follows Code.
Angularjs CSV Import and Insert Core PHP Mysql
Angularjs CSV Import and Insert Core PHP Mysql

Controller.js

$scope.submit1=function(){

  var s = $scope.fileContent;
  var d = JSON.stringify(s);

             var ds = {'unitname':d,'mode':'csv'}
  var d = JSON.stringify(ds);
console.log(d);
    var postUrl = '../app/api/unit.php';
  $http.post(postUrl,d,{
            transformRequest: angular.identity,
            headers: { 'Content-Type': "application/json" }
        }).success(function(data){
       //  console.log(data.type);
              type=data.type,
              title="UnitCreation",
              message=data.response
 toaster.pop(type, title, message);
       
        });


}
CSV File Reader
Directive.js

  
angular.module('MetronicApp').directive('fileReader', function() {
  return {
    scope: {
      fileReader:"="
    },
    link: function(scope, element) {
      $(element).on('change', function(changeEvent) {
        var files = changeEvent.target.files;
        if (files.length) {
          var r = new FileReader();
          r.onload = function(e) {
              var contents = e.target.result;
              scope.$apply(function () {
                scope.fileReader = contents;
                //console.log(scope.fileReader);
               // var s = JSON.stringify(contents)
              // console.log(contents);
              });
          };
          
          r.readAsText(files[0]);

        }

      });
    }
  };
});
View.html

<form role="form" id="form_sample_2" id="container" data-ng-submit="submit1()">
   

     <input type="file" file-reader="fileContent" ng-model="fileContent" class="btn btn-primary" />
    
    <input type="hidden" ng-model="fileContent">
     <button class="btn sbold blue" name="submit" type="submit">Upload</button>

</form>