Get the menu item variable using Jinput

by Jai   Last Updated March 27, 2015 05:03 AM

I am using joomla 3.3.6 and have added a menu item type variable. I want to get the value of this variable using jinput to query the database.

The controller file is external so i added the framework but the value of variable is not available.

<?php
// Get Joomla! framework 
define( '_JEXEC', 1 ); 
define( '_VALID_MOS', 1 ); 
define( 'JPATH_BASE', realpath(dirname(__FILE__))); 
define( 'DS', DIRECTORY_SEPARATOR ); 
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' ); 
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' ); 
$mainframe =&JFactory::getApplication('site'); 
$mainframe->initialise(); 

$jinput = JFactory::getApplication()->input;
$city = $jinput->get('city');


include('config.php');
$mysqli->set_charset("utf8");
if($stmt=$mysqli->prepare("SELECT * from test where City=?"));{
$stmt->bind_param("s",$city);
$result=$stmt->execute();
$result = $stmt -> get_result();
$arr = array();
if($result->num_rows >"0") {
while($row = $result->fetch_assoc()) {
        $arr[] = $row;  
    }
}
# JSON-encode the response
$json_response = json_encode($arr);

// # Return the response
echo $json_response;

}
?>

The xml file for the menu variable is

<metadata>

    <layout title="COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLE">

        <message>COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_DESC</message>

    </layout>

    <fields name="request">
        <fieldset name="request">
            <field
                    name="city"
                    type="list"
                    label="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL"
                    description="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC"
                    default="London">
                                <option value="London">London</option>
                                <option value="Paris">paris</option>
                                <option value="New Delhi">New Delhi</option>
                                <option value="Amsterdam">Amsterdam</option>
                                <option value="Las vegas">Las vegas</option>
                                <option value="Birmingham">Birmingham</option>
                                <option value="new york">new york</option>
                                <option value="mumbai">mumbai</option>
                        </field>
        </fieldset>
    </fields>

</metadata>

Hope you can help.

-----------------------------------Update---------------------------------------------

Deine a problem: i am trying to get the variable value from menu item so i can run a query . i have set a menu item variable as described above but it is not working.

As a alternative i am trying to get the menu item alias so i can use that in my query and get results.

Below is the code I tried but i get some warnings but no variable value.(added joomla framework to external file)

<?php
// Get Joomla! framework 
define( '_JEXEC', 1 ); 
define( '_VALID_MOS', 1 ); 
define( 'JPATH_BASE', realpath(dirname(__FILE__))); 
define( 'DS', DIRECTORY_SEPARATOR ); 
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' ); 
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' ); 
$mainframe =&JFactory::getApplication('site'); 
$mainframe->initialise(); 

$menu = JFactory::getApplication()->getMenu();
$alias = $menu->getActive()->alias;


$jinput = JFactory::getApplication()->input;
$city = $jinput->get('city');

include('config.php');
$mysqli->set_charset("utf8");
//$rcity ="london";
if($stmt=$mysqli->prepare("SELECT * from ratedmain where City=?"));{
$stmt->bind_param("s",$alias);
$result=$stmt->execute();
$result = $stmt -> get_result();
$arr = array();
if($result->num_rows >"0") {
while($row = $result->fetch_assoc()) {
        $arr[] = $row;  
    }
}
# JSON-encode the response
$json_response = json_encode($arr);

// # Return the response
echo $json_response;
echo $parentname;


}
?>

Strict standards: Only variables should be assigned by reference in C:\wamp\www\tets\test.php on line 9

Trying to get property of non-object in C:\wamp\www\test\test.php on line 16

Trying to get property of non-object in C:\wamp\www\test\test.php on line 17

Am I using the right method to get the menu item alias?



Answers 2


If you run this controller file directly you can't get the the active menu item because the application is not rendered yet.

You only init the application by using $mainframe->initialise(); but to start it you need to use $mainframe->execute();, however running this will render the whole application and the code below this call will not be called at all. You can try this by yourself by replacing $mainframe->initialise(); with $mainframe->execute();

To solve this you need to put this controller inside your component, then you can get the params and the alias:

$menu = JFactory::getApplication()->getMenu();
$menuItem = $menu->getActive();
$alias = $menuItem->alias;
$yourParamValue = $menuItem->params->get('your_param_name');

To fix the error on line 9 you need to remove "&" symbol.

$mainframe = JFactory::getApplication('site'); 
Hung Tran
Hung Tran
February 25, 2015 07:41 AM

Use the following code to get the menu item field values.

$app = JFactory::getApplication();    
$menuid = $app->getMenu()->getActive()->id;
$params = $app->getMenu()->getParams( $menuid );
$city = $params->get('city', 'London');
Nagarjun
Nagarjun
April 26, 2015 11:31 AM

Related Questions


Updated May 26, 2015 11:04 AM

Updated June 05, 2015 22:04 PM

Updated June 11, 2015 18:04 PM

Updated February 27, 2016 01:04 AM

Updated May 19, 2016 07:46 AM