I want to export searched data to the laravel below is my controller
public function getUsageData(Request $request)
{
$start_date = $request->get('start_date');
$end_date = $request->get('end_date');
$particulars = DB::table('particulars')
->join('reqs', 'particulars.particular_id', "=", 'reqs.particular_id')
->whereBetween('date_applied', [$start_date, $end_date])
->select('particulars.item_name', 'particulars.unit', 'particulars.price', 'reqs.quantity_issued',
DB::raw('particulars.price*reqs.quantity_issued AS total_cost'))
->get();
if ($particulars->isEmpty()) {
return "No Records Found...................... ";
} else {
$pdf = PDF::loadView('issuer.getUsageReport', ['particulars' => $particulars]);
return $pdf->stream('getUsageReport.issuer');
}
}
I want to retrieve those data to the excel instead of pdf please help
Replace the two PDF lines in the else { ... } statement with:
$list = $particulars->toArray();
//This line adds headers if present in your data
array_unshift($list, array_keys($list[0]));
$callback = function() use ($list) {
$FH = fopen('php://output', 'w');
foreach ($list as $row) {
fputcsv($FH, $row);
}
fclose($FH);
};
return response()->streamDownload($callback,"filename.csv");
Depending on the structure of particulars you may need to do some manipulation to the data first, e.g. flattening. Comment below if so.
Related
How can I read an excel file and compare each reference from my CSV file in my database. I specify that the CSV file and my database contain the same fields (customer number, transaction amount, currency, reference and status). I will retrieve transactions with the same reference but different status.
public function upload_file(Request $request){
$file = file($request->file->getRealPath());
$data = array_slice($file,1);
$parts = (array_chunk($data, 1000));
foreach ($parts as $index => $part) {
$fileName = public_path('excel/'.date('d-m-y-H-i-s').$index. '.csv');
file_put_contents($fileName, $part);
}
(new ExcelUploadTransaction())->importToDb();
return redirect()->route('supportone.import_excel');
}
public function importToDb(){
$path = public_path('excel/*.csv');
$files = glob($path);
foreach ($files as $file) {
$data = array_map('str_getcsv', file($file));
foreach ($data as $row) {
ExcelUploadTransaction::create([
'customer_details'=>$row[0],
'amount'=>$row[1],
'currency'=>$row[2],
'telco_reference'=>$row[3],
'status'=>$row[4]
]);
}
unlink($file);
}
}
public function compare(){
$db_csv = DB::connection('mysql3')->table('excel_upload_transactions')->select('telco_reference','status')->get();
$db_table = DB::table('transac')->select('telco_reference','status')->where('telco_reference',$telco_reference)->get();
$collection = collect($db_csv);
$diff = $collection->diff($db_table);
$diff->all();
return view('supportone.transactions.update.multiple',compact('rows','header'));
}
i tried to generate pdf from my request data function. but it can't pass variable into my pdf generate function. i get requested data from this function.
public function fetch_data()
{
if(request()->ajax())
{
if(request()->from_date != '' && request()->to_date != '')
{
$data = DB::table('sales')
->whereBetween('dlvd_date', array(request()->from_date, request()->to_date))
->get();
} else {
$data = DB::table('sales')->orderBy('dlvd_date', 'desc')->get();
}
echo json_encode($data);
}
}
i want to get this $data value into my pdf generated function like below.
function convert_sales_data_to_html($data)
{
$sales_data = $data;
dd($sales_data);
$output = '';
return $output;
}
and pass to this function
public function pdf($data)
{
$pdf = \App::make('dompdf.wrapper');
$pdf->loadHTML($this->convert_sales_data_to_html($data))->setPaper('a4', 'landscape');
return $pdf->stream();
}
i tried this so many times but show me errors with missing argument. how can i fix this or have another method to generate pdf from requested data in laravel.
this is my routes
Route::post('salesreport/fetch', 'Report\SalesReportController#fetch_data')->name('salesreport.fetch_data');
Route::get('salesreport/pdf', 'Report\SalesReportController#pdf');
my controller is
function shiftstudentdetailsmultiple(){
$data['Show'] = $this->input->post('show[]');
$roll = $this->input->post('roll[]');
//echo sizeof($data1);
for($i=0;$i<sizeof($roll);$i++)
{
$data['results'] = $this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]);
}
$this->load->view('registrar/shift_student_view', $data);
}
and model is
function getstudentsdetailscouseandmultiple($Uniq_Id){
$this->db->select("sprd.Uniq_Id, sprd.Name, sprd.Uni_Roll_No, cd.Course_Name, bd.Branch_Name, sprd.Year, sprd.Session, sprd.Section, sprd.Fee_Status, sprd.Curr_Status");
$this->db->where("sprd.Uniq_Id",$Uniq_Id);
$this->db->from("Students_Prim_Detals sprd");
$this->db->join('Course_Details cd', 'cd.Course_Id=sprd.Course_id');
$this->db->join('Branch_Details bd', 'bd.Branch_Id=sprd.Branch_id');
$query = $this->db->get();
if ($query->num_rows() > 0){
return $query->result();
}
else {
return false;
}
}
when showing in view the fetching data showing only one time.. but in my database i have more then 4 entry when i print_r
$data['results']=this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]);
the data showing correct .. when load only showing one data..the last entry..
$this->load->view('registrar/shift_student_view', $data);
Each time you call...
$data['results'] = $this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]);
you replace the value stored in $data['results'] with the new return. So only one return is sent to the view.
You will need to revise your controller along these lines.
$student_detail = array();
for($i=0;$i<sizeof($roll);$i++)
{
$student_detail[] = $this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]);
}
$data['results'] = $student_detail;
Then in your view you will have to loop through $results to display each item in the array.
foreach($results as $detail){
echo "Name: ". $detail->name;
echo "Course: ". $detail->Course_Name;
//etc...
}
OK, below is a get_members function in my controller that I pulled and manipulated from the interwebs... i can print out the information from $output in the controller, but I don't want to do that... I cannot figure out how to get it to be part of my view so I can list info from it freely...
I know it has something to do with the $data array in the index function... can anyone assist?
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Phonedirectory extends CI_Controller {
function get_members($group=FALSE,$inclusive=FALSE) {
// Active Directory server
$ldap_host = "fps";
// Active Directory DN
$ldap_dn = "OU=Users,DC=xxx,DC=org";
// User attributes we want to keep
$keep = array(
"samaccountname",
"displayName",
"telephonenumber"
);
// Connect to AD
$ldap = ldap_connect($ldap_host) or die("Could not connect to LDAP");
ldap_set_option ($ldap, LDAP_OPT_REFERRALS, 0);
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_bind($ldap, "CN=LDAP Reader,OU=Users - Special,DC=xxx,DC=org", "xxx") or die("Could not bind to LDAP");
// Begin building query
if($group) $query = "(&"; else $query = "";
$query .= "(&(objectClass=user)(objectCategory=person))";
// Filter by memberOf, if group is set
if(is_array($group)) {
// Looking for a members amongst multiple groups
if($inclusive) {
// Inclusive - get users that are in any of the groups
// Add OR operator
$query .= "(|";
} else {
// Exclusive - only get users that are in all of the groups
// Add AND operator
$query .= "(&";
}
// Append each group
foreach($group as $g) $query .= "(memberOf=OU=$g,$ldap_dn)";
$query .= ")";
} elseif($group) {
// Just looking for membership of one group
$query .= "(memberOf=OU=$group,$ldap_dn)";
}
// Close query
if($group) $query .= ")"; else $query .= "";
// Uncomment to output queries onto page for debugging
// print_r($query);
// Search AD
$results = ldap_search($ldap,$ldap_dn,$query);
$entries = ldap_get_entries($ldap, $results);
// Remove first entry (it's always blank)
array_shift($entries);
$output = array(); // Declare the output array
$i = 0; // Counter
// Build output array
foreach($entries as $u) {
foreach($keep as $x) {
// Check for attribute
if(isset($u[$x][0])) $attrval = $u[$x][0]; else $attrval = NULL;
// Append attribute to output array
$output[$i][$x] = $attrval;
}
$i++;
}
return $output;
}
public function index() {
$data = array('title' => 'Phone Directory', 'main_content' => 'pages/phoneDirectory');
$this->load->view('template/main', $data);
}
}
Correct me if I'm wrong, but it sounds like you just want the output sent to the view?
If that's the case, then add this to the index function
public function index() {
$data = array('title' => 'Phone Directory', 'main_content' => 'pages/phoneDirectory');
$data['members'] = $this->get_members();
$this->load->view('template/main', $data);
}
or however you want to append that to your data array.
Then in the view you can do:
<?php echo print_r($members, TRUE); ?>
I'm newbie in codeigniter and still learning. Anyone can help for sample in basic view, add, update, delete operation and queries in codeigniter will gladly appreciated.
Just a simple one like creating addressbook for newbie.
thanks,
best regards
Some sample queries in Codeigniter
class Names extends Model {
function addRecord($yourname) {
$this->db->set("name", $yourname);
$this->db->insert("names");
return $this->db->_error_number(); // return the error occurred in last query
}
function updateRecord($yourname) {
$this->db->set("name", $yourname);
$this->db->update("names");
}
function deleteRecord($yourname) {
$this->db->where("name", $yourname);
$this->db->delete("names");
}
function selectRecord($yourname) {
$this->db->select("name, name_id");
$this->db->from("names");
$this->db->where("name", $yourname);
$query = $this->db->get();
return $this->db->result();
}
function selectAll() {
$this->db->select("name");
$this->db->from("names");
return $this->db->get();
}
}
More information and more ways for CRUD in codeigniter active record documentation
More about error number over here
A sample controller
class names_controller extends Controller {
function addPerson() {
$this->load->Model("Names");
$name = $this->input->post("name"); // get the data from a form submit
$name = $this->xss->clean();
$error = $this->Names->addRecord($name);
if(!$error) {
$results = $this->Names->selectAll();
$data['names'] = $results->result();
$this->load->view("show_names", $data);
} else {
$this->load->view("error");
}
}
}
More about controllers over here
A sample view - show_names.php
<table>
<tr>
<td>Name</td>
</tr>
<?php foreach($names as $row): ?>
<tr><td><?ph echo $row->name; ?></td></tr>
<?php endforeach; ?>
</table>
More about codeigniter views over here
You can use this as an example
class Crud extends Model {
// selecting records by specifying the column field
function select()
{
// use $this->db->select('*') if you want to select all the records
$this->db->select('title, content, date');
// use $this->db->where('id', 1) if you want to specify what row to be fetched
$q = $this->db->get('mytable');
// to get the result
$data = array();
// for me its better to check if there are records that are fetched
if($q->num_rows() > 0) {
// by doing this it means you are returning array of records
foreach($q->result_array() as $row) {
$data[] = $row;
}
// if your expecting only one record will be fetched from the table
// use $row = $q->row();
// then return $row;
}
return $data;
}
// to add record
function add()
{
$data = array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
);
$this->db->insert('mytable', $data);
}
// to update record
function update()
{
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', 1);
$this->db->update('mytable', $data);
}
// to delete a record
function delete()
{
$this->db->where('id', 1);
$this->db->delete('mytable');
}
}
Some of this are from codeigniter userguide.
To view the records,
If return data is array of records,
foreach($data as $row)
{
echo $row['title'] . "<br />";
}
If the return data is an object (by using $q->row),
echo $data->title;
This is just a few examples or CRUD in Codeigniter. Visit the codeigniter userguide.