Jump to content

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


chitchirya

Recommended Posts

The problem, which you just mentioned earlier, is that this temporary data file is common among all users. One user might modify it while another person is trying to read it and they'll get the wrong values. This should be a job for Javascript. With the json_encode and the other code you had before, it will go adding elements to the stored array as you go clicking on different select boxes.

Link to comment
Share on other sites

  • Replies 60
  • Created
  • Last Reply

The code is supposed to be like this:

$file = 'info.txt';// Extract the array from the file$input = array();if(file_exists($file)) {  $input = json_decode(file_get_contents($file));}// Change or add one element of the arrayforeach($_GET['input'] as $sname=>$value){  $input[$sname] = $value;}// Store the modified arrayfile_put_contents($file, json_encode($input));

But all this is way too complicated for the simple task you want and you're involving the server in tasks it shouldn't have to do, wasting bandwidth and slowing down the page for your users Maybe this project is beyond your caliber. W3Schools has a Javascript tutorial and a PHP tutorial. You're asking far too many questions and writing a lot of logically incorrect code, which means you don't know enough yet and still need to learn more.

Link to comment
Share on other sites

With the code you had posted, there is an error. Fatal error: Cannot use object of type stdClass as array in... Yes, I can say I'm still new to javascript and some sort in php and still learning a lot of things. That is why I'm asking. But I know I'm wrong because I'm asking far too way beyond my knowledge. I'm sorry...

Link to comment
Share on other sites

Oh, yes. The PHP manual says that json_decode() needs a second parameter set to true in order to return an array.

$input = json_decode(file_get_contents($file), true);

The reason I'm telling you this is that maybe you should step down from the project you're working on and try simpler things until you get a better grasp on programming.

Link to comment
Share on other sites

This is sooooo cool!! I made it because of you! Thank you for your caring and guiding in me. God bless this site and God bless all of you. I learned a lot. Thanks! Till then! The only problem I do have now is $_SESSION and I think I can do it this time. Thank you again. =) Have a nice day and God bless!

Link to comment
Share on other sites

Can I ask for the last time, I did not use $_SESSION instead, I used a button named Reset. Here is the code:

if(isset($_POST['Reset'])){  $file = "info.txt";  unlink($file);   echo  "<script LANGUAGE='javascript'> window.location='home.php';</SCRIPT>";}

Would it not affect others when others are using the same file?

Link to comment
Share on other sites

The file is being manipulated at the same time by every user that's on the site. If one user deletes the file, another user will lose their temporary data. A patch-up to this mistake would be to give each user their own file, using their IP to give a unique name to the file. Again, five minutes of Javascript work could get this all done for you without any server-side involvement.

Link to comment
Share on other sites

I come up with this code in javascript.

inputs = {};function showPoint(value,name){inputs[name] = value; for (var name in inputs) {  spa -= inputs[name];}document.getElementById("sp").innerHTML = spa;}

my problem is that it doubles the subtraction of the inputs[name] in my spa. I know I've done something wrong here...

Link to comment
Share on other sites

I'm really glad. Truly a simple code in javascript >,< I'm glad I learned. Thank you Mr. Ingolme, your so kind, nice and intelligent. I come up with this code.

inputs = {};function showPoint(value,name){inputs[name] = value;total = 0;for(var abc in inputs) {  total += Number(inputs[abc]);}

I'm so happy I already got it on my own. Anyway thanks for the help and the codes everyone had posted in this thread espescially for Mr. Ingolme. I got the logic behind my problem because of him. He is awake even on nights till dawn, lol! =). Truly this forum helps a lot of people. God bless all, more power, Have a nice day and learn a lot! =)

Link to comment
Share on other sites

Archived

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


×
×
  • Create New...