How to pass all arrays into formArray maatwebsite-laravel-excel - arrays

im getting all the array what i need. but when i don't know how to pass all the arrayes
$data = array('JOB NO','INVOICE NO','CUSTOMER','VAT(OUT PUT)','ACTUAL VAT');
foreach($jobs as $row){
$client =$row->company->name;
if($row->company->name != "PSL"){
$client = $row->job->customer_name;
}
$data1[]=array($row->job_no,$row->invoice_no,$client,number_format(($row->amount)*15/115,2,'.',','));
}
// dd($data1);
$data3 = array($data,$data1[0]);
$sheet->fromArray($data3,null,'A5',false,false);
when i check here like this $data3 = array($data,$data1[0]); im getting one row in my Excel i don't know how to pass all the array ?

Here's what you can do.
// Array that will be used to generate the sheet
$sheetArray = array();
// Add the headers
$sheetArray[] = array('JOB NO','INVOICE NO','CUSTOMER','VAT(OUT PUT)','ACTUAL VAT');
// Add space between headers and results if needed
$sheetArray[] = array(); // Add an empty row
$sheetArray[] = array(); // Add an empty row
// Add the results
foreach($jobs as $row){
$client = $row->company->name;
if($row->company->name != "PSL"){
$client = $row->job->customer_name;
}
$sheetArray[] = array($row->job_no,$row->invoice_no,$client,number_format(($row->amount)*15/115,2,'.',','));
}
// Generating the sheet from the array
$sheet->fromArray($sheetArray);

Related

How can I Paginate data which are in array?

I am using Laravel 5.7. I construcet an array like below:
$games = GameResource::collection(Game::all());
$clientGames = array();
foreach ($games as $game) {
if (!$game->user->inRole('admin')) {
array_push($clientGames, $game);
}
}
How can I paginate this array in Laravel?
I used below way to solve my question:
$per_page = !empty($_GET['per_page']) ? $_GET['per_page'] : 10;
$currentPage = LengthAwarePaginator::resolveCurrentPage();
$clientGamesCollection = collect($clientGames);
$currentPageItems = $clientGamesCollection->slice(($currentPage * $per_page) - $per_page, $per_page)->all();
$paginatedItems= new LengthAwarePaginator($currentPageItems , count($clientGamesCollection), $per_page);
$paginatedItems->setPath($request->url());
$pagination = $paginatedItems;

Symfony - saving array to database

I wrote a function where I get array of marks that i need to post to my database..
My function stores it in a filed row like:
And I need to pull just one per column individually like:
Here is my api call..
public function generate(Map $seatMap)
{
$layout = $seatMap->getSeatLayout();
$seats = [];
$layoutArray = json_decode($layout, true);
$columns = range('A', 'Z');
foreach($layoutArray as $index => $result)
{
$columnLetter = $columns[$index];
$letters = str_split($result);
$letterIndex = 1;
foreach($letters as $letterIndex => $letter) {
switch($letter) {
case 'e':
$seats[] = $columnLetter . $letterIndex;
$letterIndex++;
}
}
}
foreach($seats as $seat => $result) {
$result = new Seat();
$result->setName(json_encode($seats));
$this->em->persist($result);
$this->em->flush();
}
}
Any suggestions?
I think that problem is in the part where I need to store it to database..
If I understand you correctly, your issue is here:
foreach($seats as $seat => $result) {
$result = new Seat();
$result->setName(json_encode($seats));
$this->em->persist($result);
$this->em->flush();
}
}
You're indeed creating new Seat instance for every seat, but in this line:
$result->setName(json_encode($seats));
you still assign all (encoded) seats to every instance of Seat. What you want is to assign only the seat from current loop iteration, which is represented by $result variable.
So try with:
$result->setName($result);
You do not need json_encode here too.
If your array is like you say it it then try this
foreach($seats as $seat) {
$result = new Seat();
$result->setName($seat);
$this->em->persist($result);
$this->em->flush();
}

cakephp looping only save first row of data, others row not saving

Looping to save each column field picture1pic6:
Please help, i using cake php version 2.0, try to do looping to save each column, but only the first row data save, the data should be save like attached picture1.Hope someone can help, i tried to figure out 2 weeks. V_COUNT i hardcode the number to 23.
if($this->IPI->save($this->request->data))
{
$table_name = 'IPI_V';
$this->IPI->setSource($table_name);
for($i=1;$i<=$this->request->data['IPI']['V_COUNT'];$i++)
{
if($i<10){$i = '0'. $i;}
if(($this->request->data['IPI']['Quantity'.$i])!=NULL)
{
$this->request->data['IPI']['Type_Defect'] = $this->request->data['IPI']['Type_Defect'.$i];
$this->request->data['IPI']['CAT'] = $this->request->data['IPI']['CAT'.$i];
$this->request->data['IPI']['Defect'] = $this->request->data['IPI']['Defect'.$i];
$this->request->data['IPI']['Quantity'] = $this->request->data['IPI']['Quantity'.$i];
$this->IPI->create();
$this->IPI->save($this->request->data);
}
}
try this
if($this->IPI->save($this->request->data))
{
$table_name = 'IPI_V';
for($i=1;$i<=$this->request->data['IPI']['V_COUNT'];$i++)
{
$data = array();
if($i<10){$i = '0'. $i;}
if(($this->request->data['IPI']['Quantity'.$i])!=NULL)
{
$data['IPI']['Type_Defect'] = $this->request->data['IPI']['Type_Defect'.$i];
$data['IPI']['CAT'] = $this->request->data['IPI']['CAT'.$i];
$data['IPI']['Defect'] = $this->request->data['IPI']['Defect'.$i];
$data['IPI']['Quantity'] = $this->request->data['IPI']['Quantity'.$i];
$this->IPI->setSource($table_name);
$this->IPI->create();
$this->IPI->save($data);
}
}

How to save custom datetime in cakephp 3 for created & modified field?

I'm trying to save the following data. But it's not saving the datetime fields.
Created is saving current datetime in cakephp way & the others are null.
How can I save custom datetime?
Thanks in advance
foreach ($passwords as $key => $value ) {
$data['id'] = $collection_id;
$data['name'] = $value['password'];
$data['collection_id'] = $collection_id;
$data['photo_count'] = $value['photos_count'];
$data['download_count'] = $value['downloaded_users_count'];
$data['created'] = $value['created'];
$data['modified'] = $value['modified'];
$data['expires_at'] = $value['expires_at'];
$saveData[] = $data;
}
$passwords = $this->Passwords->find('all')->toArray();
$patched = $this->Passwords->patchEntities($passwords, $saveData);
foreach ($patched as $entity) {
$this->Passwords->save($entity);
}
I've fixed the Issue. Just have to use as follows-
$data['created'] = new Time($value['created']);
$data['expires_at'] = new Time($value['expires_at']);
And to do so, have to use this.
use Cake\I18n\Time;

Add an array to a session array

I have an array from a MySQL query of ID numbers that I'm trying to add to a Session Array that is already created. For some reason, my code is adding an array inside the Session Array already in place rather than just adding the ID numbers to the Session. What is causing this to happen?
Here is my PHP...
//Find members of this group and create an array to add to cart
$deletedgroupmembersquery = "SELECT * FROM groupmember WHERE group_id='$groupid'";
$deletedgroupmembers = mysql_query($deletedgroupmembersquery) or die('SQL Error :: '.mysql_error());
if (mysql_num_rows($deletedgroupmembers) > 0) {
$groupmembers = mysql_num_rows($deletedgroupmembers);
$cart = array();
while(($deletedmembersrow = mysql_fetch_assoc($deletedgroupmembers))) {
$cart[] = $deletedmembersrow['contact_id'];
}
//Add the array to the cart session
if (isset($cart)) {
$_SESSION['cart'] = array();
array_push($_SESSION[cart],$cart);
} else {
}
Here is the session the above code is creating..
Array ( [cart] => Array ( [0] => Array ( [0] => 1362 [1] => 1371 [2] => 2241 ) )
Thanks for any help.
$cart is already an array. When you do:
array_push($_SESSION[cart],$cart);
you're pushing it as a sub-array of $_SESSION['cart']. I think you just want:
$_SESSION['cart'] = $cart;
You're defining $cart as an array before you push it into $_SESSION[cart]...so you're pushing an array into an array. Try something like this:
if (!empty($cart)) {
$_SESSION['cart'] = array();
foreach ($cart AS $item) {
array_push($_SESSION['cart'], $item);
}
}
You can also just place the array_push inside your while(), and stick the $_SESSION['cart'] = array() before the while(), and it will achieve the same thing.

Resources