ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Database class query to access category based on url_title

June 27, 2007 4:45am

Subscribe [2]
  • #1 / Jun 27, 2007 4:45am

    joelene

    87 posts

    Could some kind soul help me formulate the simplest possible query (using the database class - parse order is important) to return the category_name (or cat_name I guess) from the url_title please?
    I am not so hot at this…

  • #2 / Jun 27, 2007 4:50am

    joelene

    87 posts

    A quick update - I can also use entry_id which I guess is a more reliable/foolproof/perhaps simpler way of doing it?

  • #3 / Jun 27, 2007 4:57am

    Ingmar

    29245 posts

    So, what do you mean: You’ve got the entry_id and want to know which category this entry belongs to? What’s wrong with the old-fashioned

    {categories}{category_name}{/categories}

    ?

  • #4 / Jun 27, 2007 5:47am

    joelene

    87 posts

    I need to be able to limit the output to one category as each entry may belong to many categories and I am using the returned value in a link - using {category not="X|X|X|X"} isnt really an option

  • #5 / Jun 27, 2007 6:03am

    joelene

    87 posts

    In fact it can be using the {query} module…beg your pardons

  • #6 / Jun 27, 2007 10:26am

    Robin Sowell

    13255 posts

    I’m going to shift this over to ‘How to’- as custom queries are a better fit for the ‘brainstorming’ section.  But the very simplest approach would be:

    {exp:query sql="SELECT c.cat_id, c.cat_name FROM exp_categories c, exp_category_posts p WHERE c.cat_id = p.cat_id and p.entry_id = 5 ORDER BY cat_name LIMIT 1"}

    5 represents the entry id.  Lot of variation you could do- limit it to parent_id = 0 to only get top level cats, use url_title- by making a more complicated query that joins the exp_weblog_titles table, take out the limit 1 to see all of the cats assigned- it’s somewhat of a crapshoot which cat will be returned now- though we’re ordering by the cat name- so at least it’s predictable.

    But yep- that’s the simplest one- haven’t tested, but looks about right.

  • #7 / Jun 27, 2007 11:38am

    joelene

    87 posts

    Superb - thanks very much that works a treat

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases