I am a newbie in php, and I have a problem with associative arrays when looped.
The array is an associative array of the form: $myarray = array('id'=>id, 'name'=>name, 'num1'=> 6.5, 'num2'=> 0.385);
It is being passed from a table into arrays dynamically.
<input type='text' name="myArray[<?php echo $row['id']; ?>]" value=''>
$myarray = $_POST['myArray']; // i.e $myarray = $_POST['name'];
Then I loop through to get only the selected table rows from the index page
function addValues($x,$y,$z){return $x+$y+$z;}
foreach($myarray as $key=>$value)
{
$result = mysql_query("SELECT * FROM `test_tbl` WHERE id IN($key)");
if(!empty($value))
{
while ($row = mysql_fetch_array($result))
{
// array number 1
$val[] = array('id'=>$row['id'],
'name'=>$row['name'],
'Number1'=>$row['num1'],
'Number2'=>$row['num2']);
}//end of loop
}//end of condition
This is my problem:
I will like to add $value
(from user input in text box) unto $row['num1']
and $row['num2'] addValues($value, $row['num1'], $row['num2'])// this does not react }
To simplify my question, is it possible to access processed data from a loop(like $row['num1']
and $row['num2']
from the while loop) and use it in a function (such as addValues
) or another loop?
NB: I know mysql_ is deprecated, I am using PDO, but I want to understand the logic before going PDO.