...

View Full Version : Displaying an Array problem



littlejones
11-15-2006, 10:39 AM
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...


$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";

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..


echo $category[0].", ".$category[1].", ".$category[2].", ".$category[3];

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

sir pannels
11-15-2006, 12:01 PM
You should probably put this into a while statement but you get the idea, it does the job ...

Replace


echo $category[0].", ".$category[1].", ".$category[2].", ".$category[3];


with ...



if($category[0]){echo ("".$category[0].",");}
if($category[1]){echo ("".$category[1].",");}
if($category[2]){echo ("".$category[2].",");}
if($category[3]){echo ("".$category[3]."");}

SeeIT Solutions
11-15-2006, 12:03 PM
That won't really work, sir.

Try this instead, replace:


$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";

echo $category[0].", ".$category[1].", ".$category[2].", ".$category[3];

with


$categories = mysql_fetch_assoc($result);
$count = 0;
$comma = "";
foreach ($categories as $cat => $name) {
if (!empty($name)) {
if ($count > 0) {
$comma = ", ";
}
echo $comma.$name;
}
}

Brandt
02-08-2007, 02:21 PM
I am running into an *almost* identical problem myself. I have a database setup with two tables, one for the item and one for categories with an auto_increment primary_key id field on both tables. Categories can be added/deleted with the system I am working on. Now, I am using a list form field in the HTML where you can select multiple categories to store an item.

I am not the best with arrays and how to use them but it seems to be the best way to do this. I want to store this data into the single selected_categories field in the item table.

This is my first question: Say there are 6 categories for an item to be listed under. The user selects category 3 and 6. This is then stored into the item table under selected_categories. How can i do this with an array?

Second: When editing an item through the system, I want to pull the categories from the item table, but also display the categories that were NOT selected initially. Example: Select item 1 from database which happens to be in category 3 and 6, but in the HTML output list field it shows all available categories from the categories table but only has categories 3 and 6 selected. How can this be accomplished?

Thank you!

aedrin
02-08-2007, 03:44 PM
That's fine, and now I have the result stored in the array $category[]

Simplest answer:



$categories = implode(', ', $category);
echo $categories;

Brandt
02-08-2007, 04:14 PM
The code above my post is not related to my question. I only posted my question here since it is similar. Should i make it a new topic instead?

Brandoe85
02-08-2007, 04:15 PM
The code above my post is not related to my question. I only posted my question here since it is similar. Should i make it a new topic instead?

Yes, make one that pertains to your question specifically.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum