Jump to content

Storing Values Of List Box To An Array Through For Loop.


chitchirya

Recommended Posts

OK, in that case, you could separate the values using an array element.

xmlhttp4.open("GET","showpoints2.php?input[" + name + "]=" + value +"&spa="+spa);

Just change the foreach loop to get the elements from that specific input:

foreach($_GET['input'] as $sname=>$value) {

Link to comment
Share on other sites

  • Replies 60
  • Created
  • Last Reply

Oh it now works, what I need to do now is to store every input to an array in this foreach loop so that when a user clicks a specific combo box the value and name of it will be stored and when he clicks again the combo box the value should be overwritten. I hope you get it. =) Edit: About the code you had given, What I have done in showpoints2.php is this.

<?php$clevel = $_GET["spa"];foreach($_GET["input"] as $sname=>$value) {  $total += +$value;  $totals = $clevel - $total;}echo $totals;?>

Say I inputted 40 as its $clevel, every click that I will do on SAME COMBO BOX will minus it on the $clevel. Say I click 4, so the output will be 36 and yes it's working, however, when I click OTHER COMBO BOX, its like in the same combo box.When I click 7, instead of making the value be 29, it will make 40-7 so the output is 33 which is wrong. I hope you get it?

Link to comment
Share on other sites

Ohh, so you mean with this code it is already stored? foreach($_GET["input"] as $sname=>$value) {} can you teach me what will I do to make my

$clevel = $_GET["spa"];

be decreased every time a user clicks on combo boxes? Actually this code,

<?php$clevel = $_GET["spa"];foreach($_GET["input"] as $sname=>$value) {  $total += +$value;  $totals = $clevel - $total;}echo $totals;?>

Say my $clevel = 40, I clicked 1 with a combo box named abc so the value now will be 39. And I clicked again 4 with other combo box named abcde so the value should be 35 now, but no, it does not minus it directly to the last value of the $clevel which is 39. I'm getting an output of 36 instead of 35....

Link to comment
Share on other sites

OK. Use the data you have to update the database.

// Connect to the database// Update dataforeach($_GET["input"] as $sname=>$value) {  mysql_query("UPDATE ... ... $name ... ... $value");}

About subtracting from spa, just subtract the value.

$clevel = $_GET['spa'];  foreach($_GET["input"] as $sname=>$value) {  mysql_query("UPDATE ... ... $name ... ... $value");  $clevel -= $value;}// After the loop, you might choose to store $clevel somewhere:mysql_query( ... );

Link to comment
Share on other sites

Ahmm no, I'm not storing the values on the database. I just need this values to subtract to my $clevel (current level). with this code

foreach($_GET["input"] as $sname=>$value) {  $clevel -= $value;}

it's result is the same to what I have done. Whenever I click other combo box instead of subtracting the value to the current level, it subtracts on the oldest value.

Link to comment
Share on other sites

spa comes from my javascript, it is the value that the user input on a text box. I tried to print the code you had given, my print_r($_GET); does not store the values i clicked on combo boxes. Say I inputted 4 for my spa and I clicked 2 on my combo box named abc, the output of the code is Array ( [input] => Array ( [abc] => 2 ) [spa] => 4 )and when I click another which is 4 on my combo box abcdefg, the output of the code is Array ( [input] => Array ( [abcdefg] => 4 ) [spa] => 4 ) instead of having Array ( [input] => Array ( [abc] => 2, [abcdefg] => 4 ) [spa] => 4 ). So this means that it does not store the values I inputted previously.

Link to comment
Share on other sites

Your problem is that every time you send a request, you keep sending $_GET['spa'] again, which never changes. I have a better idea, but you'll need a temporary form of storage.

$file = 'tempdata.dat'; if(file_exists($file)) {  $data = file_get_contents($file);} else {  $data = '';} if(strlen($data)) {  $clevel = intval($data);} else {  $clevel = $_GET['spa'];} foreach($_GET["input"] as $sname=>$value) {  $clevel -= $value;} file_put_contents($file,$clevel);

Link to comment
Share on other sites

You shouldn't need the array_push method. Besides, it only works for numeri arrays, you have an associative array. The correct syntax would be:array_push($data, $value); You should do it this way:$data[$sname] = $value;

Link to comment
Share on other sites

You can print it with print_r(), or you can use implode() to print each value with a separator, or use json_encode() to output it in JSON format (which might be best, because you can store it in a text file and then retrieve it using json_decode()) Now, this needs to be stored in a file. Every time your script runs, you update this array, and the remaining $clevel would be $_GET['spa'] minus the sum of all the elements of the array:

// First extract the $data array using file_get_contents() and decoding it /* ... Code here ... */// Then update the $data array using $_GET['input']/* ... Code here ... */// Finally, obtain $clevel using $_GET['spa'] and the values of the $data array$clevel = $_GET['spa'];foreach($data as $number) {  $clevel -= $number;}// $clevel has been updated

Link to comment
Share on other sites

You're going to have to use a $_SESSION array. In order to reset the values you'll have to ask the user to press a "confirm" button that saves the values and erases the temporary data. Are you storing this into a database? If the user has fields for each of the values then you can store it in their database record instead of a session or file.

Link to comment
Share on other sites

You didn't do it in Javascript because you don't know enough, and yet you tried to do it in PHP instead? PHP is much more compicated than Javascript. You have to break down the problem: Each time a field is changed, update an object with the values. Subtract the sum of the values from the "spa"

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • Create New...