Subcategory IDs in relation to parent category ID switch case

Ok. This code:

$cat_id_gum="";
if(($category[0]->parent)!='0')
{
    $cat_id_gum=$category[0]->cat_ID;
}
elseif(($category[1]->parent)!='0')
{
    $cat_id_gum=$category[1]->cat_ID;
}
elseif(($category[2]->parent)!='0')
{
    $cat_id_gum=$category[2]->cat_ID;
}
else
{
    $cat_id_gum=$category[3]->cat_ID;
}

Is not really necessary. I’ll show you what I mean in a bit. Later, however, you then set $cat_id_gum to the current post ID…

$cat_id_gum = get_the_ID();

… which doesn’t make any sense and makes all of the preceding code pointless. You do a couple of other things which don’t really make sense to me. I am going to ignore that and try to address the basic question of simplifying the code and dealing with child categories.

First, lets associate the categories and the codes:

// the key is the category
// the value is the gum code
$gum_codes = array(
  '1' => '75803baa',
  '2' => 'b737914c'
);

Now you can echo a script like:

printf($script,$gum_codes[1]);

Now, you need to convert your category to its topmost parent:

$category = get_the_category($post->ID);
$cat_id_gum = $category[0]->cat_ID;
$cat_id_gum = get_ancestors($cat_id_gum,'category');
array_shift($cat_id_gum);
$cat_id_gum = $cat_id_gum[0];
// $cat_id_gum is now your parent category

Put that all together:

$category = get_the_category($post->ID);
$cat_id_gum = $category[0]->cat_ID;
$cat_id_gum = get_ancestors($cat_id_gum,'category');
array_shift($cat_id_gum);
$cat_id_gum = $cat_id_gum[0];
// $cat_id_gum is now your parent category

$script="<script type="text/javascript">ggv2id=\"%s\';</script> <script type="text/javascript" src="http://g2.gumgum.com/javascripts/ggv2.js"></script>';

// the key is the category
// the value is the gum code
$gum_codes = array(
  '1' => '75803baa',
  '34' => 'b737914c'
);

printf($script,$gum_codes[$cat_id_gum]);

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)