Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Apr 2004
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts

    saving array values

    Ok, I have a checkbox[array] in a form, when it is submitted I what to save/collect all the values of checkbox[array] and save it as a variable to written to a flat file.

    what I have is
    Code:
    if ($_POST['CompToInclude']){
    	while (list ($key,$val) = each ($CompToInclude)) {
    	echo "$val";
    
            //write update to email
    	$fp = fopen('../somefolder/text.txt','w');
    	fwrite($fp, $val); //only write last value
    	fclose($fp);
    	} 
    }
    how can I get $val to read like -- echo "$val"; -- from line 3

    thanks for helping out this chump!
    Ken

  • #2
    New Coder
    Join Date
    Jan 2006
    Location
    Oslo, Norway
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Not sure if I understood your problem correctly, but perhaps you should try something like this:

    PHP Code:
    $tmp_vals "";
    while (list (
    $key,$val) = each ($CompToInclude)) 
    {
        
    $tmp_vals .= $val;


    //write update to email
    $fp fopen('../somefolder/text.txt','w');
    fwrite($fp$tmp_vals); 
    fclose($fp); 
    Important to not have fopen inside the while statement, I think. This would open the file too many times.

    (I'm not sure, but I think using 'w' just writes to the file, from the start of it. On the other hand, 'a' appends stuff at the end fo the file.)

  • #3
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by kred
    Ok, I have a checkbox[array] in a form, when it is submitted I what to save/collect all the values of checkbox[array] and save it as a variable to written to a flat file.

    what I have is

    how can I get $val to read like -- echo "$val"; -- from line 3

    thanks for helping out this chump!
    Ken
    if you mean, you want to retrieve it as an array again, or variables, you would do something similar to:

    PHP Code:

    // Format information for storage...
    foreach($CompToInclude as $key =>  $val) {
      ${
    $key} = $val;
    }
    $formatedData $data1 "|" $data2 "|" $data4// And so on for each form value...

    // Retrieving information from storage...

    $CompToInclude explode("|"$storedLine); //  $storedLine would be the content from the txt file

    foreach($CompToInclude as $key $val) {
      echo 
    "<b>".$key."</b> ".$val."<br />\n";


  • #4
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you want to store an array easily, use serialize() and unserialize().

    PHP Code:
    // Define our array
    $array = array('One''Two''Three''Four''Five');

    // $serialized is set to a:5:{i:0;s:3:"One";i:1;s:3:"Two";i:2;s:5:"Three";i:3;s:4:"Four";i:4;s:4:"Five";} which is safe to store in a file or database
    $serialized serialize($array);

    // Reform array from the string
    $reformed_array unserialize($serialized);

     
    // Output array
    print_r($reformed_array); 
    From this example we can create workable code to solve your problem. We serialize $_POST['CompToInclude'] and store it in a file. The only problem we could run into would be separators if you were storing more elements in the file. In this case we would use something unique so that it'd be very unlikely to show up normally, and to be sure use a str_replace() on any data to ensure it doesn't show up.
    Last edited by Velox Letum; 01-06-2006 at 09:16 AM.
    "$question = ( to() ) ? be() : ~be();"

  • #5
    New Coder
    Join Date
    Apr 2004
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for the replies, but I didn't explain myself and needs well enough, what I have is a checkbox form that looks like:
    Code:
    <input type=checkbox name=CompToInclude[] value=1>1
    <input type=checkbox name=CompToInclude[] value=2>2
    <input type=checkbox name=CompToInclude[] value=3>3
    .... for many many lines ....
    what I need to do is collect all the values of CompToInclude to a string like 123... , than write that string to a text file, so it is written as 123..., I don't need to retrieve it, just write it, which this is almost doing. but it was only giving the last value, --- and I wasn't get $val outside the while loop that's why I had the fopen there , I moved it thanks --
    Code:
    if ($_POST['CompToInclude']){
    	while (list ($key,$val) = each ($CompToInclude)) {
    	echo "$val"; // echos 123.. which is how I need it
    }
            //write update to email
    	$fp = fopen('../somefolder/text.txt','w');
    	fwrite($fp, $val); //only write last value
    	fclose($fp);
    	 
    }
    I'm guessing it a simple loop to collect the data, but I have trouble with them, must be a mental thing. so my question is how do I save all the values of CompToInclude[] to a variable and write that to the text file so it looks like 123..., I hop that explains it better,

    Thanks again
    Ken

  • #6
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    PHP Code:
    if ($_POST['CompToInclude']) {
                 
    $tmp "";
                 while (list (
    $key,$val) = each ($CompToInclude)) {
                   
    $tmp .= $val;
                 }

                 
    //write update to email
                
    $fp fopen('../somefolder/text.txt','w');
                   
    fwrite($fp$tmp); //only write last value
                   
    fclose($fp);

    Try that, that will go through the array and add it to one string...

  • #7
    New Coder
    Join Date
    Apr 2004
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts
    that's it, thank you all

    Ken


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •