Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: [STICKY] How do I get the ID of the current category.  (Read 14858 times)

gomisan

  • Full Member
  • ***
  • Karma: 0
  • Posts: 194
    • http://fish-cam.net/
[STICKY] How do I get the ID of the current category.
« on: April 04, 2006, 11:07:50 AM »

OK I think this needs a seperate post.

How do I get the id of the current category?

What I would like is to return the category id or return 0 if it's not valid (ie I'm at soem other page on the website)

A simple function that can then be used in other araes would be best. i'm tryng to find a way to highlight the current area you are in on a navigation menu. I'm happy to highlight 'Home' by default (hence returning 0)

I can think of two ways...

1) check if a variable passed matches the current and return true/false ( function_check_id(n) )
2) get the current category id and return that, and leave the comapring to the function it's being used in. ( function_get_id() )

hope this makes sense to soemone
Logged
Check out my sNews powered Tropical Fishtank site, live Fish-Cam !!

Luka

  • Administrator
  • ULTIMATE member
  • ******
  • Karma: 36
  • Posts: 1716
    • http://www.snewscms.com
[STICKY] How do I get the ID of the current category.
« Reply #1 on: April 04, 2006, 11:42:25 AM »

This is the function I use and it's yet to be released in upcoming version:

Code: [Select]

// FIND CATEGORY'S ID THROUGH SEF TITLE
function find_cat_id($categorysef) {
$query = "SELECT id FROM " .s('prefix'). "categories WHERE seftitle = '$categorysef'";
$result = mysql_query($query);
  while ($r = mysql_fetch_array($result)) {
  $cat_id = $r['id'];  
}
if (isset($cat_sef)) {
$cat_sef_title = $cat_sef;

}
return $cat_id;
}


Paste this in snews.php.

You can find out your category's sef title with: get_id('category')
Logged

gomisan

  • Full Member
  • ***
  • Karma: 0
  • Posts: 194
    • http://fish-cam.net/
[STICKY] How do I get the ID of the current category.
« Reply #2 on: April 04, 2006, 11:46:40 AM »

You are a wizard!! That's perfect. and to think I was just on the phone with a mate trying to brainstorm it.

thanks VERY much. I'll post my modifications here to share when I'm done.
Logged
Check out my sNews powered Tropical Fishtank site, live Fish-Cam !!

gomisan

  • Full Member
  • ***
  • Karma: 0
  • Posts: 194
    • http://fish-cam.net/
[STICKY] How do I get the ID of the current category.
« Reply #3 on: April 04, 2006, 12:39:27 PM »

OK, here is how I've used all this stuff to make an functional menu bar in the Internet Services template!

First the code in the template:

Code: [Select]
<div class="bar">
<ul>
<li class="browse_category">Select Service:</li>
<li><a href="http://tglmultimedia.net/" accesskey="h">Home</a></li>
<? single_category(2); ?>
<? single_category(1); ?>
<? single_category(3); ?>
<? single_category(4); ?>
</ul>
</div>

and then in the snews.php file I have added this function:

Code: [Select]
// DISPLAY SINGLE CATEGORY
function single_category($cat_id) {
$query = "SELECT * FROM " .s('prefix'). "categories WHERE published = 'YES' and id = $cat_id";
$result = mysql_query($query);
while ($r = mysql_fetch_array($result)) {
$calc_num_query = "SELECT * FROM " .s('prefix'). "articles WHERE position = 1 AND category = $r[id]";
$cm_result = mysql_query($calc_num_query);
$num_rows = mysql_num_rows($cm_result);
$category_title = $r['seftitle'];
$current_sef = get_id('category');
$current_id = find_cat_id($current_sef);
if ($cat_id <> $current_id) {
if (s('display_num_categories') == True) {
echo "<li><a href='" .s('website').$category_title. "/' title='". $r['description'] ."'>" .$r['name']. " (" .$num_rows. ") </a></li>"; }
else { echo "<li><a href='" .s('website').$category_title. "/' title='". $r['description'] ."'>" .$r['name']. "</a></li>"; }}
else { if (s('display_num_categories') == True) {
echo "<li class=active><a href='" .s('website').$category_title. "/' title='". $r['description'] ."'>" .$r['name']. " (" .$num_rows. ") </a></li>"; }
else { echo "<li class=active><a href='" .s('website').$category_title. "/' title='". $r['description'] ."'>" .$r['name']. "</a></li>"; }}}
}

which uses:

Code: [Select]
// FIND CATEGORY'S ID THROUGH SEF TITLE
function find_cat_id($categorysef) {
$query = "SELECT id FROM " .s('prefix'). "categories WHERE seftitle = '$categorysef'";
$result = mysql_query($query);
  while ($r = mysql_fetch_array($result)) {
  $cat_id = $r['id'];  
}
if (isset($cat_sef)) {
$cat_sef_title = $cat_sef;

}
return $cat_id;
}

and there you have it!

For some reason the CSS is causing double down arrows to display.. but I'm sure I'll figure that out.

« Last Edit: March 30, 2009, 06:26:21 PM by Joost »
Logged
Check out my sNews powered Tropical Fishtank site, live Fish-Cam !!

Jochum Meester

  • Sr. Member
  • ****
  • Karma: 1
  • Posts: 309
    • JochumMeester.com
[STICKY] How do I get the ID of the current category.
« Reply #4 on: April 04, 2006, 04:00:10 PM »

#navigation a { background: none; }
where #navigation is the ID that the bar is in. This should get rid of the double arrow I think, if I remember correctly that was what I did a few weeks ago.

I still don't get all the code above though lol :) I can understand parts of it but still confuses me hehe
Logged

gomisan

  • Full Member
  • ***
  • Karma: 0
  • Posts: 194
    • http://fish-cam.net/
[STICKY] How do I get the ID of the current category.
« Reply #5 on: April 05, 2006, 01:11:36 PM »

Thanks, that fixed it. I changed it from 'inherit' to 'none'

Logged
Check out my sNews powered Tropical Fishtank site, live Fish-Cam !!

grinpin

  • Newbie
  • *
  • Karma: 0
  • Posts: 19
[STICKY] How do I get the ID of the current category.
« Reply #6 on: April 09, 2006, 10:44:43 AM »

ok all nice :)

but could somone help me with a correct CODE for txt version of Snews :)
because in this there is MySQL part, I was trying to replace:

$result = mysql_query($query);
while ($r = mysql_fetch_array($result)) {

with:

$result = $db->query($query);
while ($r = $result->fetch_array()) {

but result was something like: "Fatal error: Call to undefined ..." :)
Logged

Luka

  • Administrator
  • ULTIMATE member
  • ******
  • Karma: 36
  • Posts: 1716
    • http://www.snewscms.com
[STICKY] How do I get the ID of the current category.
« Reply #7 on: April 09, 2006, 12:29:38 PM »

Quote from: gomisan
Thanks, that fixed it. I changed it from 'inherit' to 'none'


Wouldn't that display a warning in validation?
Logged

gomisan

  • Full Member
  • ***
  • Karma: 0
  • Posts: 194
    • http://fish-cam.net/
[STICKY] How do I get the ID of the current category.
« Reply #8 on: April 09, 2006, 01:33:01 PM »

validation? I am not too fussy with that side of things yet.
Logged
Check out my sNews powered Tropical Fishtank site, live Fish-Cam !!

Patric Ahlqvist

  • Nobodys perfect, but Im pretty effing close
  • ULTIMATE member
  • ******
  • Karma: 65
  • Posts: 4867
  • I'm a self-made man and worships my creator.
    • p-ahlqvist.com
[STICKY] How do I get the ID of the current category.
« Reply #9 on: April 09, 2006, 02:38:12 PM »

Hehe, that'll come my friend, it'll come ;)'

EDIT --> oh, forgot... there is some up's with learning to validate from the beginning, rather than going there when you actually HAVE LEARNED wrong things... Just a thought in all friendlyness :)
Logged
"It's only dead fish that goes with the flow... "
Updated

Rod

  • Jr. Member
  • **
  • Karma: 0
  • Posts: 78
    • http://www.rjs-custom.com
[STICKY] How do I get the ID of the current category.
« Reply #10 on: May 01, 2006, 06:19:57 AM »

Quote from: grinpin
ok all nice :)

but could somone help me with a correct CODE for txt version of Snews :)
because in this there is MySQL part, I was trying to replace:

$result = mysql_query($query);
while ($r = mysql_fetch_array($result)) {

with:

$result = $db->query($query);
while ($r = $result->fetch_array()) {

but result was something like: "Fatal error: Call to undefined ..." :)

you need to add
Code: [Select]
connect_to_db();
$db = new Database(s('dbname'));
at the beginning of any function that calls the database to make it work in the txt version of sNews.

Am working on fixing that before the next sNews version


so the 2 functions would look like this

Code: [Select]
// DISPLAY SINGLE CATEGORY
function single_category($cat_id) {
        connect_to_db();
$db = new Database(s('dbname'));
$query = "SELECT * FROM " .s('prefix'). "categories WHERE published = 'YES' and id = $cat_id";
$result = $db->query($query);
while ($r = $result->fetch_array()) {
$calc_num_query = "SELECT * FROM " .s('prefix'). "articles WHERE position = 1 AND category = $r[id]";
$cm_result = $db->query($calc_num_query);
$num_rows = $cm_result->num_rows();
$category_title = $r['seftitle'];
$current_sef = get_id('category');
$current_id = find_cat_id($current_sef);
if ($cat_id <> $current_id) {
if (s('display_num_categories') == True) {
echo "
  • " .$r['name']. " (" .$num_rows. ")
  • ";
    } else {
    echo "
  • " .$r['name']. "
  • ";
    }
    } else {
    if (s('display_num_categories') == True) {
    echo "
  • " .$r['name']. " (" .$num_rows. ")
  • ";
    } else {
    echo "
  • " .$r['name']. "
  • ";
    }
    }
    }
    }

    Code: [Select]
    // FIND CATEGORY'S ID THROUGH SEF TITLE
    function find_cat_id($categorysef) {
            connect_to_db();
    $db = new Database(s('dbname'));
    $query = "SELECT id FROM " .s('prefix'). "categories WHERE seftitle = '$categorysef'";
    $result = $db->query($query);
    while ($r = $result->fetch_array()) {
    $cat_id = $r['id'];  
    }
    if (isset($cat_sef)) {
    $cat_sef_title = $cat_sef;
    }
    return $cat_id;
    }

    Sorry it took so long to answer, been kinda busy lately.

    cheers
    Logged
    Whatever went wrong, I am pretty sure I didn't do it!  Well atleast I think I didn't do it...

    Fred K

    • Still trying to learn stuff
    • ULTIMATE member
    • ******
    • Karma: 130
    • Posts: 2728
      • Personal
    [STICKY] How do I get the ID of the current category.
    « Reply #11 on: September 08, 2006, 09:13:03 PM »

    Is there any way to include "Home" in this. I'd like for Home to be active when on that page/section/whatever, but I can't see any way of making that happen since, well, Home isn't a category. I'm sure there's something I'm missing, as ususal. :/

    Other than that, the single category thing works just fine for me -- many thanks, gomisan.
    Logged

    tim0fee

    • Jr. Member
    • **
    • Karma: 0
    • Posts: 71
    [STICKY] How do I get the ID of the current category.
    « Reply #12 on: November 11, 2006, 09:17:22 AM »

    Yo peeps,

    Haven't been around for a bit - so excuse me if i'm behind!! First thankyou to the whole team for a great new release. It even works on my Mac!! lol

    This is a similar request @ Gomisan really. I am using the Internet_Broadcast as a basis template and want to display 'pages' in the great  tabbed menu at the top. Is this achievable in a similar way to Gomisan's function described above? I tried a few things but they all appear to be hard coded... :(

    Any help would be welcomed!

    peace and love to the web's top CMS crew!

    tim0fee
    Logged

    tim0fee

    • Jr. Member
    • **
    • Karma: 0
    • Posts: 71
    [STICKY] How do I get the ID of the current category.
    « Reply #13 on: November 12, 2006, 11:21:31 AM »

    Any takers ??  :/  

    tim0fee
    Logged

    codetwist

    • Hero Member
    • *****
    • Karma: 50
    • Posts: 940
    [STICKY] How do I get the ID of the current category.
    « Reply #14 on: November 20, 2006, 02:49:06 PM »

    Samples above contain following pieces of code inside functions:

    Code: [Select]
       if (isset($cat_sef)) {
            $cat_sef_title = $cat_sef;
        }
    I see neither where $cat_sef could come from nor any possible use of $cat_sef_title. Can somebody explain what I'm missing there, please?

                                                   
     :rolleyes: Just learning
    Logged