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];
}
?>
Related
How I can create string (like I tried in modules_for) to get foreach from $g_module_1?
Current echo saying: _1 and _2
Should be: apple_1 and banana_2
$g_module_1 = 'apple';
$g_module_2 = 'banana';
for ($i = 1; $i <= 2; $i++) {
$modules_for = $g_module.'_'.$i;
foreach ((array)$modules_for as $m_foreach_as) {
echo $modules_for;
}
}
You are searching for Variable variables.
In your case it would be echo $$modules_for; - with another fix before: $modules_for = 'g_module_'.$i;
So the whole code would be
<?php
$g_module_1 = 'apple';
$g_module_2 = 'banana';
for ($i = 1; $i <= 2; $i++) {
$modules_for = 'g_module_'.$i;
//foreach ((array)$modules_for as $m_foreach_as) {
echo $$modules_for;
//}
}
I'm not sure what your intention with the foreach was, it doesn't really make sense here, so I commented it out..
A fiddle can be found here: https://3v4l.org/nsYq2
Sidenote: This looks like you could be better off with an array.
I have problem assigning values to the $word as array so that I can make it as a variable.
I am displaying an output of
1,000,000,000 they have all different echos; I want to make them as 1 value only example:
$output = $arramt[1]="1",$arramt[2]="000",$arramt[3]="000",$arramt[4]="000"
function mnyFmt($word){
$n = strlen($word);
if ($n%3==0){
$i = $n/3;
$x=0;
while($x<$i-1){
echo substr($word,-$n,3).",";
$arramt[$x] = substr($word,-$n,3).",";
$x++;
$n = $n-3;
}
echo substr($word,-$n,3);
$arramt[$x] = substr($word,-$n,3).",";
}
elseif($n%3==1){
$word1 = substr($word,1,$n);
$w1 = strlen($word1);
$i = $w1/3;
echo substr($word,0,1).",";
$x=0;
while($x<$i-1){
echo substr($word1,-$w1,3).",";
$arramt[$x] = substr($word1,-$w1,3).",";
$x++;
$w1 = $w1-3;
}
echo substr($word1,-$w1,3);
$arramt[$x] = substr($word1,-$w1,3).",";
echo array_values($arramt[$x]);
}
elseif($n%3==2){
$word1 = substr($word,2,$n);
$w1 = strlen($word1);
$i = $w1/3;
echo substr($word,0,2).",";
$x=0;
while($x<$i-1){
echo substr($word1,-$w1,3).",";
$x++;
$w1 = $w1-3;
}
echo substr($word1,-$w1,3);
}
}
$amount = 1000000000;
mnyFmt($amount);
}
Thanks very much in advance.
You don't need to make any of the custom function you can simply use number_format and explode function like as
$amount = 1000000000;
print_r(explode(',',number_format($amount)));
Demo
I've got a form with input names like price1, price2, price3 and I'm trying to get these values in another page. I'm sending it using GET.
for ($i = 1; $i < $qtd_itens; $i++) {
$price = $_GET['price" + $i + "'];
echo $price;
}
How should I declare the $price variable?
Need to put . in place of + sign...
for ($i = 1; $i < $qtd_itens; $i++) {
$price = $_GET['price'. $i. ''];
echo $price;
}
If you just want to collect all the prices you can do:
$prices = array();
for ($i = 1; $i < $qtd_itens; $i++) {
$prices[] = $_GET["price" . $i];
}
var_dump($prices);
To clarify:
$arr[] = "a";
is shorthand for:
array_push($arr, "a");
or
$arr[$i] = "a";
If you form input having price1, price2, price3... etc, you simply make this for view content of data send:
print_r($_REQUEST)
This show you data in array send, and collect with foreach with this:
$prices = array();
foreach ($_REQUEST as $key=>$val) {
$prices[$key] = $val;
}
print_r($prices);
I need a way to do this
for($i=1;$i<=10;$i++){
$id$i = "example" . $i;
}
notice the second line has $id$i
so for the first loop $id1 will equal example1
in the second loop $id2 will equal example2
and so on...
Thank you very much!
You can use variable variable names to do this; however, it's probably much more convenient if you just used an array:
for($i = 1, $i <= 10, $i++) {
$id[] = "example" . $i;
}
You can convert a string into a variable (the name of the variable), if you put another $ in front of it:
$str = "number";
$number = 5;
$$str = 8;
echo $number; // will output 8
So in your example, you could do it like that:
for($i = 1; $i <= 10; $i++) {
$str_var = "id".$i;
$$str_var = "example".$i;
}
It would be much better to use an array, but you could do this:
for($i=1; $i<=10; $i++){
$var ="id$i";
$$var = "example" . $i;
}
Here's what I would recommend doing instead:
$ids = array;
for($i = 1; $i <= 10; $i++) {
$ids[$i] = "example" . $i;
}
You could create an array of size $i with a name of $id, and insert each element into a different index.
for($i=1;$i<=10;$i++){
$id[$i] = "example" . $i;
}
$var = array();
for($i=1; $i<=10; $i++) {
$var['id' . $i] = 'example' . $i;
}
extract($var, EXTR_SKIP);
unset($var);
but why not use a simple array?
Please check my code below,it returns 0 while I am expecting a result 14.But when I add A+D manually it returns 5.Am i doing something wrong inside the loop ?
<?php
define('A',1);
define('B',2);
define('C',3);
define('D',4);
define('E',5);
//echo A+D; returns 5
$name = 'EACE';
$len = strlen($name);
for($i = 0; $i<=$len; $i++)
{
$val += $name[$i];
}
echo $val; //returns 0
?>
You need to use constant(..) to get the value of a constant by name. Try this:
for ($i = 0; $i < strlen($name); $i++) {
$val += constant($name[$i]);
}
define('A',1);
define('B',2);
define('C',3);
define('D',4);
define('E',5);
//echo A+D; returns 5
$name = 'EACE';
$len = strlen($name);
$val = null;
for($i = 0; $i<=$len-1; $i++)
{
$val += constant($name[$i]);
}
echo $val;