Search code examples
mysqljoomla-k2

Why is this wrong in joomla?


using joomla with k2 I am trying to call from my database the category that is assigned with my item title and I use this code.In order to achieve this. But something is wrong. I do get the title but I am not getting the category name. Below is the code that I am using to get it:

    ?php
$url= $_SERVER['REQUEST_URI'];
$data = parse_url($url);
$number = basename($data['path'], '.html');
$needle="-";
$number1=substr($number, 0, strpos($number, $needle));
?>
<?php
$db =& JFactory::getDBO(); 
$db->setQuery('SELECT title FROM #__k2_items WHERE id="'.$number1.'"');
$databaseTitle = $db->loadResult();
$db->setQuery('SELECT catid FROM #__k2_items WHERE id="'.$number1.'"');
$databaseCatid= $db->loadResult();
$db->setQuery('SELECT #__k2_categories.name FROM #__k2_categories INNER JOIN #__k2_items ON #__k2_categories.id = #__k2_items.catid WHERE #__k2_items.catid ="$databaseCatid" LIMIT 1');
$databaseNamecat= $db->loadResult();
?>
<h2>Submit a Charter Request for <?php echo $databaseTitle." from port ".$databaseNamecat; ?></h2>

Solution

  • because you are enclosing a variable between double quotes.

    try this

     $db->setQuery("SELECT #__k2_categories.name FROM #__k2_categories INNER JOIN #__k2_items ON #__k2_categories.id = #__k2_items.catid WHERE #__k2_items.catid ='".$databaseCatid."' LIMIT 1 ");