...

View Full Version : saving array values



kred
01-05-2006, 11:32 PM
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


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

jskaar
01-06-2006, 08:06 AM
Not sure if I understood your problem correctly, but perhaps you should try something like this:



$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.)

Element
01-06-2006, 09:48 AM
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:




// 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";
}

Velox Letum
01-06-2006, 10:07 AM
If you want to store an array easily, use serialize() (http://www.php.net/function.serialize) and unserialize() (http://www.php.net/function.serialize).


// 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.

kred
01-06-2006, 08:39 PM
thanks for the replies, but I didn't explain myself and needs well enough, what I have is a checkbox form that looks like:


<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 --


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

Element
01-06-2006, 08:53 PM
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...

kred
01-06-2006, 10:16 PM
that's it, thank you all

Ken



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum