Ingolme Posted September 26, 2011 Share Posted September 26, 2011 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 More sharing options...
chitchirya Posted September 26, 2011 Author Share Posted September 26, 2011 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 More sharing options...
Ingolme Posted September 26, 2011 Share Posted September 26, 2011 Where is it being stored? You have enough data passed in the $_GET['input'] array to store it properly. Link to comment Share on other sites More sharing options...
chitchirya Posted September 26, 2011 Author Share Posted September 26, 2011 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 More sharing options...
Ingolme Posted September 26, 2011 Share Posted September 26, 2011 No, it's not stored. But you have the information you need in order to store it. Where are you storing all your data? Is it a database or a file? Link to comment Share on other sites More sharing options...
chitchirya Posted September 26, 2011 Author Share Posted September 26, 2011 A database. Link to comment Share on other sites More sharing options...
Ingolme Posted September 26, 2011 Share Posted September 26, 2011 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 More sharing options...
chitchirya Posted September 26, 2011 Author Share Posted September 26, 2011 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 More sharing options...
Ingolme Posted September 26, 2011 Share Posted September 26, 2011 $clevel keeps getting reset every time the script runs. Where is $_GET['spa'] coming from? You have to update the place where that originates. Link to comment Share on other sites More sharing options...
chitchirya Posted September 26, 2011 Author Share Posted September 26, 2011 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 More sharing options...
Ingolme Posted September 26, 2011 Share Posted September 26, 2011 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 More sharing options...
chitchirya Posted September 26, 2011 Author Share Posted September 26, 2011 Well, it works now. I have question again, Say my $clevel is 40.I clicked 6 with the combo box ABC, and then the new value of my $clevel is 34.But I suddenly change the value of my combo box ABC to 3, so the new value of my $clevel MUST BE 37. How would I do that? Link to comment Share on other sites More sharing options...
Ingolme Posted September 26, 2011 Share Posted September 26, 2011 You're going to have to store a temporary array of all the keys and their values in that tempdata file, then. You should try to figure some of this out yourself. Link to comment Share on other sites More sharing options...
chitchirya Posted September 26, 2011 Author Share Posted September 26, 2011 I'm having a problem again, with this code I'm getting an error <?php$data = array();$input = $_GET['input'];foreach($input as $sname=>$value){ array_push($data[$sname], $value);}?> the error is, Warning: array_push() expects parameter 1 to be array, null given...... Link to comment Share on other sites More sharing options...
Ingolme Posted September 26, 2011 Share Posted September 26, 2011 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 More sharing options...
chitchirya Posted September 26, 2011 Author Share Posted September 26, 2011 Can I ask how can I print it? I mean the whole array? Link to comment Share on other sites More sharing options...
Ingolme Posted September 27, 2011 Share Posted September 27, 2011 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 More sharing options...
chitchirya Posted September 27, 2011 Author Share Posted September 27, 2011 My problem in this storing data on a text file is that whenever I reload my page, the values are still stored instead of refreshing the values back to 0. I want all other users use this site not just only 1. Link to comment Share on other sites More sharing options...
Ingolme Posted September 28, 2011 Share Posted September 28, 2011 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 More sharing options...
chitchirya Posted September 28, 2011 Author Share Posted September 28, 2011 No I don't. It's purely on the client side. I'm just using database on storing my data to be printed out on the site. So I need to use $_SESSION? Well I'll try it. =) Thanks! Link to comment Share on other sites More sharing options...
Ingolme Posted September 28, 2011 Share Posted September 28, 2011 Why are you using AJAX for this? You could just do it with pure Javascript. Link to comment Share on other sites More sharing options...
chitchirya Posted September 28, 2011 Author Share Posted September 28, 2011 Yes but I'm having hard time on Javascript... I don't know where to start =( If you could teach me where to start, then I'll be glad.... Link to comment Share on other sites More sharing options...
chitchirya Posted September 28, 2011 Author Share Posted September 28, 2011 $file = 'info.text';$input = array();foreach($_GET['input'] as $sname=>$value){$input = json_encode($_GET['input']);file_put_contents($file, $input);}echo $input; I do this but it does not store the values, only the last value I clicked were saved. Link to comment Share on other sites More sharing options...
Ingolme Posted September 28, 2011 Share Posted September 28, 2011 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 More sharing options...
chitchirya Posted September 28, 2011 Author Share Posted September 28, 2011 What my problem now is to encode the values of the array to a file through json_encode. Yes I can now store A VALUE that I have clicked last but not all of what I have clicked... Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.