i already have the output of the items and the amount of the item but now i am trying to print out the price of the item but it is not working.
This is the page of the shoppingcar
<?php
//script opdracht1a.php
session_start();
// lees dit als: in de winkelwagen zitten 3 boeken en 1 pet
$_SESSION['winkelwagen'] = array( 'boek' => 3, 'pet' => 1 );
?>
And at this page i want to print out the products and amounts of 'winkelwagen' but next to it i also need to print out the prices of the products of array $aPrijzen
Here is the second code of the other page
session_start();
$aPrijzen = array ('boek' => 5, 'pet'=> 8 , 'jas' => 10, 'jurk' => 22);
$winkelwagen = $_SESSION['winkelwagen'];
foreach($winkelwagen as $key => $value)
{
for($i = 0; $i < count($aPrijzen); $i++)
if($value == $aPrijzen)
{
echo $key.' '.$value.' '.$aPrijzen[i].'<br>';
}
}
Your code can be simplified to:
$aPrijzen = array ('boek' => 5, 'pet'=> 8 , 'jas' => 10, 'jurk' => 22);
foreach($_SESSION['winkelwagen'] as $key => $value)
{
if(isset($aPrijzen[$key]))
{
echo $key.' '.$value.' '.$aPrijzen[$key].'<br>';
}
}
Related
I have this data:
1CPT5
'stock' => array (
'warehouse' =>
array (
'warehouse_id' => 1,
'name' => 'CPT',
),
'quantity_available' => 5,
)
3JHB21
'stock' = > array (
'warehouse' =>
array (
'warehouse_id' => 3,
'name' => 'JHB',
),
'quantity_available' => 21,
)
and this code that is looping through the above data.
foreach ($row['stock'] as $val) {
foreach ($val['warehouse'] as $warehouse => $name) {
echo $name;
}
echo $val['quantity_available'];
echo '<pre>' . var_export($val, true) . '</pre>';
}
I am trying to display the quantity_available based on the ID of the warehouse (there are 2 warehouses) The first set of data is for warehouse_id 1 with name CPT and number of items 5.
The second set of data is for warehouse_id 3 with name JHB and number of items 21.
'warehouse' is an array and according to what I know you can only loop through an array, print_r or var_dump an array. Why if I then echo $name do I see all the values of the array and how do I isolate them so I can just get the id and based on the id display the quantity associated with that id?
I finally figured it out and the following code displays the number I am after:
foreach ($row['stock_at_takealot'] as $key => $val) {
foreach ($val['warehouse'] as $key => $value) {
if ($value == 1) {
echo $val['quantity_available'];
}
}
};
but now I have another issue where all the values are displayed in one cell instead of two. What is my mistake here please?
<td class='small centerCol'>";
foreach ($row['stock_at_takealot'] as $key => $val) {
foreach ($val['warehouse'] as $key => $value) {
if ($value == 1) {
echo $val['quantity_available'];
}
}
};
"</td>
<td class='small centerCol'>";
foreach ($row['stock_at_takealot'] as $key => $val) {
foreach ($val['warehouse'] as $key => $value) {
if ($value == 3) {
echo $val['quantity_available'];
}
}
};
"</td>
<td class='small centerCol'>"
I have a question about how to make an iteration. I want to place a total row after each item in the array if the next element in the array matches a specific condition. Spesific conditions have logic like this
the data like this
if i request a qty for example = 60 the result i hope like this
you can see
data[2] = 01/03/2020 just took 10 out of 40
$iter = new \ArrayIterator($values);
$sum = 0;
foreach($values as $key => $value) {
$nextValue = $iter->current();
$iter->next();
$nextKey = $iter->key();
if(condition) {
$sum += $value;
}
}
dd($iter);
how to make this logic work on php language/ laravel?
Following logic might help you on your way:
<?php
$stock = [
'01/01/2020' => 20,
'01/02/2020' => 30,
'01/03/2020' => 40
];
showStatus($stock, 'in stock - before transaction');
$demand = 60;
foreach ($stock as $key => $value) {
if ($value <= $demand) {
$stock[$key] = 0;
$supplied[$key] = $value;
$demand -= $value;
} else {
$stock[$key] -= $demand;
$supplied[$key] = $value - ($value - $demand);
$demand = 0;
}
}
showStatus($supplied, 'supplied');
showStatus($stock, 'in stock - after transaction');
function showStatus($arr = [], $msg = '')
{
echo $msg;
echo '<pre>';
print_r($arr);
echo '</pre>';
}
?>
**Output:**
in stock - before transaction
Array
(
[01/01/2020] => 20
[01/02/2020] => 30
[01/03/2020] => 40
)
supplied
Array
(
[01/01/2020] => 20
[01/02/2020] => 30
[01/03/2020] => 10
)
in stock - after transaction
Array
(
[01/01/2020] => 0
[01/02/2020] => 0
[01/03/2020] => 30
)
Working demo
I'm not sure I've understood you correctly but this might help:
$values = [
'01/01/2020' => 20,
'01/02/2020' => 30,
'01/03/2020' => 40
];
$demand = 60;
$total = array_sum($values);
$decrease = $total - $demand; //(20+30+40) - 60 = 30
$last_key = array_keys($values,end($values))[0]; //Is 01/03/2020 in this case
$values[$last_key] -= $decrease; //Decrease value with 30 calulated above
Would output:
Array
(
[01/01/2020] => 20
[01/02/2020] => 30
[01/03/2020] => 10
)
I'm looking for a solution to apply a discount to a value based on an foreach loop of items, my main problem is that if the second item meet the requirements it will apply on it too, so what i want is just to apply to the first of the item that has found with the requirement and then pass to other action.
echo '<pre>';
$requeriment = 299;
$items = array(
array(
'id' => 1,
'price' => 199,
'quantity' => 1
),
array(
'id' => 2,
'price' => 399,
'quantity' => 1
),
array(
'id' => 3,
'price' => 199,
'quantity' => 1
)
);
$flag = false;
foreach($items as $item){
$totalItem = $item['price'] * $item['quantity'];
if($totalItem > $requeriment){
if(!$flag){
$flag = true;
echo 'Disc 1% - ' . $item['id'];
echo "<br>";
}else if($flag){
echo 'Disc 2% - ' . $item['id'];
echo "<br>";
}
continue;
}
echo 'Disc 2% - ' . $item['id'];
echo "<br>";
}
//Ok, it found ID 2 with a value bigger than requirement and the job is done.
//Now if trough loop it not found item that meet the requirement
//it need now to sum values to get the required value to meet the requirement
//and apply a 1% the the 2 items that made that sum, then apply 2% to the rest of ids.
Is there a way to to this in the same loop?
$i=0;
foreach $items as $item{
$i++;
$totalItem = $item->price * $item->quantity;
if($totalItem > $requeriment){
//Apply a discount of 1%
if($i==1){
// for discount 1
}elseif($i==2){
// for discount 2
}
}
As same as another discount.
I want to merge the table row if the date have an same id's. I have some array data like this :
Array
(
[0] => Array
(
[id] => 2
[date] => 05/13/2014
[content] => some contents 1
[act] => act1 act2 act3
)
[1] => Array
(
[id] => 2
[date] => 05/28/2014
[content] => some contents 1
[act] => act1 act2 act3
)
[2] => Array
(
[id] => 7
[date] => 06/04/2014
[content] => some contents 2
[act] => act1 act2 act3
)
)
Then I tried to group its data into the same id by :
if($queryx = $this->mymodel->myfunction($par)){
$datax = $queryx;
}
$i = 0;
foreach ($datax as $idx => $val) {
$dates[$val['id']][$i] = $val['date'].",".$val['id'];
$contn[$val['id']][$i] = $val['content'];
$i++;
}
then :
$j = 0; $y = 0;
echo "<table border='1'>\r\n";
foreach ($dates as $idx => $val) {
$tmpexp = explode(',', $val[$j]);
echo "<tr>\r\n";
echo "<td rowspan='".count($val)."'>".$tmpexp[0]."</td>\r\n";
foreach ($val as $idx1 => $val1) {
if($idx1 > 0)
{
echo "<tr>\r\n";
}
echo "<td>".$dates[$tmpexp[1]][$y]."</td>\r\n";
if($idx1 < 1)
{
echo "<td rowspan='".count($val)."'>".$contn[$tmpexp[1]][$y]."</td>\r\n";
echo "<td rowspan='".count($val)."'>act1 act2 act3</td>";
echo "</tr>\r\n";
}
$y++;
}
$j++;
}
echo "</table>";
There is no problem if the data have at least two child, but if the date only have one child (see data with id's 7) there is show an error because Undefined offset. So how I add some handler if there is only one child on the data. There is my desired result:
Please see this image:
I use the 'read ahead' technique for processing nested loops. It does mean that 'foreach' loops cannot be used as the next record must be read as soon as the current one has been processed. Basically, the last action you do in the loop is read the next record as you are setting it up for the next iteration. Note, you never test when to print a record as that is decided by the structure of the groups. The code loops are the same as the structure of the groups in the data
A 'group' is all the records with the same id.
I assume that the 'content' and 'act' are identical for each entry in the group.
Edited to add 'rowspan' attributes to the appropriate 'td' tags. I suspect css may be easier at this point.
The issue is that the group cannot be displayed until it is known how many entries are in it.
So, i 'buffer' all the records belonging to a group in an array. at the end of the group, it is displayed with the appropriate 'rowspan' attributes in the html.
It is tested on PHP 5.3.18. It includes test data.
<?php /* Q24028866 */
$testData = array(array('id' => 2, 'date' => '05/13/2014', 'content' => 'some contents 2', 'act' => 'act1 act2 act3'),
array('id' => 2, 'date' => '05/28/2014', 'content' => 'some contents 2', 'act' => 'act1 act2 act3'),
array('id' => 7, 'date' => '06/04/2014', 'content' => 'some contents 7', 'act' => 'act1 act2 act3'),
array('id' => 8, 'date' => '06/08/2014', 'content' => 'some contents 8', 'act' => 'act1 act2 act3'),
array('id' => 8, 'date' => '06/09/2014', 'content' => 'some contents 8', 'act' => 'act1 act2 act3'));
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table border='1'>
<thead><th>Date</th><th>Content</th><th>Act</th></thead>
<?php
// use 'read ahead' so there is always a 'previous' record to compare against...
$iterContents = new \ArrayIterator($testData);
$curEntry = $iterContents->current();
while ($iterContents->valid()) { // there are entries to process
$curId = $curEntry['id'];
// buffer the group to find out how many entries it has...
$buffer = array();
$buffer[] = $curEntry;
$iterContents->next(); // next entry - may be same or different id...
$curEntry = $iterContents->current();
while ($iterContents->valid() && $curEntry['id'] == $curId) { // process the group...
$buffer[] = $curEntry; // store all records for a group in the buffer
$iterContents->next(); // next entry - may be same or different id...
$curEntry = $iterContents->current();
}
// display the current group in the buffer...
echo '<tr>';
echo '<td>', $buffer[0]['date'], '</td>';
$rowspan = count($buffer) > 1 ? ' rowspan="'. count($buffer) .'"' : '';
echo '<td', $rowspan, '>', $buffer[0]['content'], '</td>',
'<td', $rowspan, '>', $buffer[0]['act'], '</td>';
echo '</tr>';
for($i = 1; $i < count($buffer); $i++) {
echo '<tr><td>', $buffer[$i]['date'], '</td>';
echo '</tr>';
}
} ?>
</table>
</body>
</html>
The following code will give you a clue what you should do,
$newArray = array();
$counter=0;
foreach($datax as $key=>$val){
$newArray[$val['id']][$counter]['date'] = $val['date'];
$newArray[$val['id']][$counter]['content'] = $val['content'];
$newArray[$val['id']][$counter]['act'] = $val['act'];
$counter++;
}
$newArray = array_map('array_values', $newArray);
I have an array like below, and I want to do the total of values in a specific manner where all values of ADDED_NEW_(.*) {regular expressions}, and similarly other values.
I have only specific vals like ADDED_NEW, ADDED_OLD, and ADD_LATER.
My array is like:
$stats = Array
(
[ADDED_NEW_2012_06_12] => 16
[ADDED_OLD_2012_06_12] => 10
[ADD_LATER_2012_06_12] => 12
[ADDED_NEW_2012_06_11] => 16
[ADDED_OLD_2012_06_11] => 10
[ADD_LATER_2012_06_11] => 12
)
Can you please tell me how can i obtain my result. I don't know how to add such values using regex in php. Please help.
The output I am expecting is $ADDED_NEW = 32 (i.e. 16+16), $ADDED_OLD = 20 (i.e. 10+10) and $ADD_LATER = 24 (i.e. 12+12)
I believe you just want to add values of similar keys in which they all start with ADDED_NEW or ADDED_OLD or ADD_LATER I assume so we can just make 3 counters and just match for those in the key and add to the counters.
I don't know much PHP but using manuals and my knowledge from Python, this is what I mustered up:
<?php
$ADDED_NEW = 0;
$ADDED_OLD = 0;
$ADD_LATER = 0;
foreach ($stats as $key => $value) {
if (preg_match("ADDED_NEW_.*", $key)) { $ADDED_NEW += $value; }
if (preg_match("ADDED_OLD_.*", $key)) { $ADDED_OLD += $value; }
if (preg_match("ADD_LATER_.*", $key)) { $ADD_LATER += $value; }
}
?>
check this out.
<?php
$stats = array(
'ADDED_NEW_2012_06_12' => 16,
'ADDED_OLD_2012_06_12' => 10,
'ADD_LATER_2012_06_12' => 12,
'ADDED_NEW_2012_06_11' => 16,
'ADDED_OLD_2012_06_11' => 10,
'ADD_LATER_2012_06_11' => 12
);
$ADDED_NEW = 0;
$ADDED_OLD = 0;
$ADD_LATER = 0;
foreach ($stats as $key => $value) {
if (preg_match("/ADDED_NEW_.*/", $key)) { $ADDED_NEW += $value; }
else if (preg_match("/ADDED_OLD_.*/", $key)) { $ADDED_OLD += $value; }
else if (preg_match("/ADD_LATER_.*/", $key)) { $ADD_LATER += $value; }
}
echo "$ADDED_NEW - $ADDED_OLD - $ADD_LATER";
?>
outputs:
32 - 20 - 24
Try this:
<?php
$stats = array
(
"ADDED_NEW_2012_06_12" => 16,
"ADDED_OLD_2012_06_12" => 10,
"ADD_LATER_2012_06_12" => 12,
"ADDED_NEW_2012_06_11" => 16,
"ADDED_OLD_2012_06_11" => 10,
"ADD_LATER_2012_06_11" => 12,
);
$accumulators = array
(
"ADDED_NEW" => 0,
"ADDED_OLD" => 0,
"ADD_LATER" => 0,
);
foreach($stats as $key => $value)
{
foreach(array_keys($accumulators) as $accumulator)
{
if(preg_match("#^${accumulator}#m", $key)){$accumulators[$accumulator] += $value;}
}
}
header('Content-Type: text/plain');
print_r($accumulators);
?>