one variable changes dynamically depending on previous variable [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i have this div inside loop
what i want to do is
if the first <div> take $a variable the second <div> want it to take $b variable and so on
so basically the structure will be
<?php
$a = '1';
$b = '2';
while(...){
//// if this div take $a variable
//// the next div will take $b variable
//// and the 3rd div take $a variable and so on
echo "<div id='$a OR $b'>This is a Div</div>";
}
?>

Try this,
you need only one variable that changes dynamically.
<?php
$a = '1';
while(...)
{
echo "<div id='$a'>This is a Div</div>";
if($a=='1') $a='2';
else $a='1'
}
?>

Try this:
$a = '1';
$b = '2';
while(...){
//// if this div take $a variable so the next div will take $b variable and the 3rd div take $a variable and so on
echo "<div id='".rand(1, 2)."'>This is a Div</div>";
}
?>

<?php
$a = '1';
$b = '2';
while(...){
if($c == $a) $c = $b;
else $c = $a;
echo "<div id='".$c."'>This is a Div</div>";
}
?>

Related

How to put multiple parameters in one $ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
Sorry if this is very obvios
I have the following
$a = 1200.00
$b = 675
$c = 123.00
$d = $a$b$c
How would I properly write $d
I thought it would be
$d = '$a'.'$b'.'$c'
How ever this is not correct
How can I make it so when asking to echo out $d
<?php echo $d; ?> it shows:
1200.00675123.00
It's not JS, . will not sum up numbers, but convert it to string
$d = $a . $b . $c;
Please note, that you are working with float numbers, so sometimes instead of 1200.00 you can get 1199.999999999999999999998 and outputting it will trim your .00 part.
That's why you need to use number_format() to output floats in format that you want:
function getFloatStr(float $num) {
return number_format($num, 2, '.', '');
}
$d = getFloatStr($a) . $b . getFloatStr($c);
Example
You are using ', hence the $a are not read as variable, but as a constant string:
$d = $a . $b . $c;
Will concatenate the three variables.
And since you declare $a, $b and $c as number, they are evaluated as such:
<?php
$a = 1200.00;
$b = 675;
$c = 123.00;
$d1 = $a . $b . $c;
$d2 = '$a' . '$b' . '$c';
$d3 = $a + $b + $c;
echo '<pre>';
echo "d1: ", $d1, "\n"; // d1: 1200675123
echo "d2: ", $d2, "\n"; // d2: $a$b$c
echo "d3: ", $d3, "\n"; // d3: 1998
echo '</pre>';
If you want to keep the 00, you will need either to use a formatting function, either to use quote:
printf("printf: %.2f%.0f%.2f\n", $a, $b, $d); // printf: 1200.006750.00
Or:
$a = '1200.00';
$b = '675';
$c = '123.00';

How to assign three different value to three the same variables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I don't know if there is a way out of this...... How can three different values be assigned to the same variable differently and echo or print the variable? Just like the code below.
$A = 'A';
$A = 'B';
$A = 'C';
echo $A;
If I echo $A we all know it going to get the last variable, so how am I going to get all the values once.
You could use an array of values like this:
$A = [];
$A[] = 'A';
$A[] = 'B';
$A[] = 'C';
echo $A[0];
...
echo $A[2];
you have to use array not variable, a variable can hold single value at a time.
there is different way to achieve this
$A = ['A', 'B', 'C'];
print_r($A);
OR
$A[] = 'A';
$A[] = 'B';
$A[] = 'C';
print_r($A);
OUTPUT
Array
(
[0] => A
[1] => B
[2] => C
)
OR
$A = 'A';
$A .= 'B';
$A .= 'C';
echo $A;
OUTPUT
ABC
simple and ugly way
<?php
$A = 'A';
$A .= 'B';
$A .= 'C';
echo $A;
OUTPUT
ABC

How To Sum with Foreach php codeigniter [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a problem to sum the values ​​using foreach.
I want to add the value of 12000 to 11000 but the result is only 11000, which is the last data.
$no = 1;
foreach ($record->result_array() as $calc) {
$sum = 0;
$sum += $calc['harga_jual'] - $calc['diskon'];
$no++;
}
The initialization of variable $sum ...is meant to be outside the foreach loop ...
That is $sum = 0 is meant to be outside the foreach loop ...
Note:- The declaration & initialization of $sum = 0; is outside the foreach loop.
<?php
$sum = 0;
foreach ($record->result_array() as $calc)
{
$sum += $calc['harga_jual'] - $calc['diskon'];
}
echo $sum;
?>

How to Adding values of two dimnetional array in php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the below given array
Array
(
[0] => 2,3
[1] => 2,3
)
I want to sum up and get result in a variable. E.g. variable a = 4 (2+2=4) and variable b = 6 (3+3=6). I am coding in php.
Use EXPLODE inside foreach
<?php
$yourArr = array('2,3','2,3');
$a = 0;
$b =0;
foreach ($yourArr as $temp)
{
$tempnew = explode(",",$temp);
$a += $tempnew[0];
$b += $tempnew[1];
}
echo "a = ".$a."<br>";
echo "b = ".$b;
?>
Try this
$a = 0;
$b =0;
foreach ($yourArr as $temp)
{
$a += $temp[0];
$b += $temp[1];
}
echo "a = ".$a."<br>";
echo "b = ".$b;
Output
a = 4
b = 6

How to Combine the two array's when the checkbox is checked [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have two array and two check boxes. The array values are
$a={1,2,3,4,5} --->It' for first Check box
$b={5,6,7,8,9} --->It' for Second Check box
When I check the first check box only I want the result is
$c[0]=1,$c[1]=2,$c[2]=3,$c[3]=4,$c[4]=5
And I check the second check box only I want the result is
$c[0]=5,$c[1]=6,$c[2]=7,$c[3]=8,$c[4]=9
And I have check both check boxes I want the result is
$c[0]={1+5},$c[1]={2+6},$c[2]={3+7},$c[3]={4+8},$c[4]={5+9}
It's Possible?
You can achieve this using a for() loop. Please note that this code assumes both arrays are the same size:
$c = array();
for($i = 0; $i < count($a); $i++)
{
$c[] = ($a[$i] + $b[$i]);
}
print_r($c);
Yes you can do it following way
<?php
$a = array(1,2,3,4,5);
$b = array(5,6,7,8,9);
$c = array(); //for result
if(isset($_POST['checkbox_1'])){
$c = $a;
}
else if(isset($_POST['checkbox_2'])){
$c = $b;
} else if(isset($_POST['checkbox_1']) && isset($_POST['checkbox_2'])){
if(count($a) == count($b)){
for($i=0;$i<count($a);$i+}){
$c[]=$a[i] + $b[i];
}
}
}else{
echo 'Error Contact Admin';
}
?>

Categories