Category ID known - how to fetch category name?

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Post Reply
Don Chambers
Regular
Posts: 3659
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Category ID known - how to fetch category name?

Post by Don Chambers »

In my config.inc.php file, I am assigning category IDs to a template variable using Garvin's idea of:

Code: Select all

 if ($serendipity['GET']['adminModule'] == 'templates') {
  $all_cats = serendipity_fetchCategories('all');
  $categories = serendipity_walkRecursive($categories, 'categoryid', 'parentid', VIEWMODE_THREADED);
  $catsel = array();
  foreach($all_cats AS $cat) {
    $catsel[$cat['categoryid']] = str_repeat(' ', $cat['depth']) . $cat['category_name'];
  }
}

........
array(
      'var'           => 'cat_assigned',
      'name'          => 'My category',
      'type'          => 'select',
      'default'       => '',
      'select_values' => $catsel
    ),
So now I have $template_option.cat_assigned available. From index.tpl, how can I now fetch the category name for that ID so I can display the name instead of the ID?
=Don=
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

Don, if you need to look up more than one category name, add this code in config.inc.php (should run every time the template is loaded):

Code: Select all

  $all_cats = serendipity_fetchCategories('all'); 
  $id_to_name = array();
  foreach ($all_cats as $cat) {
    $id_to_name[$cat['categoryid']] = $cat['category_name'];
  }
  $catnames = array('names' => $id_to_name);
  $serendipity['smarty']->assign('catnames', $catnames);
Now you can access $catnames in your entries.tpl; since it's an array with a nested array, you can even use {pickKey array='$catnames' key='$template_option.catid'}.

If you know the particular ID you like, you can just use $catnames.names[10].

If you'd like to be able to access more category information, you can either make and populate an additional array (like $cat_parents), or try something like this:

Code: Select all

  $all_cats = serendipity_fetchCategories('all'); 
  $id_to_data = array();
  foreach ($all_cats as $cat) {
    $id_to_data[$cat['categoryid']] = 
      array('category_name' => $cat['category_name'],
            'parentid' => $cat['parentid']
      );
    // You can also get:
    //   category_icon
    //   category_description
    //   authorid
    //   category_left
    //   category_right
  }
  $catdata = array('data' => $id_to_data);
  $serendipity['smarty']->assign('catdata', $catdata);
Unfortunately, getting at the data becomes more difficult. If you know the category ID, you need $catdata.data[10].category_name. You need to do something like:

Code: Select all

  {capture assign='thecat'}
    {pickKey array='$catdata' key='$template_option.catid'}
  {/capture}
  <span>Category {$thecat.category_name}'s parent: {$thecat.parentid}.</span>
Hope that does what you need; I haven't had a chance to test any of it.
Judebert
---
Website | Wishlist | PayPal
Post Reply