squiggerz Posted August 2, 2007 Share Posted August 2, 2007 I've read the 40 dozen posts on here dealing with checkboxes, either the replies dont apply to my situation or I'm just not grasping this concept yet (the latter probably). <? if ($_GET['add']) { $i = 0; $w = 0; echo '<table class="style3" align="right" width="90%" border="0" cellspacing="1"><tr>'; while ($a_row = mysql_fetch_array ($a_results, MYSQL_ASSOC)) { $i++; $w++; echo '<td valign="middle" align="left" width="25%">'; echo "<label><input type=\"checkbox\" name=\"".$a_row['category']."\" id=\"".$a_row['category']."\" value=\"".$a_row['category']."\""; if($a_row['category'] == $ucat) { echo " checked >"; } else { echo ">"; } echo $a_row['category']."</label>"; if($i % 1 == 0) { echo '</td> <td valign="middle" align="left" width="25%" valign="top">'; } if($w % 4 == 0) { echo '</tr> <tr>'; } } echo '</td></tr></table>'; } ?> Ok, this loop goes through an array of categories from a categories table in my db, the table has 2 fields, id & category, id is autoincremented. It works correctly, naming the checkbox & giving it the value of the variable from the array like its supposed to. When my form is submitted, how am I supposed to update my users table, which has a field 'category' that I want to have updated with all of the categories, I guess comma delimited. Should I just make another loop like if ($somevar) { while($_POST['WHATDOIPUTHERE?'] == $category) { //build my update query based on the $_POST[whatevergoeshere] var? } } I guess my real question would be, how do I loop the $_POST array to check against the categories table? I think... hope that made some sense to somebody. SQ Quote Link to comment https://forums.phpfreaks.com/topic/63073-more-checkbox-fun/ Share on other sites More sharing options...
squiggerz Posted August 2, 2007 Author Share Posted August 2, 2007 if($_POST) { foreach($_POST as $post -> $id) { echo "POST: ".$post."<br>POSTID: ".$id."<br>"; } Thought this might work... all I get on my echo is POST: Object POSTID: for each of the $_POST variables.. any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/63073-more-checkbox-fun/#findComment-314178 Share on other sites More sharing options...
Barand Posted August 2, 2007 Share Posted August 2, 2007 here's an example <?php if (isset($_POST['sub'])) { foreach ($_POST['cat'] as $catid) { echo $catid , '<br>'; // insert catid into table } // NOT recommended, but if you must have comma-sep list // instead of writing them to a separate table $cats = join (',', $_POST['cat']); echo $cats; } ?> <form method='post'> <input type="checkbox" name="cat[]" value="1">cat 1 <br> <input type="checkbox" name="cat[]" value="2">cat 2 <br> <input type="checkbox" name="cat[]" value="3">cat 3 <br> <input type="checkbox" name="cat[]" value="4">cat 4 <br> <input type="checkbox" name="cat[]" value="5">cat 5 <br> <input type="submit" name="sub" value="Submit"> <form> Quote Link to comment https://forums.phpfreaks.com/topic/63073-more-checkbox-fun/#findComment-314307 Share on other sites More sharing options...
squiggerz Posted August 2, 2007 Author Share Posted August 2, 2007 So as far as the checkbox names go, I can have as many as I want with the same name and all have different values? Quote Link to comment https://forums.phpfreaks.com/topic/63073-more-checkbox-fun/#findComment-314328 Share on other sites More sharing options...
Barand Posted August 2, 2007 Share Posted August 2, 2007 Yes. The value of each will be the category id. Giving them all the same name ending with [] posts them as an array. Quote Link to comment https://forums.phpfreaks.com/topic/63073-more-checkbox-fun/#findComment-314334 Share on other sites More sharing options...
squiggerz Posted August 2, 2007 Author Share Posted August 2, 2007 works like a charm! Thanks a million! Quote Link to comment https://forums.phpfreaks.com/topic/63073-more-checkbox-fun/#findComment-314348 Share on other sites More sharing options...
squiggerz Posted August 2, 2007 Author Share Posted August 2, 2007 foreach ($_POST['cat'] as $catid) { $catid = "," . $catid; } One last question, will the above give me a comma delimited output for the variables in the foreach loop? Quote Link to comment https://forums.phpfreaks.com/topic/63073-more-checkbox-fun/#findComment-314358 Share on other sites More sharing options...
squiggerz Posted August 2, 2007 Author Share Posted August 2, 2007 nevermind, you already gave me that tidbit, must have missed it before. Quote Link to comment https://forums.phpfreaks.com/topic/63073-more-checkbox-fun/#findComment-314360 Share on other sites More sharing options...
squiggerz Posted August 3, 2007 Author Share Posted August 3, 2007 I know this was solved but I figured it would be better to 'unsolve' this than start a new thread. // NOT recommended, but if you must have comma-sep list // instead of writing them to a separate table ^^ I now see the flaw of not just starting a new table, but how would you go about something like that? I mean I know how to set up a table, but would I just make a table that has the customers name as a field and then have 100 category fields and have them as flag 1 or 0 fields?? Quote Link to comment https://forums.phpfreaks.com/topic/63073-more-checkbox-fun/#findComment-314888 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.