Determine category number within index template (index.tpl)

In this board you can talk about general questions about phpMyFAQ

Moderator: Thorsten

Post Reply
natmanu
Posts: 22
Joined: Wed May 24, 2006 7:07 pm

Determine category number within index template (index.tpl)

Post by natmanu »

Back with another one...

Hi, I'm trying to change the banner/header depending on which category you are in. I'm using Apache2Triad dev server on XP dev box.

On my local server I could get the cat number from the url by doing this:

<?php

switch ($_GET['cat'])
{
case 1:
print "<img src=\"../../images/faq_header_gen.jpg\" width=\"595\" height=\"201\" alt=\"\" border=\"0\">";
break;
case 2:
print "<img src=\"../../images/faq_header_bo.jpg\" width=\"595\" height=\"201\" alt=\"\" border=\"0\">";
break;
case 3:
print "<img src=\"../../images/faq_header_cr.jpg\" width=\"595\" height=\"201\" alt=\"\" border=\"0\">";
break;
case 4:
print "<img src=\"../../images/faq_header_ec.jpg\" width=\"595\" height=\"201\" alt=\"\" border=\"0\">";
break;
default:
print "<img src=\"../../images/faq_header.jpg\" width=\"595\" height=\"201\" alt=\"\" border=\"0\">";
}
?>

All was working fine, but when I upload it to my live server the script bombs. So the question is: Is there a way to determine which category you are in from the index template? Is there a way to insert case statement in the index.tpl

eg.
$catNum = x;
where x is the category number of the section your are in.

Cheers
Nat
Thorsten
Posts: 15793
Joined: Tue Sep 25, 2001 11:14 am
Location: #phpmyfaq
Contact:

Re: Determine category number within index template (index.t

Post by Thorsten »

Hi,
natmanu wrote:All was working fine, but when I upload it to my live server the script bombs.
what happens exactly? :)

bye
Thorsten
phpMyFAQ Maintainer and Lead Developer
amazon.de Wishlist
natmanu
Posts: 22
Joined: Wed May 24, 2006 7:07 pm

Post by natmanu »

Hi,

I've managed to get the script to work but it now displays error when it branches to the default case statement. see:
http://www.uvolunteer.org/faqs/index.php

It seems to work when cat = a number !

2. I'm also having problem with include files inserted into index.tpl
What is the path to the include dir if my include directory from document root is: www.uvolunteer.org/includes/

I've tried '/includes/filename.php' or relative to the index.tpl '../../includes/filename.php' but it errors that it cannot find file.

Cheer
Nat
Thorsten
Posts: 15793
Joined: Tue Sep 25, 2001 11:14 am
Location: #phpmyfaq
Contact:

Post by Thorsten »

Hi,

the code have to look like:

Code: Select all

<?php

if (isset($_GET['cat']) && is_int($_GET['cat'])) {
    switch ($_GET['cat']) {
        case 1:
            print "<img src=\"../../images/faq_header_gen.jpg\" width=\"595\" height=\"201\" alt=\"\" border=\"0\">";
            break;
        case 2:
            print "<img src=\"../../images/faq_header_bo.jpg\" width=\"595\" height=\"201\" alt=\"\" border=\"0\">";
            break;
        case 3:
            print "<img src=\"../../images/faq_header_cr.jpg\" width=\"595\" height=\"201\" alt=\"\" border=\"0\">";
            break;
        case 4:
            print "<img src=\"../../images/faq_header_ec.jpg\" width=\"595\" height=\"201\" alt=\"\" border=\"0\">";
            break;
        default:
            print "<img src=\"../../images/faq_header.jpg\" width=\"595\" height=\"201\" alt=\"\" border=\"0\">";
            break;
        }
    }
?> 
bye
Thorsten
phpMyFAQ Maintainer and Lead Developer
amazon.de Wishlist
natmanu
Posts: 22
Joined: Wed May 24, 2006 7:07 pm

Post by natmanu »

ok i get it, if cat is not set then it will produce an error.

What about the include path issue?, see above.

Cheers
Nat
Thorsten
Posts: 15793
Joined: Tue Sep 25, 2001 11:14 am
Location: #phpmyfaq
Contact:

Post by Thorsten »

Hi,

you should try the path '../includes/filename.php'.

bye
Thorsten
phpMyFAQ Maintainer and Lead Developer
amazon.de Wishlist
natmanu
Posts: 22
Joined: Wed May 24, 2006 7:07 pm

Post by natmanu »

hi,

The conditon you wrote outputs nothing! I've modified it to contain an else conditon to test it and it always evaluates false although cat is get. you test it here, click on the right nav to see results:
www.uvolunteer.org/faqs

<?php
if (isset($_GET['cat']) && is_int($_GET['cat'])) {
switch ($_GET['cat']) {
case 1:
print "<img src=\"../../images/faq_header_gen.jpg\" width=\"595\" height=\"201\" alt=\"\" border=\"0\">";
break;
case 2:
print "<img src=\"../../images/faq_header_bo.jpg\" width=\"595\" height=\"201\" alt=\"\" border=\"0\">";
break;
case 3:
print "<img src=\"../../images/faq_header_cr.jpg\" width=\"595\" height=\"201\" alt=\"\" border=\"0\">";
break;
case 4:
print "<img src=\"../../images/faq_header_ec.jpg\" width=\"595\" height=\"201\" alt=\"\" border=\"0\">";
break;
default:
print "<img src=\"../../images/faq_header.jpg\" width=\"595\" height=\"201\" alt=\"\" border=\"0\">";
break;
}
} else {
echo "The statement evaluated to false".' and $GET = '.$_GET['cat'];
}

?>
matteo
Posts: 572
Joined: Sun Nov 20, 2005 6:53 pm
Location: Italy

Post by matteo »

Hi Nat,
natmanu wrote: } else {
echo "The statement evaluated to false".' and $GET = '.$_GET['cat'];
}?>
You should use:

Code: Select all

...
} else {
echo "The statement evaluated to false";
}
I'd suggest you to change the way of working: just add a new key {key} in the template and manage it within index.php: the template has currently no access to HTTP GET parameters.
Let me check this proposal and back here with a code sample.
phpMyFAQ QA / Developer
Amazon.co.uk Wishlist
matteo
Posts: 572
Joined: Sun Nov 20, 2005 6:53 pm
Location: Italy

Post by matteo »

Hi Nat,
back again with another different solution :wink:
  1. Use this code below for index.tpl:

    Code: Select all

    ...
    <?php
    if (isset($cat) && is_numeric($cat)) {
        switch ($cat) {
            case 1:
                print "<img src=\"../../images/faq_header_gen.jpg\" width=\"595\" height=\"201\" alt=\"\" border=\"0\">";
                break;
            case 2:
                print "<img src=\"../../images/faq_header_bo.jpg\" width=\"595\" height=\"201\" alt=\"\" border=\"0\">";
                break;
            case 3:
                print "<img src=\"../../images/faq_header_cr.jpg\" width=\"595\" height=\"201\" alt=\"\" border=\"0\">";
                break;
            case 4:
                print "<img src=\"../../images/faq_header_ec.jpg\" width=\"595\" height=\"201\" alt=\"\" border=\"0\">";
                break;
            default:
                print "<img src=\"../../images/faq_header.jpg\" width=\"595\" height=\"201\" alt=\"\" border=\"0\">";
                break;
        }
    }
    ?>
    ...
  2. Locate, open and backup the inc/parser.php file and change these lines below:

    Code: Select all

    ...
    	function processTemplate($templateName, $myTemplate)
        {
    		global $PMF_CONF;
    ...
    in:

    Code: Select all

    ...
    	function processTemplate($templateName, $myTemplate)
        {
    		global $PMF_CONF;
            // Expose some PMF variables to the template
            global $cat;
    ...
Ciao,
Matteo
phpMyFAQ QA / Developer
Amazon.co.uk Wishlist
natmanu
Posts: 22
Joined: Wed May 24, 2006 7:07 pm

Post by natmanu »

Cheers, all's fine now
Nat
Post Reply