$_GET form variables in a loop - php

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);

Related

How can I sum up values from a json string?

I get the following json data from a database:
[{"type":"Sig","value":"0.0"},{"type":"SH","value":"9.95"},{"type":"COD","value":"6.95"}][{"type":"Sig","value":"0.0"},{"type":"SH","value":"9.95"},{"type":"COD","value":"6.95"}][{"type":"Sig","value":"0.0"},{"type":"SH","value":"9.95"},{"type":"COD","value":"6.95"}][{"type":"Sig","value":"0.0"},{"type":"SH","value":"9.95"},{"type":"COD","value":"6.95"}]
I'm trying to add all value values together, so: 9.95 + 6.95 ... so that I get 67.6 as result.
I tried the below code, but I am getting 16.9 as repeated values.
for ($i = 0; $i <= $count - 1 ; $i++) {
$charge = $service[$i]['charge'];
$serviceValue = json_decode($charge, true);
$totalservice = 0;
foreach ($serviceValue as $key => $value) {
$totalservice += $value['service_value'];
}
echo $totalservice;
}
You can do it like below:-
$jsonObj = json_decode($json); // Decode the JSON to OBJ
// Now loop and find the SUM
$total = 0;
foreach ($jsonObj as $item){
$total =+ $item->value;
}
// Print the SUM
echo "Sum : $total";
Note:- In your code $totalservice beome 0 every time when loop goes to next iteration and that's why you are getting same value repeated time. So do like (what #u_mulder said) :-
$totalservice = 0;
for ($i = 0; $i <= $count-1 ; $i++) {
.....//rest code
}
I have made the below changes. It works fine.
$totalservice = 0;
for ($i = 0; $i <= $count-1 ; $i++) {
$charge = $service[$i]['charge'];
$serviceValue = json_decode($charge, true);
foreach ($serviceValue as $key => $value) {
$totalservice+= $value['service_value'];
}
echo $totalservice;
}
Thanks for the help

Simple For Loop Output

for ($i = $field +1; $i < $field2; $i++) {
echo $i.'<br/>';
}
what i'm trying to do here is Echo numbers between the input values field and field2, this works, however i was wondering if there was a more efficient way of doing this (eg changing the $i = $field +1;)
Another using while loop example:
<?php
$i = $field;
while(++$i < $field2) {
echo $i.'<br/>';
}

Issue splitting an array for use in another array

I have some data, which i currently have hard coded, basically i am trying to split the numbers, 1-port and recreate it as
'INSERT INTO '.$tbl_name.'(PortNumber) VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(145),(146),(147),(148),(149),(150),(151),(152),(153),(154),(155),(156)';
and etc.. i have a pretty good start i think..
PHP
//post vars
$port = 24; //(int)$_POST['ports'];
$super = 2; //(int)$_POST['super'];
$col = 12; //(int)$_POST['columns'];
$row = 2; //(int)$_POST['rows'];
//rest of vars
$halfPort=$port/2;//12
$colHalf = $col / $super;//6
$half=$colHalf-1;//5
$count=1;
$and=1;
$one=1;
echo "<h1>here goes nothen</h1><br><br>";
//sperate port
$finalArray = array();
for ($i = $port; $i >= 1; $i -= $colHalf) {
$tempArray = array();
for ($j = 0; $j < $colHalf; $j++) {
$tempArray[] = $i - $j;
}
$tempArray[]= sort($tempArray);
$finalArray[] = implode(",", $tempArray);
}
$finalArray = array_reverse($finalArray);
echo "<pre>" . print_r($finalArray, true) . "</pre>";
echo "<br><br>";
//sql for insert
$sqlinsert='
INSERT INTO '.$tbl_name2.'
(PortNumber) VALUES ';
$start='(';
$end=')';
//preset b
$b=0;
for($c = $port; $c >= 1; $c -= $colHalf) {
$queryStart = array();
$queryStart[]=explode(',',$finalArray[$b]);
echo "<pre>" ."start". print_r($queryStart, true) . "</pre>";
for($s=0; $s<6; $s+=$and) {
$queryEnd = array();
$queryEnd[] = $start.$queryStart[$s].$end;
echo "<pre>" ."end". print_r($queryEnd, true) . "</pre>";
}
$b+=1;
}
to view it live insert it here: http://phptester.net/index.php?lang=en
baisaclly it gets to $queryEnd, everything goes down hill, any ideas?
I'm not quite sure what you want to do, but if you just want to build something like the INSERT query you can do something like:
$tbl_name = '?';
echo 'INSERT INTO '.$tbl_name.'(PortNumber) VALUES ('.implode('),(', array_merge(range(1,12),range(145,156))).')';
I think I see it. You are reinitializing your $queryEnd array every time the For loop runs. Move that out of the For loop.
Mike

php variable after a variable in for loop

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?

create new variables with a for cycle

Hi I have a list of values named:
$value1
$value2
$value3
...
and I'd like to assign each value to an array element; something like:
$my_array[1]=$value1;
$my_array[2]=$value2;
$my_array[3]=$value3;
How can I do this using a for cycle? The array is not a problem but I can't figure out how to write some code for the value, it should be something like:
for($i=1; $i<=10000; $i++)
{
$my_array[$i]=$value$i;
}
Try this:
for ($i=1; $i<=10000; $i++) {
$val_name = "value" . $i;
$my_array[$i]=$$val_name;
}
You are almost there:
for($i=1; $i<=10000; $i++)
{
$my_array[$i] = $value;
}
Or this, if you want to append the counter as well:
for($i=1; $i<=10000; $i++)
{
$my_array[$i] = $value . $i;
}
What you are looking for are the {}.
$my_array[$i]=${'value'.$i};
for ($i = 1; isset(${"value$i"}); $i++) {
$my_array[$i] = ${"value$i"};
}
This syntax is known as variable variables.
You can use the $$ syntax:
for($i = 1; $i <= 10000; $i++) {
$name = 'value' . $i;
$my_array[$i] = $$name;
}

Categories