I want to do this:
<?php echo $factuurgegevens->aantal.$i; ?>
But it doesn't work, how can I handle this?
So I have to get:
<?php echo $factuurgegevens->aantal1; ?>
<?php echo $factuurgegevens->aantal2; ?>
<?php echo $factuurgegevens->aantal3; ?>
<?php echo $factuurgegevens->aantal4; ?>
You can use curly braces ({}) to make up the dynamic variables:
$name = 'aantal';
for($i = 0 ; $i < 5 ; $i++)
echo $factuurgegevens->{$name . $i};
However you should really use an array because that is made for things like this:
$factuurgegevens->aantal = array();
$factuurgegevens->aantal[1] = 'something';
$factuurgegevens->aantal[2] = 'something';
$factuurgegevens->aantal[3] = 'something';
Do this:
$name = 'aantal';
for($i = 0 ; $i < 5 ; $i++)
echo $factuurgegevens->{$name . $i};
You can do this by placing your string in a variable.
for($i=0;$i<10;$i++)
{
$var = 'aantal'.$i;
echo $factuurgegevens->$var;
}
Related
I want to print the values of variable but it prints 12 only
$str1="abhishek";
$str2="ashish";
for($i=1;$i<=2;$i++)
{
echo $str.$i;
}
This is a better practice:
<?php
$str = array("abhishek","ashish");
for($i=0; $i <= 2; $i++)
{
echo $str[$i].'<br>';
}
?>
Do this:
$str1="abhishek";
$str2="ashish";
for($i=1;$i<=2;$i++){
echo ${"str".$i};
}
I want to make a form input with dynamic Name Label and Value,
there are two arrays, how to make it loop in single foreach ?
This is an example:
<?php
$value = explode(',',$row['value']);
$name = explode(',',$row['name']);
for($x = 0; $x <= 10; $x++) {
echo $name;
echo $value;
}
?>
$row Variable is an Array,
This method didn't work for me. Any suggestions?
Why cant you use array_combine before loop, try this..
<?php
$value = explode(',',$row['value']);
$name = explode(',',$row['name']);
$combainedArray = array_combine ( $name , $value );
foreach($combainedArray as $name => $value ) {
echo $name, '=>', $value;
}
?>
OR
if you don't what to combine make it like this..
<?php
$value = explode(',',$row['value']);
$name = explode(',',$row['name']);
$count = count($value);
for($x = 0; $x < $count; $x++) {
echo $name[$x];
echo $value[$x];
}
?>
As a solution to above mentioned problem,Please try executing below mentioned code snippet.
In consideration with same no of form fields for name and value
<?php
$value = explode(',',$row['value']);
$name = explode(',',$row['name']);
for($x = 0; $x <= 10; $x++) {
echo $value[$x];
echo $name[$x];
}
?>
i need to call similar variable on shortest way.
$test = new stdClass();
$test->var0=0;
$test->var1=1;
$test->var2=2;
And now i need to echo all 3 variables in for cycle, not like this:
echo $test->var0;
echo $test->var1;
echo $test->var2;
Try this -
for($i = 0; $i < 3; $i++) {
echo $test->{'var' . $i};
}
I got value using this:
$count = <?php $details->shop_image_count; ?>
I want to use this $count in my for loop, but I am not getting the value of count
<?php
for($i=1;$i<($count);$i++){
echo '<li data-target="#shopCarousel" data-slide-to="' . $i . '"></li>';
}
?>
You should use something like this
$count = $details->shop_image_count;
instead of
$count = <? echo $details->shop_image_count;?>
Try this
$count = $details->shop_image_count;
Insted of this
$count = <? echo $details->shop_image_count;?>
And your for loop
<?php
for ($i=1; $i<$count; $i++){
echo '<li data-target="#shopCarousel" data-slide-to="'.$i.'"></li>';
}
?>
No need to close php in the loop. Make your code more readable and maintainable, like below:
<?php
for ($i = 1; $i <= $details->shop_image_count; $i++)
echo '<li data-target="#shopCarousel" data-slide-to="' . $i . '"></li>';
?>
Further, you start with 1, so you want to loop while $i <= $details->shop_image_count
$count = <? echo $details->shop_image_count;?> is not correct syntax.
Try
<?php
$count = $details->shop_image_count;
for($i=1; $i<($count); $i++)
{
?>
<li data-target="#shopCarousel" data-slide-to="<?php echo $i;?>"></li>
<?php
}
?>
$count = $details->shop_image_count;
Then use this method for better application
for($i=1;$i<(intval($count));$i++){
?>
<li data-target="#shopCarousel" data-slide-to="<? echo $i?>"></li>
<?php
}
?>
There is any possibility making something like that using PHP?
<?php
$number = 1;
$str_$number = "BlahBlah";
// and make this: $str_1 = "BlahBlah";
echo $str_1;
?>
<?php
$number = 1;
${'str_'.$number} = 'foobar';
echo $str_1;//foobar
<?php
$number = 1;
$value = 'str_' . $number;
$$value = 'blahblah';
echo $str_1;
?>
Try
<?php
$number = 1;
${'str_' . $number} = 'foobar';
?>
You can also try
$number = 1;
eval('$str_'.$number.' = "foobar";');
echo $str_1;