Forums

output current page title and category

vistech 25 Feb, 2011
Hi, I have a form in which I would like to output the article title and its category name, then display those in the email. I am currently using this code for title only and it works great. How do I get both title and category?


<?php
if ( !$mainframe->isSite() ) { return; }
$doc =& JFactory::getDocument();
//echo '<div>$doc->title :'.print_r($doc->title, true).'</div>';
?>
<input type='hidden' name='doc_title' value='<?=$doc->title?>' />
GreyHead 26 Feb, 2011
Hi vistech,

You should be able to get the category id from the page url
<?php
$catid = JRequest::getInt('catid', '', 'get');
?>

But note that this will only work if the underlying url includes the category id, you may need to add code to handle the case where no value is found.

Bob
vistech 26 Feb, 2011
The emails are going to a user so just having a catid won't work, I need the category title. All articles will be in a category so that's not an issue.
GreyHead 27 Feb, 2011
Hi vistech,

You can look the title up from the id
<?php
$catid = JRequest::getInt('catid', '', 'get');
$db =& JFactory::getDBO();
$query = "
    SELECT `title` 
        FROM `#__categories`
        WHERE `id` = $catid ;
";
$db->setQuery($query);
$title = $db->loadResult();
?>

Bob
vistech 28 Feb, 2011
So here is my code...


<?php
if ( !$mainframe->isSite() ) { return; }
$doc =& JFactory::getDocument();
//echo '<div>$doc->title :'.print_r($doc->title, true).'</div>';
?>

<?php
$catid = JRequest::getInt('catid', '', 'get');
$db =& JFactory::getDBO();
$query = "
    SELECT `title`
        FROM `#__categories`
        WHERE `id` = $catid ;
";
$db->setQuery($query);
$cattitle = $db->loadResult();
?>

<input type='hidden' name='doc_title' value='<?=$doc->title?>' />
<input type='hidden' name='cat_title' value='<?=$cattitle?>' />


...and here is what I am getting for debug output. The category title is not submitted correctly in the emails either.

1. Form passed first SPAM check OK
2. Form passed the submissions limit (if enabled) OK
3. Form passed the Image verification (if enabled) OK
4. Form passed the server side validation (if enabled) OK
5. $_POST Array: Array ( [name] => name [email] => [email]me@gmail.com[/email] [phone] => 111-111-1111 [resume] => test [message] => test [chrono_verification] => jxvut [button_8] => Submit [doc_title] => Document Title [cat_title] => [80612a62d3aa91cd6198efd61a3630f9] => 1 [1cf1] => 3bfc61f04c7ab9e7db1f36bdf9d1ad32 [chronoformname] => test_careers )
6. $_FILES Array: Array ( [upload] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) )
7. Form passed the plugins step (if enabled) OK
8. An email has been SENT successfully from xxx [email]noreply@domian.org[/email] to [email]me@gmail.com[/email]
9. Debug End

GreyHead 28 Feb, 2011
Hi vistech,

Odd, I just ran a quick test with the code on my site and it appears to pick up the Category Title OK.

By all means email or PM me the site URL and a SuperAdmin login and I'll take a quick look.

Bob
GreyHead 01 Mar, 2011
Hi vistech,

I couldn't get any useful information from the page URL -- I don't know why :-( All that is available (or all I could find) is the Menu title as the Page title.

I was able to use this to build a complex MySQL query to get the page link from the jos_menus table, the article id from the link, the category id from the jos_content table and then the Category name from the jos_categories table.
<?php
if ( !$mainframe->isSite() ) { return; }
$doc =& JFactory::getDocument();
$db =& JFactory::getDBO();
$query = "
  SELECT `title` 
    FROM `#__categories` WHERE `id` = ( 
      SELECT `catid` 
        FROM `#__content` 
        WHERE `id` = (
          SELECT SUBSTR(`link` FROM LOCATE('&id=',`link`)+4 ) 
            FROM `#__menu` 
            WHERE `name` = ".$db->quote($doc->title)."
          )
      );
";
$db->setQuery($query);
$cattitle = $db->loadResult();
?>
<input type='hidden' name='doc_title' value='<?php echo $doc->title; ?>' />
<input type='hidden' name='cat_title' value='<?php echo $cattitle; ?>' />


There has to be a better way!

Bob

PS You can see the result in the 'Volunteers Accepted' article.
vistech 23 Mar, 2011
Here is my output. I seem to get the page title but nothing more. Category is not displayed.

1. Form passed first SPAM check OK
2. Form passed the submissions limit (if enabled) OK
3. Form passed the Image verification (if enabled) OK
4. Form passed the server side validation (if enabled) OK
5. $_POST Array: Array ( [name] => me [email] => [email]me@me.com[/email] [phone] => 111 [resume] => [message] => 111 [chrono_verification] => aqwy4 [button_8] => Submit [doc_title] => Page Title [cat_title] => [27fe351265461a938ffc7a099214321b] => 1 [1cf1] => a54c1fae12dc916ac2038182a8fd76d3 [chronoformname] => test_careers )
6. $_FILES Array: Array ( [upload] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) )
7. Form passed the plugins step (if enabled) OK
8. An email has been SENT successfully from (The Website)noreply@me.org to [email]me@gmail.com[/email]
9. Debug End
This topic is locked and no more replies can be posted.