I am using smarty templating engine and am encountering a math issue. I am trying to create a total amount (sum) based on amounts in the array. (Normally I would do this at the server level, but do to the way that I create the array, dont think that is possible.) I am merging two arrays into one, but each array shares an 'Amount' which I am trying to determine the 'Total Amount'
Here are the steps I am taking two arrays pushing into one array:
foreach ($data_main1 as $transaction_main1) {
$json_decoded = json_decode($transaction_main1['NewObject']);
$amount = $transaction_main1['Amount'];
$mycart1[] = array('ParentType' => $ParentType, 'Amount' => $amount);
}
$mycart2=array();
foreach ($data_main2 as $transaction_main2) {
$json_decoded = json_decode($transaction_main2['NewObject']);
$amount = $transaction_main2['Amount'];
$mycart2[] = array('ParentType' => $ParentType, 'Amount' => $amount);
}
$mycart = array_merge((array)$mycart1, (array)$mycart2);
$smarty->assign('cart', $mycart);
Here is my Smarty along with the math equation that does not show the value:
{assign var=last value=$cart[cart].Amount+1}
(I am certainly open to the idea of creating a total amount on the array_merge, just unsure how to do that, or even if it is possible)
Do you know about the {math} Smarty bult-in function?
{math equation="x + 1" x=$cart[cart].Amount}
Let me know if it works.
Related
I have read in a lot of sources that I should use the geofield_compute_values() function when trying to programmatically save coordinates in Drupal.
However it does not work for me, that function is undefined in the Drupal 8.5.2 that I am using.
I've installed geofield using composer and I can use it as usual in the admin area and there are no problems with saving there.
Here are some examples I've tried with, the first example gives me undefined function geofield_compute_values() :
$geofield_data = geofield_compute_values([
'lat' => $lat,
'lon' => $lon,
], GEOFIELD_INPUT_LAT_LON);
$cbisProduct->set('field_koordinater', $geofield_data);
I have also tried this out with no successful result and no errors :
$geofield = [
'geom' => "POINT (" . $lon . " " . $lat . ")",
'geo_type' => 'point',
'lat' => $lat,
'lon' => $lon,
'left' => $lon,
'top' => $lat,
'right' => $lon,
'bottom' => $lat,
];
$cbisProduct->set('field_koordinater', $geofield);
Seems like you're trying to use the geofield_compute_values() function which was available in 7.x version, but not in 8.x
You should look into the wkt_generator service. i.e.
<?php $wktGenerator = \Drupal::service('geofield.wkt_generator'); ?>
I haven't tried this, but something like this should work:
<?php
$point = [
'lat' => $request->get('lat'),
'lon' => $request->get('lon'),
];
$value = \Drupal::service('geofield.wkt_generator')->WktBuildPoint($point);
$node->field_koordinater->setValue($value);
Also, WktGeneratorTest.php and GeofieldItemTest.php files could be a good start to see how to use the service in your implementation.
This function is not available in Drupal 8. You have to rely on the basic GeofieldItem class that extends FieldItemBase. Also, as mentioned by oman, you can use WktGenerator to easily build points, polygons, etc.
Here a working example. Let's say your have an entity $cbisProduct with a multivalued geofield field_koordinater, and you want to set the first item with arbitrary lat/lon coordinates :
// Get geofield item
$geofield = $cbisProduct->get('field_koordinater')->get(0);
// Generate a point [lat, lon]
$coord = ['45.909621', '6.127147'];
$point = \Drupal::service('geofield.wkt_generator')->WktBuildPoint($coord);
// Calling this function will compute values AND assign geodata to the field instance
$geofield->setValue($point);
// You can read the computed geodata from the field
$geodata = $geofield->getValue();
//dpm($geodata);
// Explicitly set field data (needed if $geofield is not a reference)
$cbisProduct->set('field_koordinater', [$geodata]);
// Save entity
$cbisProduct->save();
Under the hood, GeofieldItem::setValue calls another method responsible to directly assign the computed values to the field instance :
# \Drupal\geofield\Plugin\Field\FieldType\GeofieldItem
protected function populateComputedValues() {
/* #var \Geometry $geom */
$geom = \Drupal::service('geofield.geophp')->load($this->value);
if (!empty($geom)) {
/* #var \Point $centroid */
$centroid = $geom->getCentroid();
$bounding = $geom->getBBox();
$this->geo_type = $geom->geometryType();
$this->lon = $centroid->getX();
$this->lat = $centroid->getY();
$this->left = $bounding['minx'];
$this->top = $bounding['maxy'];
$this->right = $bounding['maxx'];
$this->bottom = $bounding['miny'];
$this->geohash = substr($geom->out('geohash'), 0, GEOFIELD_GEOHASH_LENGTH);
$this->latlon = $centroid->getY() . ',' . $centroid->getX();
}
}
Note : You don't necessarily need WktGenerator for building points, as long as you know the geofield type and how geophp should handle it. For example, the following 2 statements are equivalent :
$point = \Drupal::service('geofield.wkt_generator')->WktBuildPoint($coord);
// is equivalent to
$point = GEOFIELD_TYPE_POINT . '(' . implode(' ', $coord) . ')');
But it is safer to rely on the WktGenerator especially with more complex data types.
I'm trying to build a view that shows the most recent (now four but eventually five) years of data in a chart. I'm able to pull the data successfully in a variable the shows the most recent 4 years(I don't have the fifth year in the database yet). In order to break it down for the view and add some calculations on associated arrays, I have created a collection. I can get to the first and last array entity but can not figure out how to break out the middle two arrays.
public function viewAsuFiveYear()
{
$asuFiveYearEnrollments = $this->Enrollments->find('all', [
'contain' => ['Azinstitutions', 'FallFtes', 'FallHeadcounts', 'ProjectedEnrollments',
'FallHeadcounts.Locations', 'FallHeadcounts.StudentTypes', 'FallHeadcounts.ResidentStatuses', 'FallHeadcounts.Campuses',
'FallFtes.Locations', 'FallFtes.StudentTypes', 'FallFtes.ResidentStatuses', 'FallFtes.Campuses']
], ['limit' => 5])->where(['azinstitution_id' => 1])->order(['enrollment_year' => 'DESC']);
$collection = new Collection($asuFiveYearEnrollments);
$yearone = $collection->first();
$yearTwo = $collection->take(1, 1);
$yearFour = $collection->last();
$collection1HC = new Collection($yearone->fall_headcounts);
$onCampusesHc1 = $collection1HC->match(['location_id' => 1 ])->match(['campus_id' => NULL]);
$collection4HC = new Collection($yearFour->fall_headcounts);
$onCampusesHc4 = $collection4HC->match(['location_id' => 1 ])->match(['campus_id' => NULL]);
$this->set(compact('asuFiveYearEnrollments', 'azinstitutions', 'yearone', 'yearTwo', 'yearFour', 'onCampusesHc1', 'onCampusesHc4'));
}
I tried ->take (one array, second position) but it times out with that. Not sure what filter I can use to get to the 2nd or 3rd entity array.
I believe I've figured it out. May not be the best answer but it works. for the second year I skipped the 1st array and grabbed the first array in what was left.
and the same with the third year.
$yearTwo = $collection->skip(1)->first();
$yearThree = $collection->skip(2)->first();
I have an array of ids (eventids) which can be million or billion ids inside the table. I am using a for loop to go through each id and retrieve an item from a dynamo db table using the query operation in PHP. However, because the query operation goes over a large amount of info in my table, it takes too long the query operation to do its work. So, in this case I don't get any result because I get a time out. The following code tries to do this operation, which is failing for large amounts of info. I am wondering if you have any recommendation or suggestions on how to make the query operation faster and doable for large amounts of ids inside the $sfweventarrayRDS array?
for ($j = 0 ; $j < count($sfweventarrayRDS) ; $j++){
$keyconditions = array(
"eventid" => array(
"ComparisonOperator" => ComparisonOperator::EQ,
"AttributeValueList" => array(
array(Type::NUMBER => $sfweventarrayRDS[$j]["eventid"])
)
)
);
$sfweventarrayDynamo = Dynamo::getItems("eventlocation",$keyconditions,$limit);
if (count($sfweventarrayDynamo) > 0){
$timediffepoch = $sfweventarrayDynamo[0]["edatecreated"]["N"] - $sfweventarrayRDS[$j]["edatecreated"];
$timediffstandard = new DateTime("#$timediffepoch");
if ($timediffstandard->format('i') >= 5){
$starttimeepoch = $sfweventarrayRDS[$j]["edatecreated"];
$endtimeepoch = $sfweventarrayDynamo[0]["edatecreated"]["N"];
$starttimestandard = new DateTime("#$starttimeepoch");
$endtimestandard = new DateTime("#$endtimeepoch");
$each_event = array("eventid" => $sfweventarrayDynamo[0]["eventid"]["N"],
"organizationid" => $sfweventarrayRDS[$j]["organizationid"],
"userid" => $sfweventarrayDynamo[0]["userid"]["N"],
"starttime" => $starttimestandard->format('Y-m-d H:i:s'),
"endtime" => $endtimestandard->format('Y-m-d H:i:s'),
"location" => $sfweventarrayRDS[$j]["location"],
"startlatitude" => $sfweventarrayRDS[$j]["latitude"],
"startlongitude" => $sfweventarrayRDS[$j]["longitude"],
"endlatitude" => $sfweventarrayDynamo[0]["latitude"]["N"],
"endlongitude" => $sfweventarrayDynamo[0]["longitude"]["N"]);
array_push($safewalkeventsDynamo, $each_event);
}
}
}
There can be two possible solutions that can improve your query operation:
Use batch get: If you already know event IDs and want to query the db to get more information, then batch get would be ideal. You create a chunk of 100 IDs (max allowed) and call DynamoDB to get information in one go. Repeat until you get all the IDs. Here is the documentation link.
You can increase your tables' read capacity according to your needs. (This will result in paying more money for dynamoDB.)
I have a problem. I don't know how to show the exactly data I want.
My array is like this:
$races[$numRace][$finalPosition]
$races[1][1] = array ('stephan','1:27,895');
$races[1][2] = array ('george', '1:29,075');
$races[1][3] = array ('peter', '1:29,664');
$races[1][4] = array ('benson', '1:29,915');
$races[2][1] = array ('benson', '1:41,113');
$races[2][2] = array ('stephan','1:41,434');
$races[2][3] = array ('george', '1:43,654');
foreach ($races as $v1) {
foreach ($v1 as $v2) {
foreach ($v2 as $v3) {
echo "$v3\n";
}
}
}
This one shows me every data of $race array.
My question is: How can I do for showing just results for race 2?
Important: We don't know how many runners have participated on each race (So, we need a "foreach").
I would like a result like this:
benson
stephan
george
Simply iterate through only the relevant array. Based on your code your foreach should be:
foreach ($races[2] as $pos => $info){
echo $pos.': '.$info[0];
}
I've hard-coded the value 2 in the code but you could easily use a variable (foreach ($races[$raceNum]).
If you don't already know, => makes it possible for us to use both the key and the value as variables in our loop.
I've written a little script that is supposed to create an array that looks something like this:
array(1 => array( 'month' => 'Jan',
'salary' => '01/31/2013',
'bonus' => '02/15/2013'),
2 => array('month' => '',...));
You get the basic idea: the index in the main array are the months (numerically), each holding an array that is to be filled dynamically. The month key depends on the language the user requested, the salary and bonus are assigned the dates when the salary and or bonus was payed. So far no surprizes.
To get the basic structure of that array, I thought this would be the easiest:
$this->months = array_fill_keys(range(1,12), array( 'month' => null,
'salary' => null,
'bonus' => null));
I then fill the array, and things run smoothly all the same, right up until the point where I want to write the data to a file, which I did like so:
private function writeFile()
{
foreach($this->months as $key => $vals)
{
if ($vals['month'] === null)
{//Only filled from date x to date y, some months can be empty
continue;
}
//this seems to raise notices?
if ($vals['salary'] === null)
{
$vals['salary'] = 'Does not apply';
}
fwrite($this->file, implode(',', $vals).PHP_EOL);
}
fclose($this->file);
return $this;
}
The line where I check if the salary is null raises notices: "Warning: Undefined index salary". At the moment I've had to add this to the code:
if (!array_key_exists('salary', $vals) || $vals['salary'] === null)
{
if (!array_key_exists('bonus', $vals) || $vals['bonus'] === null)
{
break;
}
$vals['salary'] = 'Does not apply';
}
To get to the result I need. I have googled this, and stumbled across this bug report, which was last modified 4 years ago (2009-05-08), but the status is still set to "no feedback".
Has anyone else encountered a similar glitch/bug? Or am I missing something here? How can I avoid this issue without the need for all too many if's and function calls without changing my settings (E_STRICT | E_ALL, as it should be).
BTW: I'm running PHP 5.4.7, on Slackware 14. For this little application, I'm using 2 Symfony components (ClassLoader and Console), but since this is part of an object that has nothing to do with Symfony, apart from being loaded through UniversalClassLoader I don't think that's relevant.
Since the bug is said to be PDO related: yes, I am using PDO, but in another class.
I am not sure , but try to use
$this->months = array_fill(1,12, array( 'month' => null,
'salary' => null,
'bonus' => null));
After a couple of var_dumps, I found out what the cause was: The array keys were range(1,12), to determine the which month was I was processing. For that I used a DateTime object in the following fashion:
$date->modify('+ '.$diff.' days');
$key = $date->format('m');
The problem was that the format call returns a string. The goal was to list when the salary and bonus are to be paid. The bonus has to be paid each 15th, or the following Wednesday, if the 15th was a Saturday or Sunday. The salary is to be paid the last day of the month, or the last Friday.
In other words, the bonus pay date is assigned like so:
$key = $date->format('m');
$this->months[$key]['month'] = $date->format('M');
if ($date->format('d') == 15)
{
//find find week-day (15th or following Wednesday)
$this->months[--$key]['bonus'] = $date->format('m/d/Y');
$key++;
//set date to end of month
}
//check week-day, and modify date if required
$this->months[$key]['salary'] = $date->format('m/d/Y');
Because the keys of the $this->months array are numeric, but the format used for $key is a 2-digit string, with leading zeroes, I was having problems.
Each 15th of the month, the $key value is coerced to an integer (decrement/increment operators), but the month is assigned using a string.
The information I provided in my original question was insufficient, sorry for that, but I had just put in an all-nighter. The fix, in the end was pretty easy:
$key = (int) $date->format('m');//cast
I sincerely appreciate all responses, and everyone contributing to the SO community. I would've deleted this question, but if nobody objects, I think I might leave it be as a testimony to my stupidity.