2

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?

  • Hi Jai. Welcome to JSE. What happens when you try get the variable? Do you get any error messages? – TryHarder Feb 20 '15 at 8:41
  • If you're importing the Joomla API, you don't need to write mysqli queries. You can simply use JDatabase – Lodder Feb 20 '15 at 9:28
  • the variable is blank . I get no errors. the reason for mysqli queries is because i am connecting to external database. – Jai Feb 20 '15 at 14:06
  • Did try something else as well to solve the problem. added a update to the original post hope you can help.@Lodder @moomoochoo – Jai Feb 20 '15 at 14:57
  • Even with an external database, you can use JDatabaseDriver – Lodder Feb 20 '15 at 23:20
1

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'); 
0

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');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.