as of now in my code i am able to generate excel report of entire table. I want to generate report of specific row using checkbox. i am not able to do this pls help me out.
From view function i get the table results from which i have to generate excel report of particular row using checkbox. I'm at the final stage of my project please help me out with this.
Thanks in Advance
Controller:
public function exportExcelData($records)
{
$heading = false;
if (!empty($records))
foreach ($records as $row)
{
if (!$heading)
{
echo implode("\t", array_keys($row)) . "\n";
$heading = true;
}
echo implode("\t", ($row)) . "\n";
}
}
public function fetchDataFromTable()
{
$query =$this->db->get('jc_meeting_expense'); // fetch Data from table
$allData = $query->result_array(); // this will return all data into array
$dataToExports = [];
//Export To Excel
foreach ($allData as $data)
{
$arrangeData['Name'] = $data['name'];
$arrangeData['Employee ID'] = $data['employee_id'];
$arrangeData['Category'] = $data['category'];
$arrangeData['Placename'] = $data['place_name'];
$arrangeData['No of Employees'] = $data['no_of_employees'];
$arrangeData['Claimed Amount'] = $data['claimed_amount'];
$arrangeData['Amount per head'] = $data['amount_per_head'];
$arrangeData['Meeting Date'] = $data['meeting_date'];
$arrangeData['Expense Bill'] = $data['expense_bill'];
$arrangeData['Other Expense'] = $data['other_expense'];
$arrangeData['Other Expense Amount'] = $data['amount'];
$dataToExports[] = $arrangeData;
}
$today = date("d.m.y");
// set header
$filename = "Employee Name ".$today.".xls";
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"$filename\"");
$this->exportExcelData($dataToExports);
}
View
<table class="viewjcexpense">
<thead>
<tr>
<th>Select</th><th>Name</th><th>Employee ID</th>
<th>Category</th><th>Place Name</th><th>No Of Employees</th>
<th>Claimed Amount</th><th>Amount Per Head</th>
<th>Meeting Date</th><th>Expense Bill</th><th>Other Expense</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<div class="scroll1">
<?php foreach($query as $row): ?>
<tr>
<td><input type="checkbox" id="checkbox" name="checkbox[]" value="<?php echo $row->name ?>"></td>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->employee_id; ?></td>
<td><?php echo $row->category; ?></td>
<td><?php echo $row->place_name; ?></td>
<td><?php echo $row->no_of_employees; ?></td>
<td><?php echo $row->claimed_amount; ?></td>
<td><?php echo $row->amount_per_head; ?></td>
<td><?php echo $row->meeting_date; ?></td>
<td><?php echo $row->expense_bill; ?></td>
<td><?php echo $row->other_expense; ?></td>
<td><?php echo $row->amount; ?></td>
</tr>
<?php endforeach; ?>
<tr>
<td colspan="12" align="center">
<p>
<input type="submit" value="Generate Report" name="generatereport" formaction="<?php echo site_url('JcMeetingExpense/fetchDataFromTable'); ?>">
</p>
</td>
</tr>
</div>
</tbody>
</table>
Just check your data in the function fetchDataFromTable
something like
public function fetchDataFromTable()
{
$arrRows = $this->input->post('checkbox[]');
if (!empty($arrRows)) $this->db->where_in('id', $arrRows);
$query =$this->db->get('jc_meeting_expense');
...
}
and in your view just put in your id as value in your checkbox element
<td><input type="checkbox" id="checkbox" name="checkbox[]" value="<?php echo $row->id; ?>"></td>
That should pretty much do the job - assuming you did wrap your entire table in a form.
Please check the below code.
Controllers (welcome.php)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index(){
$query[] = array(
'name' => 'empl_1',
'employee_id' => 'empl_1',
'category' => 'empl_1',
'place_name' => 'empl_1',
'no_of_employees' => 'empl_1',
'claimed_amount' => 'empl_1',
'amount_per_head' => 'empl_1',
'meeting_date' => 'empl_1',
'expense_bill' => 'empl_1',
'other_expense' => 'empl_1',
'amount' => 'empl_1',
);
$query[] = array(
'name' => 'empl_2',
'employee_id' => 'empl_2',
'category' => 'empl_2',
'place_name' => 'empl_2',
'no_of_employees' => 'empl_2',
'claimed_amount' => 'empl_2',
'amount_per_head' => 'empl_2',
'meeting_date' => 'empl_2',
'expense_bill' => 'empl_2',
'other_expense' => 'empl_2',
'amount' => 'empl_2',
);
$query[] = array(
'name' => 'empl_3',
'employee_id' => 'empl_3',
'category' => 'empl_3',
'place_name' => 'empl_3',
'no_of_employees' => 'empl_3',
'claimed_amount' => 'empl_3',
'amount_per_head' => 'empl_3',
'meeting_date' => 'empl_3',
'expense_bill' => 'empl_3',
'other_expense' => 'empl_3',
'amount' => 'empl_3',
);
$data['query'] = $query;
$this->load->view('welcome',$data);
}
public function fetchDataFromTable(){
// Here you will get the id of employee and on basis of you need to fetch data from DB.
// var_dump($this->input->post('checkbox'));
$data['selected_emp'] = $this->input->post('checkbox');
$this->load->view('report_view',$data);
}
}
Views (welcome.php)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Simple CodeIgniter App</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<form action="<?php echo base_url('index.php/Welcome/fetchDataFromTable'); ?>" method="post">
<table class="viewjcexpense" border>
<thead>
<tr>
<th>Select</th><th>Name</th><th>Employee ID</th>
<th>Category</th><th>Place Name</th><th>No Of Employees</th>
<th>Claimed Amount</th><th>Amount Per Head</th>
<th>Meeting Date</th><th>Expense Bill</th><th>Other Expense</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<div class="scroll1">
<?php foreach($query as $row): ?>
<tr>
<td><input type="checkbox" id="checkbox" name="checkbox[]" value="<?php echo $row['employee_id']; ?>"></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['employee_id']; ?></td>
<td><?php echo $row['category']; ?></td>
<td><?php echo $row['place_name']; ?></td>
<td><?php echo $row['no_of_employees']; ?></td>
<td><?php echo $row['claimed_amount']; ?></td>
<td><?php echo $row['amount_per_head']; ?></td>
<td><?php echo $row['meeting_date']; ?></td>
<td><?php echo $row['expense_bill']; ?></td>
<td><?php echo $row['other_expense']; ?></td>
<td><?php echo $row['amount']; ?></td>
</tr>
<?php endforeach; ?>
<tr>
<td colspan="12" align="center">
<p> <input type="submit" value="Generate Report" name="generatereport"> </p>
</td>
</tr>
</div>
</tbody>
</table>
</form>
</body>
</html>
Views (report_view.php)
<?php
header("Content-type: application/octet-stream");
header("Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet charset=utf-8");
header("Content-Disposition: attachment; filename=Emp_List.xls");
header("Pragma: no-cache");
header("Expires: 0");
?>
<table class="display" id="userListTbl">
<thead>
<tr>
<td>Emp_ID</td>
</tr>
</thead>
<tbody>
<?php foreach($selected_emp as $v){ ?>
<tr>
<td><?php echo $v; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Related
I have one file PHP for show datatable
My index
<script type="text/javascript">
document.getElementById('cari').
addEventListener('click', jalankan);
function jalankan(){
const unit = document.getElementById('unitcari').value;
const nosam = document.getElementById('nosam').value;
const tahun = document.getElementById('tahun').value;
$.ajax({
url: 'get/rek_cari.php',
data: {'unitcari': unit, 'nosam': nosam, 'tahun': tahun},
cache: false,
success: function(show){
$('#tampilkan').html(show);
}
});
}
</script>
And this is my code show datatable
<?php
require '../../konfigurasi.php';
$unit = $_REQUEST['unitcari'];
$nosam = trim($_REQUEST['nosam']);
$tahun = $_REQUEST['tahun'];
?>
<div>
<table class="table table-hover">
<thead>
<tr>
<th>No</th>
<th>Nama</th>
<th>Alamat</th>
<th>Bulan</th>
<th>Tahun</th>
<th>Awal</th>
<th>Akhir</th>
<th>Rupiah</th>
<th>Aksi</th>
</tr>
</thead>
<?php
$select = mysqli_query($conn, "SELECT * FROM datatran WHERE no_unit = '$unit' AND no_samb = '$nosam' AND tahun = '$tahun' ORDER BY bulan ASC");
$a = 1;
foreach ($select as $tr) :
?>
<tbody>
<tr>
<td><?= $a++ ?></td>
<td><?= $tr['nama']; ?></td>
<td><?= $tr['alamat']; ?></td>
<td><?= $tr['bulan']; ?></td>
<td><?= $tr['tahun']; ?></td>
<td><?= $tr['awal']; ?></td>
<td><?= $tr['akhir']; ?></td>
<td><?= $tr['rupiah']; ?></td>
<td>
<button type="button" id="hapus" class="btn btn-primary"><span class="glyphicon glyphicon-remove"></span>
</button>
</td>
</tr>
</tbody>
<?php endforeach; ?>
</table>
</div>
I want to add button in my table but, I don't know what to do
because that table different files, I can make if that PHP, but I want to make not reload
I am working in a cakephp 2 project. On my view I have a table on which I want to make a cell editable. After editing I want want to save it.
I have tried contentEditable of HTML 5. By that I can edit but data is not passed to controller. I also tried innerHTML which change the cell to input but I can't write anything on the input field. How can I solve the problem? Thanks
<script>
function makeInput(e) {
document.getElementById('test').innerHTML= "<input type='text'>"
}
function formSubmit2(){
document.getElementById('StockReportEditForm').submit();
}
</script>
<?php if(!empty($results)): ?>
<?php echo $this->Form->create('StockReport',array('url'=>array('type'=>'post','action'=>'edit')));?>
<table>
<tr>
<th>Year Week</th>
<th>Counter</th>
<th>Refrigerator</th>
<th>Freezer</th>
<th>Summary</th>
</tr>
<?php foreach ($results as $result): ?>
<?php $today = date("Y-m-d", strtotime("+1 week"));
$date = new DateTime($today);
$week = $date->format("W");
$week_int = (int)$week;
$week_int=$week_int-1;
$last_week = (string)$week_int;
$year = date("Y"); ?>
<tr>
<?php if($year.$last_week==$result['StockReport']['yearweek']): ?>
<td onclick="makeInput(this)" id="test">
<?php echo $result['StockReport']['yearweek']; ?>
<?php else:?>
<td><?php echo $result['StockReport']['yearweek'];?></td>
<?php endif ?>
</td>
<td><?php echo $result['StockReport']['counter']; ?></td>
<td><?php echo $result['StockReport']['refrigerator']; ?></td>
<td><?php echo $result['StockReport']['freezer']; ?></td>
<td><?php echo $result['StockReport']['summary']; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php
// echo $this->Form->submit('Submit');
$options = array(
'label' => 'Submit',
'div' => array(
'class' => 'glass-pill',
),
'id'=>'submitButton',
'onclick'=>'formSubmit2()'
);
echo $this->Form->end($options); ?>
<?php endif; ?>
I would create a hidden input within the td element and when the form is submitted I copy the htmlinner of the contenteditabe to the hidden input value.
I have a Chart with an select option for the Year, by the default the selection option will choose the newest Year, but the user can choose to see another data from another Year through this select option
<script type="text/javascript">
$(document).ready(function() {
$('table.highchart').highchartTable();
$('.btnTahun').click(function() {
$coba = $('#Year').val();
$.ajax({
type: 'POST',
url: "<?php echo site_url('dashboard/list_data/')?>",
data: {'tahun':$coba},
success: function(data) {
$("#tableChart").html(data);
}
});
});
});
</script>
The problem is when I choose another Year the previously selected year still exist in my view, i want to make it that only the selected year will be show in my view, take a look at my picture below, as u can see i choose year 2016, but the data chart from 2017 is still showed in my view, and i don't want that, what is wrong with my Ajax? or Perhaps is it possible because my controller or Model?
Structure of My Html to show the chart can be seen below
<table id="tableChart" class="highchart" data-graph-container-before="1" data-graph-type="column">
<thead style="display: none">
<tr>
<th>Month</th>
<th>Category Project</th>
</tr>
</thead>
<tbody style="display: none">
<tr>
<td>Jan</td>
<td><?php echo $januari ?></td>
</tr>
<tr>
<td>Feb</td>
<td><?php echo $februari ?></td>
</tr>
<tr>
<td>Mar</td>
<td><?php echo $maret ?></td>
</tr>
<tr>
<td>Apr</td>
<td><?php echo $april ?></td>
</tr>
<tr>
<td>May</td>
<td><?php echo $mei ?></td>
</tr>
<tr>
<td>June</td>
<td><?php echo $juni ?></td>
</tr>
<tr>
<td>July</td>
<td><?php echo $juli ?></td>
</tr>
<tr>
<td>Aug</td>
<td><?php echo $agustus ?></td>
</tr>
<tr>
<td>Sep</td>
<td><?php echo $september ?></td>
</tr>
<tr>
<td>Oct</td>
<td><?php echo $oktober ?></td>
</tr>
<tr>
<td>Nov</td>
<td><?php echo $november ?></td>
</tr>
<tr>
<td>Dec</td>
<td><?php echo $desember ?></td>
</tr>
</tbody>
You can use form submit rather than ajax since your controller function creates another view when you call it. heres a link for form helper https://www.codeigniter.com/userguide3/helpers/form_helper.html
CONTROLLER
public function list_data(){
$this->load->helper('form');
$thn = $this->input->post('tahun');
$data['januari'] = $this->dashboard_m->get_kategori_totals('01',$thn)->num_rows();
$data['februari'] = $this->dashboard_m->get_kategori_totals('02',$thn)->num_rows();
$data['maret'] = $this->dashboard_m->get_kategori_totals('03',$thn)->num_rows();
//And the code above will be the same until december, the differences only in the parameter
$data['title'] = 'Aplikasi Saranabudi';
$data['aktif'] = 'Jumlah Kategori Project';
$data['judul_hal']= 'Dashboard Kategori Project';
$this->load->view('dashboard/kategori_project/list_data',$data);
}
VIEW
$attributes = array('id' => 'myID', 'name' => 'myName', 'class' => 'myClass', 'method' => 'post');
echo form_open('', $attributes);
$options = array(
'2015' => '2015',
'2016' => '2016',
'2017' => '2017',
'2018' => '2018',
);
echo form_dropdown('tahun', $options);
// THE CHART
echo form_submit(array('id' => 'filter_button', 'name' => 'filter_button'), 'Filter');
echo form_close();
The below code is connected to a local database that holds customer information. First, the page is supposed to display the customer name and country, but should display more information for that particular customer once the name is clicked.
The problem with this code below is that it will only always display the first customers information, no matter which name is clicked. Can anyone see where I might be going wrong? Do I need to create an ID for each button (customer name)? Bare in mind that there are a lot of customers in the database and I feel that an ID for each would be very difficult to implement, especially if the database gets modified.
<DOCTYPE html!>
<html>
<head>
<link rel="stylesheet" type="text/css" href="a3.css">
</head>
<body>
<div class="nav">
<?php include 'nav.php';
?>
</div>
<?php include 'dbconfig.php';
$sql ='SELECT * FROM `customers` ORDER BY `customers`.`country` ASC';
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
?>
<div class = "main">
<table>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><button onclick = "showCustomer();"/><?php echo ($r['customerName'])?></td>
<td><?php echo ($r['country'])?></td>
</tr>
<tr id ="extra" style="display:none">
<td><?php echo ($r['customerNumber'])?></td>
<td><?php echo ($r['contactLastName'])?></td>
<td ><?php echo ($r['contactFirstName'])?></td>
<td><?php echo ($r['phone'])?></td>
<td><?php echo ($r['addressLine1'])?></td>
<td><?php echo ($r['addressLine2'])?></td>
<td><?php echo ($r['city'])?></td>
<td><?php echo ($r['state'])?></td>
<td><?php echo ($r['postalCode'])?></td>
<td><?php echo ($r['salesRepEmployeeNumber'])?> </td>
<td><?php echo ($r['creditLimit'])?></td>
</tr>
<?php endwhile; ?>
</table>
</div>
<div class="foot">
<?php include 'foot.php'; ?>
</div>
<script>
function showCustomer() {
var showForm=document.getElementById('extra');
if (showForm.style.display="none") {
showForm.style.display ="";
alert('ok');
console.log(showForm);
}
else if (showForm.style.display=""){
showForm.style.display ="none";
}
}
</script>
</body>
</html>
</DOCTYPE>
All your <tr> have the same id="extra". As suggested, you need a different "id" for each of them, this is how : I created a variable $i (line 18), that is increased right after the while (line 20), inserted as parameter in the call for showCustomer (line 23), inserted at the end of the id "extra" (line 27), finally, on the bottom, where function showCustomer is declared, the parameter "index" is inserted at the end of the id "extra".
<DOCTYPE html!>
<html>
<head>
<link rel="stylesheet" type="text/css" href="a3.css">
</head>
<body>
<div class="nav">
<?php include 'nav.php';
?>
</div>
<?php include 'dbconfig.php';
$sql ='SELECT * FROM `customers` ORDER BY `customers`.`country` ASC';
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
?>
<div class = "main">
<table>
<?php $i = 0;
while ($r = $q->fetch()):
$i++; // UNIQUE INDEX FOR EACH CUSTOMER.
?>
<tr>
<td><button onclick = "showCustomer('<?php echo $i;?>');"/>
<?php echo ($r['customerName'])?></td>
<td><?php echo ($r['country'])?></td>
</tr>
<tr id ="extra<?php echo $i;?>" style="display:none">
<td><?php echo ($r['customerNumber'])?></td>
<td><?php echo ($r['contactLastName'])?></td>
<td ><?php echo ($r['contactFirstName'])?></td>
<td><?php echo ($r['phone'])?></td>
<td><?php echo ($r['addressLine1'])?></td>
<td><?php echo ($r['addressLine2'])?></td>
<td><?php echo ($r['city'])?></td>
<td><?php echo ($r['state'])?></td>
<td><?php echo ($r['postalCode'])?></td>
<td><?php echo ($r['salesRepEmployeeNumber'])?> </td>
<td><?php echo ($r['creditLimit'])?></td>
</tr>
<?php endwhile; ?>
</table>
</div>
<div class="foot">
<?php include 'foot.php'; ?>
</div>
<script>
function showCustomer( index ) { // UNIQUE INDEX AS PARAMETER.
var showForm=document.getElementById('extra' + index); // USING INDEX.
if (showForm.style.display="none") {
showForm.style.display ="";
alert('ok');
console.log(showForm);
}
else if (showForm.style.display=""){
showForm.style.display ="none";
}
}
</script>
</body>
</html>
</DOCTYPE>
I am working on a PHP file. I am trying to make a table that shows the list of products from database. There will be also a button for deleting any product. I have used javascript for deleting products from database. I have written the code and could not find anything wrong. When I click delete button, it shows me the confirmation box, but does not delete the product. Here is the code:
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("grocery_shop",$con);
error_reporting(E_ALL^E_NOTICE);
session_start();
$sql = mysql_query("select * from products");
if($_GET['did']){
mysql_query("delete from products where product_id='$_GET[did]'");
header("Location: product.php");
}
?>
<table border="1px" style="width:100%">
<tr>
<th>Serial No</th>
<th>Product Name</th>
<th>Product Type</th>
<th>Quantity</th>
<th>Price</th>
<th>Delete Product</th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<?php
$i=1;
while ($u = mysql_fetch_array($sql)) {
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $u['product_name'];?></td>
<td><?php echo $u['product_type'];?></td>
<td><?php echo $u['quantity'];?></td>
<td><?php echo $u['price'];?></td>
<td><?php echo "Delete";?></td>
</tr>
<?php
$i++;
}
?>
<script>
function delproduct(id){
var msg = confirm("Are you sure you want to delete this product?");
if (msg) {
window.location = "product.php?did="+product_id;
}
}
</script>
</table>
The problem is in your javascript, the product_id doesn't exist
if (msg) {
window.location = "product.php?did="+id;
}
For debugging purposes try to replace this with your code and let us know the error message you get.
if($_GET['did']){
mysql_query("delete from products where product_id='".$_GET['did']."'");
echo "delete from products where product_id='".$_GET['did']."'";
echo mysql_errno() . ": " . mysql_error() ;
die();
//header("Location: product.php");
}
additionally also try to run the query without the single quotes, i am assuming product_id is an integer.
so mysql_query("delete from products where product_id=".$_GET['did']);
You forgot the '' around did in $_GET[did]:
mysql_query("delete from products where product_id='{$_GET['did']}'");
Also, as #chris85 noted, is not a good idea to use $_GET or $_POST directly, remember to sanitize these values before using it in a query.
$did = filter_input(INPUT_GET, 'did', FILTER_SANITIZE_NUMBER_INT);
mysql_query("delete from products where product_id={$did}");
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("grocery_shop",$con);
error_reporting(E_ALL^E_NOTICE);
session_start();
$sql = mysql_query("select * from products");
if($_GET['did']){
mysql_query("delete from products where product_id='$_GET[did]'");
header("Location: product.php");
}
?>
<table border="1px" style="width:100%">
<tr>
<th>Serial No</th>
<th>Product Name</th>
<th>Product Type</th>
<th>Quantity</th>
<th>Price</th>
<th>Delete Product</th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<?php
$i=1;
while ($u = mysql_fetch_array($sql)) {
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $u['product_name'];?></td>
<td><?php echo $u['product_type'];?></td>
<td><?php echo $u['quantity'];?></td>
<td><?php echo $u['price'];?></td>
<td><?php echo "Delete";?></td>
</tr>
<?php
$i++;
}
?>
<script>
function delproduct(id){
var product_id = id;
var msg = confirm("Are you sure you want to delete this product?");
if (msg) {
window.location = "product.php?did="+product_id;
}
}
</script>
</table>
You should try: delproduct($u[product_id]) instead of delproduct(id=$u[product_id])
mysql_query("delete from products where product_id='".$_GET['did']."'");
while ($u = mysql_fetch_array($sql)) {
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $u['product_name'];?></td>
<td><?php echo $u['product_type'];?></td>
<td><?php echo $u['quantity'];?></td>
<td><?php echo $u['price'];?></td>
<td><?php echo "Delete";?></td>
</tr>
<?php
$i++;
}