|
-
Dec 22nd, 2006, 02:37 PM
#1
Thread Starter
Hyperactive Member
How to work with this array?
Because of the way I am making my form (ie. with PEAR HTML_Quickform), I am getting my checkbox/textarea contents in an array as follows:
Code:
Array ( [1] => 1 [txt1] => box1 [2] => 2 [txt2] => box2 [3] => -1 [4] => -1 [5] => -1 [6] => -1 [7] => 7 [txt7] => box7 [9] => -1 )
That looks confusing so I will explain it as best I can.
The array is made up of [checkbox][textarea] etc.
When the checkbox is not checked however it's value is set to -1. And it will obviously have no corresponding [textarea] value either.
So in the above code posted, checkbox 1 is checked, and txt1 has contents 'box1'. Hope your following...
Well this has caused me endless hassles and I haven't been able to figure out how to change the array so it is easier to work with.
What would be best is for each checkbox which is checked, I can create some sort of an array with the textarea contents as its values.
Something like:
Code:
1 = box1
2 = box2
7 = box7
No doubt many of you will be confused about the array because that's what I've got so far but as far as I know that is how the Quickform package is passing me them. (I have just used print_r on the data it passes)...
Can anyone out there give me a clue or an idea on how to get the desired output 
JUST REMEMBERED: What I will be doing with this array is putting it into my table. So '1' will be saved in an 'id' field and 'box1' into a 'comment' field. Hope that makes sense.
Maybe you guys know of a better way using the above Array???
I tried using 'foreach' but obviously I only want to cycle through the checkbox arrays and add them if they are checked etc... I know it sounds confusing
Last edited by wwwfilmfilercom; Dec 22nd, 2006 at 02:40 PM.
Reason: Remembered something
-
Dec 22nd, 2006, 04:08 PM
#2
Re: How to work with this array?
since I don't know how you're building your array, I can't offer you help on how to make it better. however, I can offer you help on how to use the current array you want in the way that you want to use it. you can actually use foreach, you just have to filter out what you don't want and then use what you did want. here's an example, including a sample rebuild of the array you posted:
PHP Code:
<?
//build sample array
$array = array(1 => 1,
"txt1" => "test_txt1",
2 => 2,
"txt2" => "test_txt2",
3 => -1,
4 => -1,
5 => -1,
6 => -1,
7 => 7,
"txt7" => "test_txt7",
8 => -1,
9 => -1);
foreach($array as $key => $value){
if(is_numeric($key) && $value == $key){
//your checkbox was checked, do whatever you want to do
//to reference the textbox corresponding to THIS key, use:
// $array['txt' . $key];
}
}
?>
this is untested, but it should be fine, and probably gives you somewhere to start.
hope that helps.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|