codeigniter generating multiple pdfs - codeigniter

i'm trying to generates pdfs in loop against member id, but every time i got only the first one, i addedd code in foreach so it runs multiple times, but it gives me only one pdf.please someone help me.
public function pdf($ids){
$k = 0;
foreach ($ids as $key => $id) {
$data[$k]['academics'] = $this->Utility_model->get_all_from_table_where('member_academics', array('member_id' => $id));
$data[$k]['member_employment'] = $this->Utility_model->get_all_from_table_where('member_employment', array('member_id' => $id));
$data[$k]['member_data'] = $this->Utility_model->get_all_from_table_where('members',array('member_id' => $id));
$data[$k]['disclosure'] = $this->Utility_model->get_all_from_table('generalsettings',array('key'=>'disclosure'));
$data[$k]['enrollment_fee'] = $this->Utility_model->get_all_from_table('member_invoices',array('user_id' => $id,'paymentFor'=>'Enrollment Fee'));
$data[$k]['enrollment_fee']['payment'] = $this->Utility_model->get_all_from_table('member_payments',array('invoice_id'=>$data[$k]['enrollment_fee']['id']));
$j=0;
if (!empty($data[$k]['member_data'])) {
foreach ($data[$k]['member_data'] as $member) {
$data[$k]['member_data'][$j]['term'] = $this->Utility_model->get_all_from_table('term', array('term_id' => $member['term_id']));
$data[$k]['member_data'][$j]['degree'] = $this->Utility_model->get_all_from_table('degree', array('degree_id' => $member['degree_id']));
$data[$k]['member_data'][$j]['year'] = $this->Utility_model->get_all_from_table('year', array('year_id' => $member['year_id']));
$data[$k]['member_data'][$j]['address'] = $this->Utility_model->get_all_from_table('member_addresses', array('id' => $member['address_id']));
$j++;
}
}
$this->load->view('admin/reports_management/applications_pdf/pdf_output',$data[$k],false);
$html = $this->output->get_output();
$this->load->library('dompdf_gen');
$this->dompdf->load_html($html);
$this->dompdf->render();
$this->dompdf->stream($k."file".".pdf");
$k++;
}
// echo "<pre>";
// print_r($data);
// die;
}

As long as you only request the page once you cannot get the controller method to output more than one file. You could either try to instead make a multiple page PDF or you need to change how the controller is called, and call it multiple times.
See this answer for some input about making multiple page PDF: How to create several pages with dompdf

Related

How to insert big data on the laravel?

I am using laravel 5.6
My script to insert big data is like this :
...
$insert_data = [];
foreach ($json['value'] as $value) {
$posting_date = Carbon::parse($value['Posting_Date']);
$posting_date = $posting_date->format('Y-m-d');
$data = [
'item_no' => $value['Item_No'],
'entry_no' => $value['Entry_No'],
'document_no' => $value['Document_No'],
'posting_date' => $posting_date,
....
];
$insert_data[] = $data;
}
\DB::table('items_details')->insert($insert_data);
I have tried to insert 100 record with the script, it works. It successfully insert data
But if I try to insert 50000 record with the script, it becomes very slow. I've waited about 10 minutes and it did not work. There exist error like this :
504 Gateway Time-out
How can I solve this problem?
As it was stated, chunks won't really help you in this case if it is a time execution problem. I think that bulk insert you are trying to use cannot handle that amount of data , so I see 2 options:
1 - Reorganise your code to properly use chunks, this will look something like this:
$insert_data = [];
foreach ($json['value'] as $value) {
$posting_date = Carbon::parse($value['Posting_Date']);
$posting_date = $posting_date->format('Y-m-d');
$data = [
'item_no' => $value['Item_No'],
'entry_no' => $value['Entry_No'],
'document_no' => $value['Document_No'],
'posting_date' => $posting_date,
....
];
$insert_data[] = $data;
}
$insert_data = collect($insert_data); // Make a collection to use the chunk method
// it will chunk the dataset in smaller collections containing 500 values each.
// Play with the value to get best result
$chunks = $insert_data->chunk(500);
foreach ($chunks as $chunk)
{
\DB::table('items_details')->insert($chunk->toArray());
}
This way your bulk insert will contain less data, and be able to process it in a rather quick way.
2 - In case your host supports runtime overloads, you can add a directive right before the code starts to execute :
ini_set('max_execution_time', 120 ) ; // time in seconds
$insert_data = [];
foreach ($json['value'] as $value)
{
...
}
To read more go to the official docs
It makes no sense to use an array and then convert it to a collection.
We can get rid of arrays.
$insert_data = collect();
foreach ($json['value'] as $value) {
$posting_date = Carbon::parse($value['Posting_Date']);
$posting_date = $posting_date->format('Y-m-d');
$insert_data->push([
'item_no' => $value['Item_No'],
'entry_no' => $value['Entry_No'],
'document_no' => $value['Document_No'],
'posting_date' => $posting_date,
....
]);
}
foreach ($insert_data->chunk(500) as $chunk)
{
\DB::table('items_details')->insert($chunk->toArray());
}
Here is very good and very Fast Insert data solution
$no_of_data = 1000000;
$test_data = array();
for ($i = 0; $i < $no_of_data; $i++){
$test_data[$i]['number'] = "1234567890";
$test_data[$i]['message'] = "Test Data";
$test_data[$i]['status'] = "Delivered";
}
$chunk_data = array_chunk($test_data, 1000);
if (isset($chunk_data) && !empty($chunk_data)) {
foreach ($chunk_data as $chunk_data_val) {
DB::table('messages')->insert($chunk_data_val);
}
}
I used the code below to check the update or insert data of 11 thousand rows. I hope it useful for you.
$insert_data = [];
for ($i=0; $i < 11000; $i++) {
$data = [
'id' =>'user_'.$i,
'fullname' => 'Pixs Nguyen',
'username' => 'abc#gmail.com',
'timestamp' => '2020-03-23 08:12:00',
];
$insert_data[] = $data;
}
$insert_data = collect($insert_data); // Make a collection to use the chunk method
// it will chunk the dataset in smaller collections containing 500 values each.
// Play with the value to get best result
$accounts = $insert_data->chunk(500);
// In the case of updating or inserting you will take about 35 seconds to execute the code below
for ($i=0; $i < count($accounts); $i++) {
foreach ($accounts[$i] as $key => $account)
{
DB::table('yourTable')->updateOrInsert(['id'=>$account['id']],$account);
}
}
// In the case of inserting you only take about 0.35 seconds to execute the code below
foreach ($accounts as $key => $account)
{
DB::table('yourTable')->insert($account->toArray());
}

Batch insert in codeigniter

Hi I am quite new to cpdeignoter I just wanted to ask if I am doing the insert batch in ci right because it seemed not to work here is my controller
foreach($dat as $key => $val){
$data[] = array('ModelNumber' => $_POST['txt_model_num'][$key],
'Access' => $_POST['txt_accessories'][$key],
'SerialNumber' => $_POST['txt_snum'][$key],
'Charges' => $_POST['txt_charges'][$key],
'OtherRemarks' => $_POST['txt_rem'][$key],
'RequirementID' => $id1);
}
$cmd3 = $this->Software_model->add_type($data);
if($cmd3){
foreach($sql->result_array() as $row){
$id2 = $row['ID'];
}
$data2s = array();
foreach($dat as $key => $val){
$data2s[] = array('EquipmentName' => $_POST['txt_equipb'][$key],
'EquipmentType' => $_POST['txt_equiptype'][$key],
'RequirementID' => $id2);
}
$cmd2 = $this->Software_model->add_equip($data2s);
and this is the model
public function add_type($data)
{
return $this->db->insert_batch('jobtype', $data);
}
public function add_equip($data2s)
{
return $this->db->insert_batch('equipment', $data2s);
}
and the way I duplicate the textboxes in the views is like this
function second function(){
var etype = document.createElement('input');
etype.type = 'text';
etype.setAttribute("name", "txt_equiptype[]");
etype.setAttribute("class", "form-control");
etype.setAttribute("id", "etype");
etype.setAttribute("placeholder", "Enter Equipment Type");
document.getElementById('third').appendChild(etype);
}
thanks in advance
Hope this will help you :
Try out this alternate method :
foreach($dat as $key => $val)
{
$data[$key]['ModelNumber'] = $_POST['txt_model_num'][$key];
$data[$key]['Access'] = $_POST['txt_accessories'][$key];
$data[$key]['SerialNumber'] = $_POST['txt_snum'][$key];
$data[$key]['Charges'] = $_POST['txt_charges'][$key];
$data[$key]['OtherRemarks'] = $_POST['txt_rem'][$key];
$data[$key]['RequirementID'] = $id1;
}
/* Note : remove auto increment primary key from above array if you are including*/
user it for both the foreach

codeigniter Trying to get property of non-object

$account_types = $this->members_model->getAllAccountTypes();
$result_set = array();
foreach($account_types as $account_type){
$result_set[$account_type->db_name] = array(
'subscribed' => $this->members_model->getAllSubscribedMembers($account_type->id),
'unsubscribed' => $this->members_model->getAllUnsubscribedMembers($account_type->id)
);
}
echo json_encode($result_set);
return;
i have these line of code which return me Trying to get property of non-object on foreach($account_types as $account_type)
Change your code with following it will solved your problem :-
$result_set = array();
$account_types = json_decode(json_encode($account_types), true);
foreach($account_types as $account_type){
$result_set[$account_type->db_name] = array(
'subscribed' => $this->members_model->getAllSubscribedMembers($account_type->id),
'unsubscribed' => $this->members_model->getAllUnsubscribedMembers($account_type->id)
);
}
Always take care of your query results in the model:
This could be possible that the query returns null so you better test the result first:
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->body;
}
}
You can also pass a string to result() which represents a class to instantiate for each result object. More Information

how to get data from database throught model using codeigniter with for each loop

http error occured while calling data from model using function
model
public function getProductCombo() {
$q = $this->db->get_where('products', array('type' => 'combo'));
if ($q->num_rows() > 0) {
foreach (($q->result()) as $row) {
$data[] = $row;
}
return $data;
}
}
controller
function sets() {
$this->sma->checkPermissions();
$this->load->helper('security');
$this->data['error'] = (validation_errors() ? validation_errors() :
$this->session->flashdata('error'));
// problem in this line also
$this->data['showcombo'] = $this->load->sales_model->getComboProduct();
$bc = array(array('link' => base_url(),
'page' => lang('home')),
array('link' => site_url('sales'),
'page' => lang('products')),
array('link' => '#', 'page' => "sets")
);
$meta = array('page_title' => "Add Sets", 'bc' => $bc);
$this->page_construct('sales/sets', $meta, $this->data);
}
First of all, No need to include the curly braces for $q->result
foreach ($q->result as $row)
{
$data[] = $row;
}
No need to use validation_errors in your php file.You can directly load your form page.Use validation_errors() in view page.
In your Controller, do this
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
Then in your formpage you can echo
<?php echo validation_errors(); ?>
change this line to
$this->data['showcombo'] = $this->load->sales_model->getComboProduct();
this
$this->data['showcombo'] = $this->load->sales_model->getProductCombo();
Because your
model name is
public function getProductCombo()
{
}
Firstly you load model in controller. And then called function, which you have defined in model..
$this->load->model('sales_model','sales'); // sales is alias name of model name
$this->data['showcombo'] = $this->sales->getComboProduct();

Display multi-option customer attribute within customer management admin grid

Okay so with Customer Attributes I have a multi-option selection that I have added to the Manage Customers Grid.
$prodCode = Mage::getSingleton('eav/config')->getAttribute('customer','prod_codes');
$prodCodeOptions = $prodCode->getSource()->getAllOptions(false);
$prodOptions = array();
foreach($prodCodeOptions as $k)
$prodOptions[$k['value']] = $k['label'];
$this->addColumn('prod_codes', array(
'header' => Mage::helper('customer')->__('Product Code'),
'width' => '100',
'index' => 'prod_codes',
'type' => 'options',
'options' => $prodOptions,
'filter_condition_callback'
=> array($this, '_filterProdOptionsCondition'),
));
I do have my attribute added to the collection at the top of my Grid.php:
->addAttributeToSelect('prod_codes')
Here is my _filterProdOptionsCondition method:
protected function _filterProdOptionsCondition($collection, $column) {
if(!$value = $column->getFilter()->getValue()) {
return;
}
$this->getCollection()->addFieldToFilter('prod_codes', array('finset' => $value));
#print($collection->getSelectSql());
}
Now this work fine and dandy if I only have ONE of the options selected, once I apply more than one option to the customers attribute I will get a blank results within the admin grid, however it IS still searchable.
A close look with the print($collection->getSelectSql()); uncommented I see that the attribute ID values are being returned in an comma delimited list.
Now onto my question with that background laid out, is there a method or "Magento" way to display these multi-options within the admin grid I'm just unaware of? Or do I need to simply get the comma values exploded and call for a new collection to build out the display values? Any help appreciated!
Appears I had to extend the Column renderer to anticipate comma values and simply render them, I'm amazed this isn't built in since the functionality exists to create the multioptions attributes but no grid display option for it.
app/code/local/Mage/Adminhtml/Block/Widget/Grid/Column/Renderer/Options.php
public function render(Varien_Object $row)
{
$options = $this->getColumn()->getOptions();
$showMissingOptionValues = (bool)$this->getColumn()->getShowMissingOptionValues();
if (!empty($options) && is_array($options)) {
$value = $row->getData($this->getColumn()->getIndex());
if (is_array($value)) {
$res = array();
foreach ($value as $item) {
if (isset($options[$item])) {
$res[] = $options[$item];
}
elseif ($showMissingOptionValues) {
$res[] = $item;
}
}
return implode(', ', $res);
}
elseif (isset($options[$value])) {
return $options[$value];
} elseif (is_string($value)) { // <--- MY CHANGES HERE
$values = explode(',', $value);
$returnOptions = "";
foreach($values as $k=>$v) {
$returnOptions .= $options[$v]. ", ";
}
return substr($returnOptions, 0, -2);
}
return '';
}
}

Resources