foreach ($ids as $key => $value) {
$settlement_data = $this->settlement_model->getSettlementData($value);
foreach ($settlement_data as $key_date => $price) {
$temp[] = array($key_date, (float)$price['settlement_price']);
}
$this->load->library('xlsxwriter');
$file = new XLSXWriter();
$first_row = array();
if ($temp) {
$first_row = array_fill(0, count($temp[0]), "");
}
//add one empty row at the beginning of array
array_unshift($temp, $first_row);
//add title row at the beginning of array (above the empty one)
$title = 'test title';
$file->writeSheet(array_values($temp), 'Mytest', $headers, $title);
$file->downloadAsFile('test' . ".xlsx");
die();
}
The sample data for $settlement data is as follows:
array(7571) {
["2017-05-20"]=>
array(4) {
["settlement_price"]=>
string(15) "10.001"
}
["2017-05-21"]=>
array(4) {
["settlement_price"]=>
string(14) "15.726"
}
["2017-05-22"]=>
array(4) {
["settlement_price"]=>
string(15) "15.729352220997"
}
write now only the dates and price of one id is written to the excel file.How can i write the prices of other ids as well corresponding to the date?
example:
Date Price of id1 price of id2
2017-05-20 10.001 20.001
2017-05-21 15.726 70.001
Related
I'm a bit stuck here
I have an array that I'm exporting with laravel excel and I'm basically creating a category row and then all subsequent item rows that belong to that category.
How can I properly add a counter for every array_push to the groupItem array so that I can count every row between the groupItem push and set the row with the category info to bold?
Basically I only want to bold the row that has the data for category_code, category_name and category_desc so I would need to iterate based on the array_push for the category info I believe
I think I would need to set a count, increase count for the categoryItem array_push, store that count in an array and then set those array stored rows to bold?
$allgroupResult= array();
foreach($prices->groups as $group){
$groupItem = array();
$groupItem["category_code"] = $group->category_code;
$groupItem["category_name"] = $group->category_name;
$groupItem["category_desc"] = $group->category_desc;
array_push($allgroupResult, $groupItem);
foreach($group->skus as $sku){
$skuItem = array();
$skuItem["item_code"] = $sku->sku_info->item->item_code;
$skuItem["identifier"] = $sku->sku_info->identifier;
foreach($sku->prices as $price => $amount){
$skuItem[] = $amount;
}
$skuItem[] = strip_tags(html_entity_decode($sku->sku_info->item->desc));
foreach ($sku->sku_info->details as $details) {
$skuItem[] = $details->details1;
$skuItem[] = $details->details2;
$skuItem[] = $details->details3;
}
array_push($allgroupResult, $skuItem);
}
}
$name = 'File Export';
$build = Excel::create($name, function ($excel) use ($allgroupResult) {
$excel->setTitle('File Export');
$excel->sheet('File Export', function ($sheet) use ($allgroupResult) {
$sheet->fromArray($allgroupResult);
// bold the column headers
$sheet->getStyle('A1:'.$sheet->getHighestColumn().'1')->getFont()->setBold(true);
// $count = 2;
// foreach($excelRows as $one){
// $sheet->fromArray($one, null, 'A2');
// $sheet->row($count, function($row) {
// $row->setFontWeight('bold');
// });
// $count += count( $one ) + 1;
// }
// set the width for the columns that are used
$sheet->setWidth('A', 10);
$sheet->setWidth('B', 24);
$sheet->setWidth('C', 20);
$sheet->setWidth('D', 12);
$sheet->setWidth('E', 10);
$sheet->setWidth('F', 16);
$sheet->setWidth('G', 16);
$sheet->setWidth('H', 16);
$sheet->setWidth('I', 16);
$sheet->setWidth('J', 16);
$sheet->setWidth('K', 16);
});
})->download('xlsx');
Why not creating another folded array inside $allgroupResult for each category, so having structure like this:
array(1) {
[0] =>
array(4) {
'category_code' =>
string(13) "category_code"
'category_name' =>
string(13) "category_name"
'category_desc' =>
string(13) "category_desc"
'skus' =>
array(3) {
[0] =>
string(4) "sku1"
[1] =>
string(4) "sku2"
[2] =>
string(4) "sku3"
}
}
}
and then you can just do count($item['skus']) whenever you need to get the number of products in every category. In order to do this , try the following modification to your foreach loop:
foreach($prices->groups as $group){
$groupItem = array();
$groupItem["category_code"] = $group->category_code;
$groupItem["category_name"] = $group->category_name;
$groupItem["category_desc"] = $group->category_desc;
$groupItem["skus"] = array();
foreach($group->skus as $sku){
$skuItem = array();
$skuItem["item_code"] = $sku->sku_info->item->item_code;
$skuItem["identifier"] = $sku->sku_info->identifier;
foreach($sku->prices as $price => $amount){
$skuItem[] = $amount;
}
$skuItem[] = strip_tags(html_entity_decode($sku->sku_info->item->desc));
foreach ($sku->sku_info->details as $details) {
$skuItem[] = $details->details1;
$skuItem[] = $details->details2;
$skuItem[] = $details->details3;
}
$groupItem["skus"][] = $skuItem;
}
$allgroupResult[] = $groupItem;
}
I have a HTML form with multiple inputs.
I have the below php code to get them inputs and put them in an associated array.
However, when dumping the Associated array the value only shows the first letter...
<?php
$valueArray=array
(
"servername"=>'',
"serverlocation"=>'',
"servertype"=>'',
"serverdescription"=>''
);
foreach($valueArray as $key => $value)
{
if (isset($_POST[$key]))
{
$postValue = $_POST[$key];
$actualValue = $postValue;
$valueArray[$key][$value] = $actualValue;
}
}
var_dump($valueArray);
?>
This is what is dumped -
array(4) { ["servername"]=> string(1) "d" ["serverlocation"]=> string(1) "K" ["servertype"]=> string(1) "P" ["serverdescription"]=> string(1) "t" } post
How do i get it to store the whole string, and not just the first letter?
If you want to fill the valueArray with the content of the POST request you have to do this:
$valueArray=array
(
"servername"=>'',
"serverlocation"=>'',
"servertype"=>'',
"serverdescription"=>''
);
foreach($valueArray as $key => $value)
{
if (isset($_POST[$key]))
{
$postValue = $_POST[$key];
$valueArray[$key] = $postValue;
}
}
var_dump($valueArray);
I think you ar wrong with this line:
$valueArray[$key][$value] = $actualValue;
Try this
$valueArray=array
(
"servername"=>'',
"serverlocation"=>'',
"servertype"=>'',
"serverdescription"=>''
);
$postData=array
(
"servername"=>'serverName',
"serverlocation"=>'serverLocation',
"servertype"=>'serverType',
"serverdescription"=>'serverDescription'
);
foreach($valueArray as $key => $value)
{
if (isset($postData[$key]))
{
$postValue = $postData[$key];
$actualValue = $postValue;
$valueArray[$key] = $actualValue;
}
}
var_dump($valueArray);
I'm trying to add the dynamically generated variables $title and $price to the array $list.
This works to a certain degree. The code creates an array with the keys where they have a title and price value.
The price value is correct and different for each key. However, it seems that only the first title result is added, creating the following array (same title untill key 30)
[0]=> array(2) { ["title"]=> string(57) "Gibson Les Paul ** Nr.1 Gibson dealer ** 18 gitaarwinkels" ["price"]=> string(25) " € 300,00 " } [1]=> array(2) { ["title"]=> string(57) "Gibson Les Paul ** Nr.1 Gibson dealer ** 18 gitaarwinkels" ["price"]=> string(25) " € 100,00 " }
Looking at the code I think it is because the first foreach loop is only executing the second one.
I know the correct values for $title are there, because when I isolate the title foreach loop like this:
foreach ($titlehit as $titles) {
$title = $titles->nodeValue;
echo "$title";
}
30 different $title result are displayed
$url = "https://url.com";
$html = new DOMDocument();
#$html->loadHtmlFile($url);
$xpath = new DOMXPath($html);
$titlehit = $xpath->query("//span[#class='mp-listing-title']");
$pricehit = $xpath->query("//span[#class='price-new']");
$list = array();
$i = 0;
foreach ($titlehit as $titles) {
foreach ($pricehit as $prices) {
if ($i >=5 && $i <=35) {
$title = $titles->nodeValue;
$price = $prices->nodeValue;
$list[] = array(
'title' => $title,
'price' => $price
);
}
$i++;
}
}
How can I get the array $list to hold both the correct title and price values? Thanks for your help.
Assuming that for each title, there is one price i.e there is a 1:1 title-to-price ratio in the corresponding arrays, you will only need 1 loop.
$list = array();
$i = 0; // initialize
// Assuming $titlehit AND $pricehit have the same number of elements
foreach ($titlehit as $titles) {
// Assuming $pricehit is a simple array. If it is an associative array, we need to use the corresponding key as the index instead of $i. We'll get to this once you can confirm the input.
$prices = $pricehit[$i];
// Not entirely sure why you need this condition.
if ($i >=5 && $i <=35) {
$title = $titles->nodeValue;
$price = $prices->nodeValue;
$list[] = array(
'title' => $title,
'price' => $price
);
}
$i++;
}
Not exactly enough code here to know for sure but it looks like you need to reset your iterator $i with each iteration of your foreach loop:
$list = array();
foreach ($titlehit as $titles) {
$i = 0;
foreach ($pricehit as $prices) {
if ($i >=5 && $i <=35) {
$title = $titles->nodeValue;
$price = $prices->nodeValue;
$list[] = array(
'title' => $title,
'price' => $price
);
}
$i++;
}
}
i have case where there are a timestamp date contains of date format.
Then i wanted to build a chart that show the number of "clicked" items "per day" ,
//array declaration
$array1 = array("Date" => 0);
$array2 = array("Date" => 0);
$array3 = array("Date" => 0);
$array4 = array("Date" => 0);
$array5 = array("Date" => 0);
$array6 = array("Date" => 0);
$array7 = array("Date" => 0);
$array8 = array("Date" => 0);
//var_dump($array);
foreach ($_sql as $result1) {
$timestamp = $result1['timestamp'];
$itemType = $result1['itemType'];
//separate the time and date
$explodeTime = explode(" ", $timestamp);
$date = $explodeTime[0];
//$arrDate = array($date);
//var_dump($Date);
//if the item type is 1;
if($itemType == 1 ){
//check the existence
if(array_key_exists($date,$array1)){
//if exist increment the click by 1
foreach ($array1 as $key => $value) {
$array1[$key]=$value + 1;
//var_dump($array1);
}
}
//else add new record and set default value as 1
else{
//echo "Insert new Key";
//$array1 = array($date => 1);
//var_dump($array1);
//var_dump($array1);
//exit();
}
}
i wanted to get my array result with this format
array(1) {
["2009-04-17"]=> 211
int(1)
}
array(1) {
["2009-04-18"]=> 1213
int(1)
}
array(1) {
["2009-04-19"]=> 1232
int(1)
}
array(1) {
["2009-04-20"]=> 32312
int(1)
}
so i can get the value of date, then it is easily to convert the data into json, then insert into the Chartjs.
sorry if my question is not clear, because i have just started learn php.
I think this is what you are trying to do:
//array declaration
$array = array();
foreach ($_sql as $result1) {
//separate the time and date
$dateTime = new DateTime($result1['timestamp']);
$date = $dateTime->format('Y-m-d');
//if the item type is 1;
if (1 == $result1['itemType']) {
//check the existence
if (array_key_exists($date, $array)) {
//if exist increment the click by 1
$array[$date]++;
} else {
$array[$date] = 1;
}
}
}
What do you need exactly?
if you need to convert timestamp to date, use format function.
$timestamp=$result1['timestamp'];
$timestamp=new DateTime($timestamp);
$date=$timestamp->format("n.j.Y");
My foreach is entering an array in the second level of my $bad_email array, something like this
["vlley#.rsr.com"] { ["name"]=> "Woo Hilley", ["amount"]=> "125.16"}
["vey#.rsr.com"] { ["name"]=> "Shoo Moo", ["amount"]=> "12.16"}
If I try to enter the same value like ["vlley#.rsr.com"] { ["name"]=> "Woo Hilley", ["amount"]=> "125.16"} again I want it to run specific code. Im not sure how to fix this. The code Im running seems to work when the name is the same but I want the email, name and amount to all match before it fires. Help please
$bad_email = array();
$i = 0;
foreach ($id_array as $key => $id) {
$bad_email[$email][name] =$name;
$bad_email[$email][amount] = $amount;
if ($bad_email[$email][$amount], $bad_email[$email])) {
// DO CODE HERE!!!!
$i++;
}
}
$email, $name, and $amount are all pulled from an api call
This worked...
$bad_email = array();
$temp_email = array();
foreach ($id_array as $key => $id) {
$temp_email =$bad_email;
$bad_email[$email][name] =$name;
$bad_email[$email][amount] = $amount;
if ($bad_email[$email][amount] == $temp_email[$email][amount]){
// DO CODE HERE!!!!
}
}
}