PHP for each issue
<p>I am using the foreach function to get all the categories for a WordPress site and need a way to adjust the output if there is another tag, to include dynamic CSS.</p>

<p>Basically, if I have only one tag I need the code output to look like this:</p>

<pre><code>.category-wordpress #wordpress { background: #FFFFFF; color: #232323; text-decoration: none; }
</code></pre>

<p>and if there's more than one tag like this:</p>

<pre><code>.category-wordpress #wordpress, .category-html #html { background: #FFFFFF; color: #232323; text-decoration: none; }
</code></pre>

<p>The problem is with the comma between declarations. How can I change this code so a comma is only included if there is another category to include?</p>

<pre><code>if (is_category()) { 

$css_cats = "&lt;style type='text/css'&gt;";

 //get all blog categories
 $categories = get_categories('title_li=&amp;orderby=name&amp;hide_empty=0');

 if ($categories) {

  foreach($categories as $category) {

   $css_cats .= ".category-".$category-&gt;category_nicename." #".$category-&gt;category_nicename.", ";

  }

 } 

$css_cats .= "{ background: #FFFFFF; color: #232323; text-decoration: none; }";

$css_cats .= "&lt;/style&gt;";

echo $css_cats;

}
</code></pre>

<p>This code is outputting a comma at the end of the last declaration like this:</p>

<pre><code>.category-wordpress #wordpress, .category-html #html, { background: #FFFFFF; color: #232323; text-decoration: none; }
</code></pre>

<p>so the CSS breaks.</p>

<p>I can change the code so it outputs the CSS declarations individually like this:</p>

<pre><code>.category-wordpress #wordpress { background: #FFFFFF; color: #232323; text-decoration: none; }
.category-html #html { background: #FFFFFF; color: #232323; text-decoration: none; }
</code></pre>

<p>but surely there's a better way?</p>

<p>Any help is greatly appreciated.</p>

<p>Cheers,</p>

<p>James</p>
