Topic: Using current topic id to apply dynamic CSS class

On my site, I have a two column layout with recent topics displayed in a sidebar using extern.php.  I'd like to use CSS to change the colour of the sidebar link if that topic is being displayed.

I've managed to get a class into the list output from extern.php by changing:

echo '<li><a href="'.$pun_config['o_base_url'].'/viewtopic.php?id='.$cur_topic['id'].'&action=new" title="'.pun_htmlspecialchars($cur_topic['subject']).'">'.$subject_truncated.'</a></li>'."\n";

to:

echo '<li class="pun_'.$cur_topic['id'].'"><a href="'.$pun_config['o_base_url'].'/viewtopic.php?id='.$cur_topic['id'].'&action=new" title="'.pun_htmlspecialchars($cur_topic['subject']).'">'.$subject_truncated.'</a></li>'."\n";

(note I've added the following inside the <li> tag:

class="pun_'.$cur_topic['id'].'"

I've now tried adding the following CSS to the <head> of header.php:

<?php
echo '<style type="text/css"> ';

echo 'ul li. pun_'.$cur_topic['id'].' a:link,';
echo 'ul li. pun_'.$cur_topic['id'].' a:hover,';
echo 'ul li. pun_'.$cur_topic['id'].' a:active,';
echo 'ul li. pun_'.$cur_topic['id'].' a:visited,';

echo '{color: #ccc; text-decoration: none;} ';
echo '</style>';
?>

But I don't get a value for $cur_topic[id]. I think that I need to get the topic id into header.php but I don't know how.

I'm pretty sure it should be easy to give $cur_topic[id] the id of the current topic but I'm at the limit of my (very limited) php/sql knowledge here.

Can anyone help me out please?

Re: Using current topic id to apply dynamic CSS class

Why not use the id variable in the url? (viewtopic.php?id=xxx)

Looking for a certain modification for your forum? Please take a look here before posting.

Re: Using current topic id to apply dynamic CSS class

Well I thought about that, but do you know how I can append that to the <li> tag in extern.php?

I need to use the same id in the CSS as in the <li> within extern.php.  Extern.php seems to use the topic id, while the id variable in the page that extern.php links to is the post id (I think).

Just to clarify here's an example from my forum:

In extern.php, $cur_topic['id'] == 148
But the id in url of the page that extern.php points to is 182. (I guess this is the post id?)

So can I get extern.php to use the post id instead?

4 (edited by marvincooper 2006-02-08 14:11)

Re: Using current topic id to apply dynamic CSS class

Hmmm, I've just realised that there's more to this than I first thought.

In my example above, different id's were present in extern.php to the url that was displayed by the post. However, I just realised that this is only the case when logged in to the forum. When logged out, the id's are the same.

As an example, here are two url's that are from the same page, one while logged in and the other while logged out:

http://swindonharriers.com/forum/viewto … action=new  (logged out)
http://swindonharriers.com/forum/viewto … d=182#p182  (logged in)

As I want to use the class name (the topic id) from extern.php in the CSS within header.php, header.php needs to get that id somehow. Can it still do that if the user is logged in? I guess I need some SQL query cleverness here that I just don't have....

So, the problem is now a little more complicated. If (in header.php) I get the topic id from the url, it will only work when the user is logged out. If the user is logged in, I will need to find the topic id based on the post id (I think).

Does this make sense? And if it does, any ideas how to do it? This'll make me a very happy chap if I can get it to work smile

5 (edited by marvincooper 2006-02-08 15:46)

Re: Using current topic id to apply dynamic CSS class

A follow up to my previous post - I've got the styling working for logged-out users by using $_GET["id"] as shown in the code below:

echo '<style type="text/css"> ';

echo 'ul li.pun_'.$_GET["id"].' a:link,';
echo 'ul li.pun_'.$_GET["id"].' a:hover,';
echo 'ul li.pun_'.$_GET["id"].' a:active,';
echo 'ul li.pun_'.$_GET["id"].' a:visited';

echo '{color: #ccc; text-decoration: none;} ';
echo '</style>';

However, I'm still stuck with what to do when the id changes to the post id rather than the topic id for logged-in users.

6 (edited by marvincooper 2006-02-08 16:23)

Re: Using current topic id to apply dynamic CSS class

So, to continue this conversation with myself - I have now cracked it!

If the user is logged in, then the url will contain the post id, so this is cross referenced in the database to get the topic id (this query was copied from viewtopic.php, with just a minor change to get the post id from the url).

Here's the code I've ended up with in header.php:

if ($_GET["pid"])
{
    $result = $db->query('SELECT topic_id FROM '.$db->prefix.'posts WHERE id='.$_GET["pid"]) or error('Unable to fetch post info', __FILE__, __LINE__, $db->error());
    if (!$db->num_rows($result))
        message($lang_common['Bad request']);

    $aid = $db->result($result);
}

else
    
{
    $aid = $_GET["id"];
}

echo '<style type="text/css"> ';

echo 'ul li.pun_'.$aid.' a:link,';
echo 'ul li.pun_'.$aid.' a:hover,';
echo 'ul li.pun_'.$aid.' a:active,';
echo 'ul li.pun_'.$aid.' a:visited';

echo '{color: #ccc; text-decoration: none;} ';
echo '</style>';

Hurrah!

Re: Using current topic id to apply dynamic CSS class

Oh, forgot that you had to edit extern.php too. Sorry.

Anyway, glad to see you worked it out.

Looking for a certain modification for your forum? Please take a look here before posting.