Thursday, January 20, 2022

CodeIgniter CRUD example with 4 fields with bootstrap

C:\xampp\htdocs\newci2\application\config\config.php
$config['base_url'] = 'http://localhost/newci2/';


C:\xampp\htdocs\newci2\application\config\autoload.php
$autoload['libraries'] = array('database', 'session', 'form_validation');
$autoload['helper'] = array('url');


C:\xampp\htdocs\newci2\application\config\database.php
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'crud',

Database code :-

-- phpMyAdmin SQL Dump
-- version 4.9.2
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jan 20, 2022 at 10:13 AM
-- Server version: 10.4.11-MariaDB
-- PHP Version: 7.2.26
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `crud`
--
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `fname` varchar(50) NOT NULL,
  `lname` varchar(50) NOT NULL,
  `username` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  `status` tinyint(1) NOT NULL,
  `created_at` datetime NOT NULL,
  `gender` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;


C:\xampp\htdocs\newci2\application\controllers\User.php
<?php
class User extends CI_Controller
{
function index(){
$this->load->model('User_model');
$users = $this->User_model->all();
$data = array();
$data['users'] = $users;
$this->load->view('list', $data);
}
    function create(){
    //$this->load->view('create');
    $this->load->model('User_model');
$this->form_validation->set_rules('fname','First name','required');
$this->form_validation->set_rules('username','Username','required');
$this->form_validation->set_rules('password','Password','required');
if ($this->form_validation->run() == false) {
$this->load->view('create');
}else{
$formArray = array();
$formArray['fname']=$this->input->post('fname');
$formArray['lname']=$this->input->post('lname');
$formArray['username']=$this->input->post('username');
$formArray['password']=$this->input->post('password');
$formArray['gender']=$this->input->post('gender');
$formArray['status']= 1;
$formArray['created_at']=date('Y-m-d H:i:s');
$this->User_model->create($formArray);
$this->session->set_flashdata('success','Record inserted Successfully..');
redirect(base_url().'index.php/user/index');
}
    }
    function edit($id){
    $this->load->model('User_model');
    $user = $this->User_model->get_user($id);
    $data = array();
    $data['user']= $user;
    //$this->load->view('edit',$data);
    $this->form_validation->set_rules('fname','First Name','required');
    $this->form_validation->set_rules('lname','Last Name','required');
    $this->form_validation->set_rules('username','User Name','required');
    $this->form_validation->set_rules('password','password','required');
    $this->form_validation->set_rules('gender','Gender','required');
    if ( $this->form_validation->run() == false ) {
    $this->load->view('edit',$data);
    }else{
    $formArray = array();
$formArray['fname']=$this->input->post('fname');
$formArray['lname']=$this->input->post('lname');
$formArray['username']=$this->input->post('username');
$formArray['password']=$this->input->post('password');
$formArray['gender']=$this->input->post('gender');
$this->User_model->update_user($id,$formArray);
$this->session->set_flashdata('success','Record updated Successfully..');
redirect(base_url().'index.php/user/index');
    }
    }
    function delete($id){
    $this->load->model('User_model');
$user = $this->User_model->get_user($id);
if(empty($user)){
$this->session->set_flashdata('failure','Record Not Found..');
redirect(base_url().'index.php/user/index');
}else{
$this->User_model->deleteUser($id);
$this->session->set_flashdata('success','Record Deleted Successfully..');
redirect(base_url().'index.php/user/index');
}
    }
}
?>



C:\xampp\htdocs\newci2\application\models\User_model.php
<?php
class User_model extends CI_model
{
function create($formArray){
$this->db->insert('users',$formArray);
}
function all(){
return $result = $this->db->get('users')->result_array();
}
function get_user($id){
$this->db->where('id', $id);
return $user = $this->db->get('users')->row_array();
}
function update_user($id,$formArray){
$this->db->where('id',$id);
$this->db->update('users', $formArray);
}
function deleteUser($id){
$this->db->where('id', $id);
$this->db->delete('users'); 
}
}
?>



C:\xampp\htdocs\newci2\application\views\create.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Create New User</title>
<link rel="stylesheet" href="<?php echo base_url().'assets/css/bootstrap.min.css'; ?>">
</head>
<body>
<div class="navbar navbar-dark bg-dark">
<div class="container">
<a href="#" class="navbar-brand">Crud Application</a>
</div>
</div>
<div class="container" style="padding-top: 10px;">
<div class="row">
<div class="col-8"><h3>Create User Data:-</h3></div>
<div class="col-4 text-right pull-right">
<a href="<?php echo base_url().'index.php/user/index'; ?>" title="" class="btn btn-primary">Show All Users</a>
</div>
</div>
<hr>
<div class="row">
<div class="col-md-12">
<form name="createUser" action="<?php echo base_url().'index.php/user/create'; ?>" method="post" >
<div class="form-group">
<label for="fname">First Name : </label>
<input type="text" name="fname" id="fname" value="<?php echo set_value('fname'); ?>" placeholder="Enter First Name Here" class="form-control">
<?php echo form_error('fname'); ?>
</div>
<div class="form-group">
<label for="lname">Last Name : </label>
<input type="text" name="lname" id="lname" value="<?php echo set_value('lname'); ?>" placeholder="Enter Last Name Here" class="form-control">
<?php echo form_error('lname'); ?>
</div>
<div class="form-group">
<label for="username">Username : </label>
<input type="text" name="username" id="username" value="<?php echo set_value('username'); ?>" placeholder="Enter Username Here" class="form-control">
<?php echo form_error('username'); ?>
</div>
<div class="form-group">
<label for="name">Password : </label>
<input type="password" name="password" id="password" value="<?php echo set_value('password'); ?>" placeholder="Enter Password Here" class="form-control">
<?php echo form_error('password'); ?>
</div>
<div class="form-group">
<label>Gender : </label>
<input type="radio" name="gender" id="male" value="Male"> <label for="male">Male</label>
<input type="radio" name="gender" id="female" value="Female"> <label for="female">Female</label>
<?php echo form_error('gender'); ?>
</div>
<div class="form-group">
<button class="btn btn-primary">Create</button>
<a href="<?php echo base_url().'index.php/user/index' ?>" class="btn btn-secondary">Cancel</a>
</div>
</form>
</div>
</div>
</div>
</body>
</html>



C:\xampp\htdocs\newci2\application\views\list.php
<!DOCTYPE html>
<html>
<head>
<title>Crud Operation: Display All Users</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url().'assets/css/bootstrap.min.css'; ?>">
</head>
<body>
<div class="navbar navbar-dark bg-dark">
<div class="container">
<a href="#" class="navbar-brand">Crud Application</a>
</div>
</div>
<div class="container" style="padding-top: 10px;">
<div class="row">
<div class="col-md-12">
<?php $success = $this->session->userdata('success'); 
if($success != ""){
?>
<div class="alert alert-success">
<?php echo $success; ?>
</div>
<?php }
?>
<?php $failure = $this->session->userdata('failure'); 
if($failure != ""){
?>
<div class="alert alert-danger">
<?php echo $failure; ?>
</div>
<?php }
?>
</div>
</div>
<div class="row">
<div class="col-8"><h3>View User Data:-</h3></div>
<div class="col-4 text-right pull-right">
<a href="<?php echo base_url().'index.php/user/create'; ?>" title="" class="btn btn-primary">Create</a>
</div>
</div>
<hr>
<div class="row">
<div class="col-md-8">
<table class="table table-striped">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
<th>password</th>
<th>Gender</th>
<th>Action</th>
</tr>
<tbody>
<?php 
if(!empty($users)) { 
foreach($users as $user) {
?>
<tr>
<td><?php echo $user['id']; ?></td>
<td><?php echo $user['fname']; ?></td>
<td><?php echo $user['lname']; ?></td>
<td><?php echo $user['username']; ?></td>
<td><?php echo $user['password']; ?></td>
<td><?php echo $user['gender']; ?></td>
<td><a href="<?php echo base_url().'index.php/user/edit/'.$user['id'] ; ?>" title="Edit" class="btn btn-primary">Edit</a> 
<!-- <td><a href="<?php //echo base_url().'index.php/user/delete/'.$user['id'] ; ?>" title="Delete" class="btn btn-danger">Delete</a> </td> -->
<a href="javascript:void(0);>" onclick="deleteThis( <?php echo $user['id']; ?> );"  title="Delete" class="btn btn-danger">Delete</a> </td>
</tr>
<?php } } else{ ?>
<tr>
<td colspan="7" >No Data Found!</td>
</tr>
<?php } ?>
</tbody>
</table>

</div>
</div>
</div>
<script type="text/javascript">
    var url="<?php echo base_url();?>";
    function deleteThis(id){
       var r=confirm("Do you want to delete this?")
        if (r==true)
          window.location = url+"index.php/user/delete/"+id;
        else
          return false;
        } 
</script>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript"> 
      $(document).ready( function() {
        $('.alert').delay(4500).fadeOut();
      });
    </script>
</body>
</html>



C:\xampp\htdocs\newci2\application\views\edit.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Edit User</title>
<link rel="stylesheet" href="<?php echo base_url().'assets/css/bootstrap.min.css'; ?>">
</head>
<body>
<div class="navbar navbar-dark bg-dark">
<div class="container">
<a href="#" class="navbar-brand">Crud Application</a>
</div>
</div>
<div class="container" style="padding-top: 10px;">
<div class="row">
<div class="col-8"><h3>Edit User:-</h3></div>
<div class="col-4 text-right pull-right">
<a href="<?php echo base_url().'index.php/user/index'; ?>" title="" class="btn btn-primary">Show All Users</a>
</div>
</div>
<hr>
<div class="row">
<div class="col-md-12">
<form name="createUser" action="<?php echo base_url().'index.php/user/edit/'.$user['id']; ?>" method="post" >
<div class="form-group">
<label for="fname">First Name : </label>
<input type="text" name="fname" id="fname" value="<?php echo set_value('fname', $user['fname']); ?>" placeholder="Enter First Name Here" class="form-control">
<?php echo form_error('fname'); ?>
</div>
<div class="form-group">
<label for="lname">Last Name : </label>
<input type="text" name="lname" id="lname" value="<?php echo set_value('lname', $user['lname'] ); ?>" placeholder="Enter Last Name Here" class="form-control">
<?php echo form_error('lname'); ?>
</div>
<div class="form-group">
<label for="username">Username : </label>
<input type="text" name="username" id="username" value="<?php echo set_value('username', $user['username']); ?>" placeholder="Enter Username Here" class="form-control">
<?php echo form_error('username'); ?>
</div>
<div class="form-group">
<label for="name">Password : </label>
<input type="password" name="password" id="password" value="<?php echo set_value('password', $user['password']); ?>" placeholder="Enter Password Here" class="form-control">
<?php echo form_error('password'); ?>
</div>
<div class="form-group">
<label>Gender : </label>
<input type="radio" name="gender" id="male" value="Male" <?php if($user['gender']== 'Male') echo "checked"; ?> > <label for="male">Male</label>
<input type="radio" name="gender" id="female" value="Female" <?php if($user['gender']== 'Female') echo "checked"; ?>> <label for="female">Female</label>
<?php echo form_error('gender'); ?>
</div>
<div class="form-group">
<button class="btn btn-primary">Create</button>
<a href="<?php echo base_url().'index.php/user/index' ?>" class="btn btn-secondary">Cancel</a>
</div>
</form>
</div>
</div>
</div>
</body>
</html>

Wednesday, January 19, 2022

CodeIgniter CRUD example

 C:\xampp\htdocs\mycrud\application\config\config.php

$config['base_url'] = 'http://localhost/mycrud/';

$config['index_page'] = 'index.php';


C:\xampp\htdocs\mycrud\application\config\autoload.php

$autoload['libraries'] = array('database', 'session', 'form_validation');

$autoload['helper'] = array('url');


C:\xampp\htdocs\mycrud\application\config\database.php

$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'mycrud',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);


C:\xampp\htdocs\mycrud\application\controllers\User.php

<?php
class User extends CI_controller
{
function index(){
$this->load->model('User_model');
$users = $this->User_model->all();
$data = array();
$data['users'] = $users;
$this->load->view('list',$data);
}
function create(){
$this->load->model('User_model');
$this->form_validation->set_rules('name','Name','required');
$this->form_validation->set_rules('email','Email','required|valid_email');

if ($this->form_validation->run() == false) {
$this->load->view('create');
}else{
$formArray = array();
$formArray['name']=$this->input->post('name');
$formArray['email']=$this->input->post('email');
$formArray['created_at']=date('Y-m-d');
$this->User_model->create($formArray);
$this->session->set_flashdata('success','Record inserted Successfully..');
redirect(base_url().'index.php/user/index');
}
}
function edit($userid){
$this->load->model('User_model');
$user = $this->User_model->getUser($userid);
$data = array();
$data['user'] = $user;
$this->form_validation->set_rules('name','Name','required');
$this->form_validation->set_rules('email','Email','required|valid_email');
if ($this->form_validation->run() == false) {
$this->load->view('edit', $data);
}else{
//update user data
$formArray = array();
$formArray['name']=$this->input->post('name');
$formArray['email']=$this->input->post('email');
$this->User_model->updateUser($userid, $formArray);
$this->session->set_flashdata('success','Record updated Successfully..');
redirect(base_url().'index.php/user/index');
}
}
function delete($userid){
$this->load->model('User_model');
$user = $this->User_model->getUser($userid);
if(empty($user)){
$this->session->set_flashdata('failure','Record Not Found..');
redirect(base_url().'index.php/user/index');
}else{
$this->User_model->deleteUser($userid);
$this->session->set_flashdata('success','Record Deleted Successfully..');
redirect(base_url().'index.php/user/index');
}
}
}
?>


C:\xampp\htdocs\mycrud\application\models\User_model.php

<?php
class User_model extends CI_model{
function create($formArray){
$this->db->insert('users',$formArray); // insert into users (formArray data)
}
function all(){
return $result = $this->db->get('users')->result_array(); // select * from users
}
function getUser($user_id){
$this->db->where('user_id', $user_id);
return $user = $this->db->get('users')->row_array(); //select * from users where user_id = ?
}
function updateUser($user_id, $formArray){
$this->db->where('user_id',$user_id);
$this->db->update('users', $formArray); // update users  set (formarray data) where user_id = ?
}
function deleteUser($user_id){
$this->db->where('user_id', $user_id);
$this->db->delete('users'); // delete from users where user_id = ?
}
}
?>



C:\xampp\htdocs\mycrud\application\views\create.php

<!DOCTYPE html>
<html>
<head>
<title>Crud Operation: Create User</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url().'assets/css/bootstrap.min.css'; ?>">
</head>
<body>
<div class="navbar navbar-dark bg-dark">
<div class="container">
<a href="#" class="navbar-brand">Crud Application</a>
</div>
</div>
<div class="container" style="padding-top: 10px;">

<div class="row">
<div class="col-8"><h3>Create User Data:-</h3></div>
<div class="col-4 text-right pull-right">
<a href="<?php echo base_url().'index.php/user/index'; ?>" title="" class="btn btn-primary">Show All Users</a>
</div>
</div>
<hr>
<div class="row">
<form name="createUser" action="<?php echo base_url().'index.php/user/create'; ?>" method="post" >
<div class="col-md-6">
<div class="form-group">
<label for="name">Name : </label>
<input type="text" name="name" id="name" value="<?php echo set_value('name'); ?>" placeholder="Enter Name Here" class="form-control">
<?php echo form_error('name'); ?>
</div>
<div class="form-group">
<label for="name">Email : </label>
<input type="email" name="email" id="email" value="<?php echo set_value('email'); ?>" placeholder="Enter Email ID Here" class="form-control">
<?php echo form_error('email'); ?>
</div>
<div class="form-group">
<button class="btn btn-primary">Create</button>
<a href="<?php echo base_url().'index.php/user/index' ?>" class="btn btn-secondary">Cancel</a>
</div>
</div>
</form>
</div>
</div>
</body>
</html>


C:\xampp\htdocs\mycrud\application\views\edit.php

<!DOCTYPE html>
<html>
<head>
<title>Crud Operation: Update User</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url().'assets/css/bootstrap.min.css'; ?>">
</head>
<body>
<div class="navbar navbar-dark bg-dark">
<div class="container">
<a href="#" class="navbar-brand">Crud Application</a>
</div>
</div>
<div class="container" style="padding-top: 10px;">

<div class="row">
<div class="col-8"><h3>Update User:-</h3></div>
<div class="col-4 text-right pull-right">
<a href="<?php echo base_url().'index.php/user/index'; ?>" title="" class="btn btn-primary">Show All Users</a>
</div>
</div>
<hr>
<div class="row">
<form name="createUser" action="<?php echo base_url().'index.php/user/edit/'.$user['user_id']; ?>" method="post" >
<div class="col-md-6">
<div class="form-group">
<label for="name">Name : </label>
<input type="text" name="name" id="name" value="<?php echo set_value('name', $user['name']); ?>" placeholder="Enter Name Here" class="form-control">
<?php echo form_error('name'); ?>
</div>
<div class="form-group">
<label for="name">Email : </label>
<input type="email" name="email" id="email" value="<?php echo set_value('email', $user['email']); ?>" placeholder="Enter Email ID Here" class="form-control">
<?php echo form_error('email'); ?>
</div>
<div class="form-group">
<button class="btn btn-primary">Update</button>
<a href="<?php echo base_url().'index.php/user/index' ?>" class="btn btn-secondary">Cancel</a>
</div>
</div>
</form>
</div>
</div>
</body>
</html>


C:\xampp\htdocs\mycrud\application\views\list.php

<!DOCTYPE html>
<html>
<head>
<title>Crud Operation: Display All Users</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url().'assets/css/bootstrap.min.css'; ?>">
</head>
<body>
<div class="navbar navbar-dark bg-dark">
<div class="container">
<a href="#" class="navbar-brand">Crud Application</a>
</div>
</div>
<div class="container" style="padding-top: 10px;">
<div class="row">
<div class="col-md-12">
<?php $success = $this->session->userdata('success'); 
if($success != ""){
?>
<div class="alert alert-success">
<?php echo $success; ?>
</div>
<?php }
?>
<?php $failure = $this->session->userdata('failure'); 
if($failure != ""){
?>
<div class="alert alert-danger">
<?php echo $failure; ?>
</div>
<?php }
?>
</div>
</div>
<div class="row">
<div class="col-8"><h3>View User Data:-</h3></div>
<div class="col-4 text-right pull-right">
<a href="<?php echo base_url().'index.php/user/create'; ?>" title="" class="btn btn-primary">Create</a>
</div>
</div>
<hr>
<div class="row">
<div class="col-md-8">
<table class="table table-striped">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<tbody>
<?php 
if(!empty($users)) { 
foreach($users as $user) {
?>
<tr>
<td><?php echo $user['user_id']; ?></td>
<td><?php echo $user['name']; ?></td>
<td><?php echo $user['email']; ?></td>
<td><a href="<?php echo base_url().'index.php/user/edit/'.$user['user_id'] ; ?>" title="Edit" class="btn btn-primary">Edit</a> </td>
<!-- <td><a href="<?php //echo base_url().'index.php/user/delete/'.$user['user_id'] ; ?>" title="Delete" class="btn btn-danger">Delete</a> </td> -->
<td><a href="javascript:void(0);>" onclick="deleteThis(<?php echo $user['user_id']; ?>);"  title="Delete" class="btn btn-danger">Delete</a> </td>
</tr>
<?php } } else{ ?>
<tr>
<td colspan="5" >No Data Found!</td>
</tr>
<?php } ?>
</tbody>
</table>

</div>
</div>
</div>
<script type="text/javascript">
    var url="<?php echo base_url();?>";
    function deleteThis(id){
       var r=confirm("Do you want to delete this?")
        if (r==true)
          window.location = url+"index.php/user/delete/"+id;
        else
          return false;
        } 
</script>
</body>
</html>


https://drive.google.com/file/d/1r8BWZzmiInRV5CY4Hjd2hgTxOP-jeG8e/view?usp=sharing


Friday, December 3, 2021

Any autocomplete box with select dropdown

 Any autocomplete dropdown, use this code

$(document).ready(function () {

    var orgid = "0";

    getSeverityList(orgid);

    var $advAutoComplete = $('#addForm87 input[name="orgs_id"]');

    $advAutoComplete.each(function()

    {

      var $el = $(this);

      $el.on( "autocompletechange", function( event, ui )

      {

          orgid = $(event.target).attr('data-selectedid');

          getSeverityList(orgid);

      });

    });

 });


Html is like this

<div style="" class="mui-textfield ui-front mui-col-md-6">
                                    <input name="orgs_id" class="autoCompCollAdv mui--is-empty ui-autocomplete-input mui--is-dirty" type="text" data-returnpropname="orgs_id" data-coll="12" data-alias="o" data-columns="{'ValueProperty':'_id', 'DisplayProperties':['_id','name'], 'FilterDBCols':['_id','name'], 'RunOnFocus':'true', 'Functions':{'Focus':'FocusFunc','Type':'TypeFunc'}, 'PermDBFilters':[] }" value="" data-modalid="addModel87" required="" data-dirty="0" data-selectedid="" autocomplete="off" data-oldval="">
                                    <label class="">Customer ID</label>
  </div>




run any ajax oj js function when bootstrap modal open

For any bootstrap modal open, these are by default class. These classes are not mention but this is worked.   

bs modal

These are default class.

js code for call when modal is open.

$(document).ready(function () {

$( '#editModel87' ).on('show.bs.modal', function(){

  //alert("I want this to appear after the modal has opened!");

  setTimeout(

    function() {

      var orgsid = $('#editForm87 input[name="orgs_id"]').attr('data-selectedid');

      var ticketId = $('#editForm87 input[name="key_id"]').val();

      getSeverityListEdit(orgsid,ticketId);

    }, 3000); // run after 3 seconds

  });

});


Tuesday, November 30, 2021

Marge more than two array (marge by datetime) and display it

 Hello

Here I display how to marge more than two array according to datetime and display it bind together.


public function getTicketChatData($ticketID) {
$noteList = $this->customersupportmodel->getUserTicketNotes($ticketID);
$notes = json_decode($noteList)->Data[0]->Results;
$notesCount = count($notes);

$docList = $this->customersupportmodel->getUserTicketDocs($ticketID);
$docs = json_decode($docList)->Data[0]->Results;
$docsCount = count($docs);

// get job_workflow_history data 
$flowList = $this->customersupportmodel->getJobWorkflowHistory($ticketID);
$flowSteps = json_decode($flowList)->Data[0]->Results;
$flowCount = count($flowSteps);
//end jwh
//echo " c:".$flowCount;

$doc_data = $note_data = $chatData = $flowData = $chatData1 = $chatData2 = [];
if ( $notesCount > 0) {
foreach($notes as $n) {
$note_data[] = [
'type' => 1,
'date' => date('Y-m-d H:i:s', strtotime($n->key_update_date)),
'data' => $n
];
}
}
if ($docsCount > 0 ) {
foreach($docs as $d) {
$doc_data[] = [
'type' => 2,
'date' => date('Y-m-d H:i:s', strtotime($d->key_update_date)),
'data' => $d
];
}
}
if ($flowCount > 0 ) {
foreach($flowSteps as $f) {
$flowData[] = [
'type' => 3,
'date' => date('Y-m-d H:i:s', strtotime($f->key_update_date)),
'data' => $f
];
}
}

//echo "<pre>";print_r($flowData);

$chatData1 = array_merge($note_data, $doc_data);
uasort($chatData1, function ($a, $b) {
    return $a['date'] <=> $b['date'];
});

$chatData = array_merge($chatData1, $flowData);
uasort($chatData, function ($a, $b) {
    return $a['date'] <=> $b['date'];
});

return $chatData;
}


//now display it in view file



$noteHtml = '';
$loggedUserId = $this->session->userdata('UserID');
//echo $countDocNote = count($Notes);
$isDocStart = false;
$countDocNote = count($chatData);
    if ($countDocNote > 0) {
      foreach ($chatData as $key => $notesDocsData) {
        //echo "<pre>"; print_r($notesDocsData);
        $newUser = $notesDocsData['data']->userID;
        $create_by = $notesDocsData['data']->create_by;
        $type = $notesDocsData['type'];

        //$userFirstLetter = strtoupper($notesDocsData['data']->username[0]);

        if($type == 1){
          //echo "notes-"; // for notes
          if ($isDocStart) {
            $noteHtml.= "</div></div>";
            $isDocStart = false;
          }
          $note = $notesDocsData['data']->Note;
          $noteID = $notesDocsData['data']->NoteId;
          $notesCreateDate = $notesDocsData['data']->create_date;
          $notesdate =  date("M j, Y",strtotime($notesCreateDate)); 
          //$created_by_date = date("j M Y",strtotime($notesCreateDate)); 
          $newTimeForNote = new DateTime($notesCreateDate);
          $noteTime = $newTimeForNote->format("h:i a");
          $taskdate =  date("M j, Y",strtotime($notesCreateDate)); 
          //$newTime = new DateTime($taskdate);
          //$taskTime = $newTime->format("h:i a");
          //start display_name
          $fullname = $notesDocsData['data']->name_first." ".$notesDocsData['data']->name_last;
          $displayName = $notesDocsData['data']->display_name;
          if ($displayName == "") {
            $display_name = $notesDocsData['data']->name_first." ".$notesDocsData['data']->name_last;
          }elseif ($notesDocsData['data']->name_first == "" && $notesDocsData['data']->name_last == "") {
            $display_name = $notesDocsData['data']->username;
          }else{
            $display_name = $notesDocsData['data']->display_name;
          }
          $userFirstLetter = strtoupper($display_name[0]);
          //end dsiplay_name

          //if(empty($notesDocsData['data']->org_id)){
          //if($notesDocsData['data']->org_id == 0){ // customer right side support left side
          if($loggedUserId == $create_by) { // logged user chat is right side
            $chatClass = "bsit_chat_user";
          }else{
            $chatClass = "bsit_chat_support";
          }
          $noteHtml.= "<div class='$chatClass'> <div class='bsit_chat_section' id='div_$noteID' >";
            $noteHtml.= "<div class='bsit_chat_user_name'><span>".$userFirstLetter."</span>".$display_name."</div>";
            $noteHtml.= "<div class='bsit_chat_detail'> ".$note." <span class='bsit_chat_time'>".$taskdate." | ".$noteTime."</span></div>";
            //echo "<div class='bsit_chat_attachment'><img src='".base_url()."assets/core/images/attachment_chat.png'>(static)GoogleCloudPrinting.png</div>";
          $noteHtml.= "</div></div>";

        }elseif($type == 2){
          //echo "doc-";// display documents/ attachment
          //$fullname = $notesDocsData['data']->name_first." ".$notesDocsData['data']->name_last;
          //start display_name
          $fullname = $notesDocsData['data']->name_first." ".$notesDocsData['data']->name_last;
          $displayName = $notesDocsData['data']->display_name;
          if ($displayName == "") {
            $display_name = $notesDocsData['data']->name_first." ".$notesDocsData['data']->name_last;
          }elseif ($notesDocsData['data']->name_first == "" && $notesDocsData['data']->name_last == "") {
            $display_name = $notesDocsData['data']->username;
          }else{
            $display_name = $notesDocsData['data']->display_name;
          }
          $userFirstLetter = strtoupper($display_name[0]);
          //end dsiplay_name

          $notesCreateDate = $notesDocsData['data']->create_date;
          $newTimeForNote = new DateTime($notesCreateDate);
          //$notesdate =  date("M j, Y",strtotime($notesCreateDate)); 
          $noteTime = $newTimeForNote->format("h:i a");
          $taskdate =  date("M j, Y",strtotime($notesCreateDate));
          $a_id = $notesDocsData['data']->_id;
          $file_name = $notesDocsData['data']->file_name;
          $file_location = $notesDocsData['data']->file_location;
          $file_update_date = $notesDocsData['data']->key_update_date;
          //if(empty($notesDocsData['data']->org_id)){
          if($loggedUserId == $create_by) { // logged user chat is right side
            $chatClass = "bsit_chat_user";
          }else{
            $chatClass = "bsit_chat_support";
          }
          $documentDownloalLink = base_url()."page/downloadDocument?docid=".$a_id;
          if ($isDocStart && $newUser != $oldUser) {
            $noteHtml.= "</div></div>";
            $isDocStart = false;
          }
          if ($isDocStart == false) {
            $isDocStart = true;
            $noteHtml.= "<div class='$chatClass'> <div class='bsit_chat_section' id='div_$a_id' >";
            $noteHtml.= "<div class='bsit_chat_user_name'><span>".$userFirstLetter."</span>".$display_name."</div>";
          }
          
          $noteHtml.= "<div class='bsit_chat_attachment'><div class='attchment'><a href='".$documentDownloalLink."' title='Attachment' target='_blank'> <img src='".base_url()."assets/core/images/attachment_chat.png'>$file_name</a> </div><span class='bsit_chat_time'>".$taskdate." | ".$noteTime."</span></div>"; 
        }elseif($type == 3){
          //$fullname = $notesDocsData['data']->name_first." ".$notesDocsData['data']->name_last;
          //start display_name
          $fullname = $notesDocsData['data']->name_first." ".$notesDocsData['data']->name_last;
          $displayName = $notesDocsData['data']->display_name;
          if ($displayName == "") {
            $display_name = $notesDocsData['data']->name_first." ".$notesDocsData['data']->name_last;
          }elseif ($notesDocsData['data']->name_first == "" && $notesDocsData['data']->name_last == "") {
            $display_name = $notesDocsData['data']->username;
          }else{
            $display_name = $notesDocsData['data']->display_name;
          }
          $userFirstLetter = strtoupper($display_name[0]);
          //end dsiplay_name

          $notesCreateDate = $notesDocsData['data']->create_date;
          $newTimeForNote = new DateTime($notesCreateDate);
          //$notesdate =  date("M j, Y",strtotime($notesCreateDate)); 
          $noteTime = $newTimeForNote->format("h:i a");
          $taskdate =  date("j M Y",strtotime($notesCreateDate));
          $file_update_date = $notesDocsData['data']->key_update_date;
          $status_title = $notesDocsData['data']->status_title;
          if($status_title == 'Complete'){
            $noteHtml.="<div class='bsit_ticket_close_chat ticketComplete'><span>This ticket is closed by $display_name on $taskdate</span></div>";
          }elseif ($status_title == 'reopen') {
            $noteHtml.="<div class='bsit_ticket_status_chat ticketReOpen'><span>Reopen by $display_name on $taskdate</span></div>";
          }elseif ($status_title == 'Assigned'){
            $noteHtml.="<div class='bsit_ticket_status_chat ticketAccepted'><span>Assigned to $display_name on $taskdate</span></div>";
          }else{
            $noteHtml.="<div class='bsit_ticket_status_chat ticketNormal'><span>$status_title to $display_name on $taskdate</span></div>";
          }
          
        }
        $oldUser = $newUser;
      }
      if ($isDocStart) {
        $noteHtml.= "</div></div>";
        $isDocStart = false;
      }       
      
    }else{
      $noteHtml.= "<div class='noAnyNotesMainDiv'><span>This Ticket has no any notes or attachment!!!</span></div>";
    }
echo $noteHtml;

Friday, November 26, 2021

php pass variable to one page to another

 There are many ways for pass variable value to another page.


1 : GET and POST

You can pass variable value with url. 

header("Location: ../signup.php?newpwd=passwordupdated");
login.php?id=123&username=user

You can add the variable in the link to the next page:

<a href="page2.php?varname=<?php echo $var_value ?>">Page2</a>

This will create a GET variable.

Another way is to include a hidden field in a form that submits to page two:

<form method="get" action="page2.php">
    <input type="hidden" name="varname" value="var_value">
    <input type="submit">
</form>

And then on page two:

//Using GET
$var_value = $_GET['varname'];

//Using POST
$var_value = $_POST['varname'];

//Using GET, POST or COOKIE.
$var_value = $_REQUEST['varname'];

Just change the method for the form to post if you want to do it via post. Both are equally insecure, although GET is easier to hack.

The fact that each new request is, except for session data, a totally new instance of the script caught me when I first started coding in PHP. Once you get used to it, it's quite simple though.

2 : Session:

//On page 1
$_SESSION['varname'] = $var_value;

//On page 2
$var_value = $_SESSION['varname'];

Remember to run the session_start(); statement on both these pages before you try to access the $_SESSION array, and also before any output is sent to the browser.


3 : Cookie:

//One page 1
$_COOKIE['varname'] = $var_value;

//On page 2
$var_value = $_COOKIE['varname'];

The big difference between sessions and cookies is that the value of the variable will be stored on the server if you're using sessions, and on the client if you're using cookies. I can't think of any good reason to use cookies instead of sessions, except if you want data to persist between sessions, but even then it's perhaps better to store it in a DB, and retrieve it based on a username or id.


4 : In MVC (Like Codeigniter):

In mvc, you can pass value in easy way.


<?php $this->load->view('jobs_notes_list', ['activity_history' => $activity_history , 'job_id' => $job_id]);?>