littlejones Posted November 15, 2006 Share Posted November 15, 2006 Here's one for you...I have a form with 4 checkboxes. When the form is submitted those that are checked are stored in the database in 4 separate fields as "yes" or left null if they were not checked.So, I then want to display on a page the categories (checkboxes) that the person has checked. Obviously I want to separate these with a comma or an & sign. However I don't want a comma or & sign to appear if part of the array is empty. So as an example...4 categories: General, Immigration, Housing, Employment.The user checks Immigration and Employment. These are stored in the database in fields categoryImmigration and categoryEmployment with the record "yes" and the other two (categoryGeneral and categoryHousing) are left blank.I now want to display the categories on a page in the format... Immigration Team, Employment Team.So I SELECT * FROM the table with these fields in. I store the results as follows...[code]$categoryGeneral = mysql_result($result,$i,'categoryGeneral'); if ($categoryGeneral == "yes") $category[0] = "General";$categoryImmigration = mysql_result($result,$i,'categoryImmigration'); if ($categoryImmigration == "yes") $category[1] = "Immigration Team";$categoryHousing = mysql_result($result,$i,'categoryHousing'); if ($categoryHousing == "yes") $category[2] = "Housing Team";$categoryEmployment = mysql_result($result,$i,'categoryEmployment'); if ($categoryEmployment == "yes") $category[3] = "Employment Team";[/code]That's fine, and now I have the result stored in the array $category[]I then need to display on the page. As you can now see, I can't do it in the following way, because if a user didn't select one of the checkboxes there would be a redundant comma..[code]echo $category[0].", ".$category[1].", ".$category[2].", ".$category[3];[/code]In this example, the following would be displayed on the page..., Immigration Team, , Employment TeamObviously unacceptable. How can I get around this?Thanks a million in advance I have a deadline to meet!! :o Link to comment https://forums.phpfreaks.com/topic/27326-array-problem/ Share on other sites More sharing options...
Barand Posted November 15, 2006 Share Posted November 15, 2006 Name the cboxes like this, with [] at end so they are posted as an array[code]<?phpif (isset($_POST['submit'])) { echo 'Categories: ' . join(', ', $_POST['category']);}?><form method='post'><input type="checkbox" name="category[]" value="General" /> General<br/><input type="checkbox" name="category[]" value="Immigration" /> Immigration<br/><input type="checkbox" name="category[]" value="Housing" /> Housing<br/><input type="checkbox" name="category[]" value="Employment" /> Employment<br/><input type="submit" name="submit" value="Submit"></form>[/code] Link to comment https://forums.phpfreaks.com/topic/27326-array-problem/#findComment-124937 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.