steveh62 Posted November 11, 2008 Share Posted November 11, 2008 I have the following in a form...a select box and a text input box per line. each checkbox is really associated to a textbox - thats what I need really...anyway what I do need is to pull the data from both the checkbox array and each text input, so each matches up to its partner...basically its a size price relationship whereby I can apply a unique price to a size. regards steve <label>200mm x 200mm</label><input name="sizes[]" type="checkbox" value="200 x 200" checked="true"><input name="" type="text" class='dropdown'><br/> <label>200mm x 300mm</label><input name="sizes[]" type="checkbox" value="200 x 300" ><input name="sprice[]" type="text" class='dropdown'><br/> <label>200mm x 400mm</label><input name="sizes[]" type="checkbox" value="200 x 400" ><input name="sprice[]" type="text" class='dropdown'><br/> <label>200mm x 500mm</label><input name="sizes[]" type="checkbox" value="200 x 500" ><input name="sprice[]" type="text" class='dropdown'><br/> <label>200mm x 600mm</label><input name="sizes[]" type="checkbox" value="200 x 600" ><input name="sprice[]" type="text" class='dropdown'> Quote Link to comment Share on other sites More sharing options...
Adam Posted November 11, 2008 Share Posted November 11, 2008 I'm not sure about this, but can you not use: <label>200mm x 200mm</label><input name="sizes['size']" type="checkbox" value="200 x 200" checked="true"><input name="sizes['sprice']" type="text" class='dropdown'><br/> Never tried so.. Adam Quote Link to comment Share on other sites More sharing options...
steveh62 Posted November 11, 2008 Author Share Posted November 11, 2008 cheers for an answer at least...but I need it as is as it is going to poulate to fields in mysql size and price (price can be variable thats why its empty)...I am using them to apply same size ranges but a price can be different (thats based on an image I am applying later) rgds steve <label>200mm x 200mm</label><input name="sizes[]" type="checkbox" value="200 x 200" checked="true"><input name="" type="text" class='dropdown'><br/> <label>200mm x 300mm</label><input name="sizes[]" type="checkbox" value="200 x 300" ><input name="sprice[]" type="text" class='dropdown'><br/> <label>200mm x 400mm</label><input name="sizes[]" type="checkbox" value="200 x 400" ><input name="sprice[]" type="text" class='dropdown'><br/> <label>200mm x 500mm</label><input name="sizes[]" type="checkbox" value="200 x 500" ><input name="sprice[]" type="text" class='dropdown'><br/> <label>200mm x 600mm</label><input name="sizes[]" type="checkbox" value="200 x 600" ><input name="sprice[]" type="text" class='dropdown'> Quote Link to comment Share on other sites More sharing options...
steveh62 Posted November 11, 2008 Author Share Posted November 11, 2008 sorry adam, only just got you there...your making the relationship in the name tags yes? How would I pull the data out using a for loop...see the code <?php $foo = $_POST['sizes']; $sprice = $_POST['sprice']; echo £sprice; if (count($foo) > 0) { // loop through the array for ($i=0;$i<count($foo,);$i++) {//now insert sizes in table spcanvas_sizes echo $sprice; mysql_query( "INSERT INTO spcanvas_sizes(`sizes`, `pid`) VALUES('".$foo[$i]."', '".$id."')" ); // do something - this can be a SQL query, // echoing data to the browser, or whatever //echo "<li>$foo[$i] \n"; } //end for loop } //end if ?> Quote Link to comment Share on other sites More sharing options...
steveh62 Posted November 12, 2008 Author Share Posted November 12, 2008 can anybody help with this one?? I need to get the checkbox data and input data as a pair to insert into two db fields Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 12, 2008 Share Posted November 12, 2008 Checkboxes that are not checked are not included in the post data. So using a numerically indexed array as you have will not work because if the user does not check the 1st checkbox, then the 2nd checkbox (if checked) would be associated with the 1st text field. Can you not create a loop when creatingyour fields so you can specify an index: for ($i=0; $i<$max_fields; $i++) { echo "<label>200mm x 200mm</label>"; echo "<input name=\"sizes[$i]\" type=\"checkbox\" value=\"200 x 200\" checked=\"true\">"; echo "<input name=\"sprice[$i]\" type=\"text\" class=\"dropdown\"><br/>"; } Quote Link to comment Share on other sites More sharing options...
steveh62 Posted November 12, 2008 Author Share Posted November 12, 2008 thanks mjdamato, I sort of see what your say, can you elaborate more on your solution rgds steve Quote Link to comment Share on other sites More sharing options...
Adam Posted November 12, 2008 Share Posted November 12, 2008 Basically he's saying when a checkbox isn't checked, it doesn't return a value. So if you were to do that you may end up with two arrays like: true | some text value true | some other text value true | another text value | yet another text value | maybe another text value Where say the first, third and fifth checkboxes were checked, if you follow? He's saying if you loop through the checkboxes and have a counter increment on every loop, you can then set the index for both arrays, for each input. You could do it manually if it weren't so easy to loop through the inputs. but it would mean you'd end up with an array like: (0) true | (0) some text value (1) | (1) some other text value (2) true | (2) another text value (3) | (3) yet another text value (4) true | (4) maybe another text value If that makes more sense? Had to do it with brackets btw cause it went funny with [] .. Adam Quote Link to comment Share on other sites More sharing options...
steveh62 Posted November 12, 2008 Author Share Posted November 12, 2008 ok mr adam...got it going labels need to be dynamic tho'...how about pulling in the pair data Quote Link to comment Share on other sites More sharing options...
steveh62 Posted November 12, 2008 Author Share Posted November 12, 2008 ok, what I now need is to get the data from both arrays into 2 fields in a mysql db...how to loop thru the arrays? please Quote Link to comment Share on other sites More sharing options...
Adam Posted November 12, 2008 Share Posted November 12, 2008 foreach ($POST['sizes'] as $key => $size) { // .. } ..I think would work. Quote Link to comment Share on other sites More sharing options...
Adam Posted November 12, 2008 Share Posted November 12, 2008 then to get $sprice: $sprice = $_POST['sprice'][$key]; Not tested but should work - assuming you're using the POST method.. Quote Link to comment Share on other sites More sharing options...
steveh62 Posted November 12, 2008 Author Share Posted November 12, 2008 looking good... any idea on how to produce arbitary labels so that they could look like the ones I have mentioned, if I am to generate the checkbox/input text association? Quote Link to comment Share on other sites More sharing options...
Adam Posted November 12, 2008 Share Posted November 12, 2008 $labels = array( 0 => '200mm x 200mm', 1 => '200mm x 300mm', 2 => '200mm x 400mm', 3 => '200mm x 500mm', 4 => '200mm x 600mm', ); foreach ($labels as $key => $label) { print '<label>' .$label. '</label><input name="sizes[' .$key. ']" type="checkbox" value="' . $label. '"'; $key == 0 ? print ' checked'; print ' /><input name="sprice[' .$key. ']" type="text" class='dropdown' /><br/>'; } Might need a few adjustments.. Quote Link to comment Share on other sites More sharing options...
steveh62 Posted November 12, 2008 Author Share Posted November 12, 2008 getting an error syntax error, unexpected ';' round about last line Quote Link to comment Share on other sites More sharing options...
Adam Posted November 12, 2008 Share Posted November 12, 2008 print ' /><input name="sprice[' .$key. ']" type="text" class="dropdown" /><br/>'; error was: class='dropdown' .. Quote Link to comment Share on other sites More sharing options...
steveh62 Posted November 12, 2008 Author Share Posted November 12, 2008 got it thanks...the error was to do with $key == 0 ? bit and also amended the class to class="dropdown" so I changed it a tad to the following: BUT thanks a million works now :D <?php $labels = array( 0 => '200mm x 200mm', 1 => '200mm x 300mm', 2 => '200mm x 400mm', 3 => '200mm x 500mm', 4 => '200mm x 600mm', ); foreach ($labels as $key => $label) { print '<label>' .$label. '</label><input name="sizes[' .$key. ']" type="checkbox" value="' . $label. '"'; if ($key == 0){ print "checked"; } print ' /><input name="sprice[' .$key. ']" type="text" class="dropdown" /><br/>'; } ?> Quote Link to comment Share on other sites More sharing options...
Adam Posted November 12, 2008 Share Posted November 12, 2008 great stuff. I wasn't 100% sure about the syntax with the short-hand if .. Adam Quote Link to comment Share on other sites More sharing options...
steveh62 Posted November 12, 2008 Author Share Posted November 12, 2008 works a treat tho' thanks again...I'm sure I'll drop by again soon Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.