Jump to content

Array problem...


littlejones

Recommended Posts

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 Team

Obviously 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

Name the cboxes like this, with [] at end so they are posted as an array
[code]
<?php
if (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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.