Want to create variable looping like these $H[count] - php

I have problem with my variable, I wanna make variable like these
$H1,H2, until $h48
but I know is kinda difficult, so I wanna change to like these
$H[1],$H[2],$H[3] until $H[48]
these I try with my code:
$loopCount = 1;
$H = array();
for ($loopCount = 1; $loopCount<49; $loopCount=$loopCount + 1)
{
$H[$loopCount] = 0;
}
But it is not working at all. How to make it work?? Any idea??
EDIT RESOLVED : Thanks To Ghost helping me out.
Here my revisi code :
$H = array();
for ($loopCount = 1; $loopCount<49; $loopCount=$loopCount + 1)
{
$H{$loopCount} = 0;
}
And just call $H{1} , is complete work. Thanks again to Ghost you're hero :)

You could try this :
$loopCount = 1;
$H = '';
for ($loopCount = 1; $loopCount<48; $loopCount=$loopCount + 1)
{
${"H" . $loopCount} = 0;
}

Related

PHP for each for items in array with Drupal

I am trying to calculate a value based on a price field in an entity reference field.
I currently have this, which works...
if (isset($entity->field_choose_a_package['und'][0]['target_id'])) {
$package1nid = $entity->field_choose_a_package['und'][0]['target_id'];
$package1 = node_load($package1nid);
$package1price = $package1->field_price['und'][0]['value'];
} else {
$package1price = 0;
}
if (isset($entity->field_choose_a_package['und'][1]['target_id'])) {
$package2nid = $entity->field_choose_a_package['und'][1]['target_id'];
$package2 = node_load($package2nid);
$package2price = $package2->field_price['und'][0]['value'];
} else {
$package2price = 0;
}
if (isset($entity->field_choose_a_package['und'][2]['target_id'])) {
$package3nid = $entity->field_choose_a_package['und'][2]['target_id'];
$package3 = node_load($package3nid);
$package3price = $package3->field_price['und'][0]['value'];
} else {
$package3price = 0;
}
$packagestotal = $package1price + $package2price + $package3price;
$entity_field[0]['value'] = $packagestotal;
However, there could be an unlimited amount of packages added, and rather than me replicate the code for 20+ packages to try and cover my bases, there must be a way I can do a for each loop.
I have tried something like this,
$arr = $entity->field_choose_a_package['und'];
foreach ($arr as &$value) {
if (isset($entity->field_choose_a_package['und'][$value]['target_id'])) {
$package1nid = $entity->field_choose_a_package['und'][$value]['target_id'];
$package1 = node_load($package1nid);
$package1price = $package1->field_price['und'][$value]['value'];
} else {
$package1price = 0;
}
}
unset($value);
but I cant figure out how to increment the variables, or if even need to? Can i just calculate the totals from the foreach?
$packagestotal = 0;
$numPackages = 3;
for($i = 0; $i <= $numPackages; $i++) {
if(isset($entity->field_choose_a_package['und'][$i]['target_id'])) {
${'package' . $i . 'nid'} = $entity->field_choose_a_package['und'][$i]['target_id'];
${'package' . $i} = node_load(${'package' . $i . 'nid'});
$packagestotal += ${'package' . $i}->field_price['und'][0]['value'];
}
}
$entity_field[0]['value'] = $packagestotal;
That should work.
Although, I would recommend that you wrap the package variables in an array rather than using variable variables as then the code would be much more readable and you could access each package attribute using $package[$i]

Make an adittion in php

I have a problem with my addition :
So I have this code :
$total = 0;
foreach(getHistory($this->id) as $history){
$aHistoryFilter['date'] = $history['date'];
$aHistoryFilter['ls'] = $history['ls']);
$aHistoryFilter['montant'] = $history['montant'];
$aHistoryFilter['total_montant'] = $total+$history['montant'];
$aHistory[] = $aHistoryFilter;
}
return $aHistory;
So I want to save in total_montant the last value, but not work and I don't understand why...Can you help me please ? Thx in advance
You should also do:
$total = $total + $history['montant'];
otherwise you do not add anything (since $total=0;)
So you get:
foreach(getHistory($this->id) as $history){
$aHistoryFilter['date'] = $history['date'];
$aHistoryFilter['ls'] = $history['ls']);
$aHistoryFilter['montant'] = $history['montant'];
$aHistoryFilter['total_montant'] = $total+$history['montant'];
$total = $total + $history['montant'];
$aHistory[] = $aHistoryFilter;
}
update your code to be:
$total = 0;
foreach(getHistory($this->id) as $history){
$aHistoryFilter['date'] = $history['date'];
$aHistoryFilter['ls'] = $history['ls']);
$aHistoryFilter['montant'] = $history['montant'];
$total = $total+$history['montant'];
$aHistory[] = $aHistoryFilter;
}
$aHistoryFilter['total_montant'] = $total ;
because in your code you $history['montant'] to $total but you didn't assign the result to $total
Try this:
$total = 0;
foreach(getHistory($this->id) as $history){
$aHistoryFilter['date'] = $history['date'];
$aHistoryFilter['ls'] = $history['ls']);
$aHistoryFilter['montant'] = $history['montant'];
// this adds the $history['montant'] to the $total
$total += $history['montant'];
// this adds the $total to your variable
$aHistoryFilter['total_montant'] = $total;
$aHistory[] = $aHistoryFilter;
}
return $aHistory;

How I get dynamic name?

Good day.
Me need check 42 names, but it very long:
Useally i use next code:
$info_1 = (isset($info['1'])) ? $info['1'] : 0;
$info_2 = (isset($info['2'])) ? $info['2'] : 0;
$info_42 = (isset($info['42'])) ? $info['42'] : 0;
Can i get dinamic name?
ex. i want use code:
for($i=0;$i<43;$i++){
$info_$i = (isset($info[$i])) ? $info[$i] : 0;
}
How aright add $i for $info?
And is it possible?
for($i=0;$i<43;$i++){
$name = 'info_'.$i;
$$name = (isset($info[$i])) ? $info[$i] : 0;
}
Better use an array:
for($i=0;$i<43;$i++){
$info[$i] = (isset($info[$i])) ? $info[$i] : 0;
}
Just another option:
for($i=0;$i<43;$i++){
${"info_$i"} = (isset(${"info_$i"}) ? ${"info_$i"} : 0;
}
You can do it like this
for($i = 0; $i<43; $i++) {
$var_name = "info_{$i}";
$$var_name = isset($info[$i])) ? $info[$i] : 0;
}
But if you really need this kind of stuff you're doing it real wrong and better off converting this long list of variables to an array;

PHP print every second hour?

Is there a easier/better way to get every second hour than this
if(date("H")=='00'){$chart_updates = '|02|04|06|08|10|12|14|16|18|20|22|00';}
if(date("H")=='01'){$chart_updates = '|03|05|07|09|11|13|15|17|19|19|23|01';}
if(date("H")=='02'){$chart_updates = '|04|06|08|10|12|14|16|18|20|21|00|02';}
if(date("H")=='03'){$chart_updates = '|05|07|09|11|13|15|17|19|21|23|01|03';}
if(date("H")=='04'){$chart_updates = '|06|08|10|12|14|16|18|20|22|00|02|04';}
if(date("H")=='05'){$chart_updates = '|07|09|11|13|15|17|19|21|23|01|03|05';}
if(date("H")=='06'){$chart_updates = '|08|10|12|14|16|18|20|22|00|02|04|06';}
if(date("H")=='07'){$chart_updates = '|09|11|13|15|17|19|21|23|01|03|05|07';}
if(date("H")=='08'){$chart_updates = '|10|12|14|16|18|20|22|00|02|04|06|08';}
if(date("H")=='09'){$chart_updates = '|11|13|15|17|19|21|23|01|03|05|07|09';}
if(date("H")=='10'){$chart_updates = '|12|14|16|18|20|22|00|02|04|06|08|10';}
if(date("H")=='11'){$chart_updates = '|13|15|17|19|21|23|01|03|05|07|09|11';}
if(date("H")=='12'){$chart_updates = '|14|16|18|20|22|00|02|04|06|08|10|12';}
if(date("H")=='13'){$chart_updates = '|15|07|19|21|23|01|03|05|07|09|11|13';}
if(date("H")=='14'){$chart_updates = '|16|08|20|22|00|02|04|06|08|10|12|14';}
if(date("H")=='15'){$chart_updates = '|17|09|21|23|01|03|05|07|09|11|13|15';}
if(date("H")=='16'){$chart_updates = '|18|20|22|00|02|04|06|08|10|12|16|16';}
if(date("H")=='17'){$chart_updates = '|19|21|23|01|03|05|07|09|11|13|15|17';}
if(date("H")=='18'){$chart_updates = '|20|22|00|02|04|06|08|10|12|14|16|18';}
if(date("H")=='19'){$chart_updates = '|21|23|01|03|05|07|09|11|13|15|17|19';}
if(date("H")=='20'){$chart_updates = '|22|00|02|04|06|08|10|12|14|16|18|20';}
if(date("H")=='21'){$chart_updates = '|23|01|03|05|07|09|11|13|15|17|19|21';}
if(date("H")=='22'){$chart_updates = '|00|02|04|06|08|10|12|14|16|18|20|22';}
if(date("H")=='23'){$chart_updates = '|01|03|05|07|09|11|13|15|17|19|21|23';}
I need this for google charts and wanted to check if this way is stupid.
1) take the current hour
2) mod2 (there are only two different sets of numbers, odd and even)
3) build array of hours
4) sort array by value
5) split array where the original hour was, and recombine.
$h = date("H");
$line = '';
for($i=0; $i<=24; $i++)
{
if($i % 2 == $h % 2)
$line .= '|' . ($i < 10 ? '0'.$i : $i);
}
One way is to create an array with keys:
$theHour['00'] = '|02|04|06|08|10|12|14|16|18|20|22|00';
Then you can call it like this:
$chart_updates = $theHour[date("H")];
There is also probably a better way to generate this too, but since you already typed it out, its there.. It would just suck if you want to make a change.
Nice code :)
There's actually much easier way to do this in php:
$chars = array();
$start = date("H")+2;
for( $i = 0; $i < 12; $i++){
$chars[] = str_pad( ($start+2*$i)%24, 2, '0', STR_PAD_LEFT);
}
$chart_updates = '|' . implode( '|', $chars);
function helper_add($h,$plus=0){
if($h+$plus > 23){
return $h+$plus-24;
}
return $h+$plus;
}
function helper_10($in){
return $in < 10 ? '0'.$in : $in;
}
function getchartupdates(){
$now = date('G');
for($i=($now%2==0?0:1); $i<=24 ;$i+=2)
$res[] = helper_10(helper_add($now,$i));
return '|'.implode('|',$res);
}
used this to test it !

For loop problem takes the third data as undefined

Controller
for ($x = 1; $x <= $numb; $x++)
{
echo $quanoutput = $this->input->post('quanoutput');
$barcodeoutput = $this->input->post('barcodeoutput');
$productsoutput = $this->input->post('productsoutput');
$outward_date=$this->input->post('outward_date');
$stock=$this->input->post('stock');
$warehouse_id =$this->input->post('warehouse_id');
$request_id =$this->input->post('request_id');
$warehouse=$this->input->post('warehouse');
$buyprice = $this->input->post('buyprice');
if ($productsoutput=='undefined'){
//$flag3 = $this->cartmodel->cartInsert($barcodeoutput,$quanoutput,$buyprice,$stock,$warehouse,$warehouse_id,$request_id,$outward_date);
} else {
$flag3 = $this->cartmodel->cartInsert($barcodeoutput,$quanoutput,$buyprice,$stock,$warehouse,$warehouse_id,$request_id,$outward_date);
}
}
Try starting your for loops at 0. (ie. j=0) and change the <= to just <.

Categories