Related items of a list in the details page

anikolao

New Member
Hi, is there an easy way to have a filtered list in the details page of an item? To give you an easy example:

We have a list of classmates:
ID Class Name
1 1 John <-- select
2 1 Peter
3 1 Jonathan
4 2 Luke
5 2 Maria
6 2 Sophie

We then select row 1 - John and go the the details page that shows John's details. Is it possible to also show the same list but filtered where Class is the same: i.e.

Details Page
Class: 1
Name: John

Classmates
ID Class Name
2 1 Peter
3 1 Jonathan

Thanks
Achilles
 
Yup, although it'll require a little custom coding. You'll need to install Sourcerer (from Regular Labs) so you can embed some code in the outro of the details:

Once Sourcerer is installed, you should have a "<> code" button top right of the editor. So edit the Footer Text of your form, hit the button, and insert ...

(first read the comments and change the table/field names)

Code:
{source}
<?php
$app = JFactory::getApplication();

// get the view, make sure it's details not form
$view = $app->input->get('view', 'form');

if ($view === 'details') {
   // we're in a details view, so get the rowid
   $rowid = $app->input->get('rowid', '');

   if (!empty($rowid)) {
      // we got a rowid, so look the row up and get the class_id
      $db = JFactory::getDbo();
      $query = $db->getQuery(true);
      // EDIT HERE - change the field and table names to suit
      $query->select('class_id')->from('classmates')->where('id = ' . (int)$rowid);
      $db->setQuery($query);
      $classId = $db->loadResult();

      // if we got a class ID, output a content plugin string for the list filtering it by class_id
      // EDIT HERE - change the list id and the full element name of the class_id to suite (but keep the _raw suffix)
      if (!empty($classId)) {
         echo "{fabrik view=list id=123 classmates___class_id_raw=$classId}";
      }
   }
}
?>
{/source}

So you grab the current rowid from the request, lookup the class_id in your table for that row, then render a {fabrik ...} content plugin for the list, filtering on that class_id.

Make sure you enable "Run Joomla Plugins" on the form settings.

-- hugh
 
Thanks Hugh, I am also trying to pass an array to the list but doesn't seem to work, I use the following code. The list loads fine but the results don't really match.

Code:
{source}
<?php
$app = JFactory::getApplication();


// get the view, make sure it's details not form
$view = $app->input->get('view', 'form');
if ($view === 'article') {
// we're in a details view, so get the rowid
$rowid = $app->input->get('rowid', '');


if (!empty($rowid)) {
// we got a rowid, so look the row up and get the ebp_application_id
$db = JFactory::getDbo();
$query = $db->getQuery(true);
// EDIT HERE - change the field and table names to suit
$query->select(array('ebp_application_id'))->from('uspe5_ebp_lubricants_repeat_ebp_application_id')->where('parent_id = ' . (int)$rowid);
$db->setQuery($query);
$results = $db->loadObjectList();

foreach($results as $value){
   foreach($value as $key => $data){
$appid=$data."|".$appid;
   }
}

echo $appid;

// if we got a ebp_application_id, output a content plugin string for the list filtering it by ebp_application_id
if (!empty($appid)) {
echo "{fabrik view=list id=10 clearfilters=1 uspe5_ebp_lubricants___ebp_application_id_raw=$appid}";
}
}
}
?>
{/source}
 
Wow, well I just learned something new. I was about to reply and say no, you can use an array, as what those plugin strings do is imitate query string filters, as if you'd add &table___element=foo on the query string. And you can't put an array on the query string.

But on a hunch I tried using the "IN" condition on a query string, with a comma separated list of ids, and it seemed to work. So try this ...

Code:
// get the view, make sure it's details not form
$view = $app->input->get('view', 'form');
if ($view === 'article') {
// we're in a details view, so get the rowid
$rowid = $app->input->get('rowid', '');


if (!empty($rowid)) {
   // we got a rowid, so look the row up and get the ebp_application_id
   $db = JFactory::getDbo();
   $query = $db->getQuery(true);
   // EDIT HERE - change the field and table names to suit
   $query->select('ebp_application_id')->from('uspe5_ebp_lubricants_repeat_ebp_application_id')->where('parent_id = ' . (int)$rowid);
   $db->setQuery($query);

   // load the results as a single column array
   $results = $db->loadColumn();

   // if we got any results, output a content plugin string for the list filtering it by ebp_application_id
   // explode the array of id's into a comma separated string for the IN filter
   if (!empty($results)) {
      echo "{fabrik view=list id=10 clearfilters=1 uspe5_ebp_lubricants___ebp_application_id_raw[value]=" . explode(',' $results) . "&uspe5_ebp_lubricants___ebp_application_id_raw[condition]=IN}";
   }
}
 
Thanks Hugh, this is good stuff. I have made some changes to the code as it wasn't working for me. See below

PHP:
{source}
<?php
$app = JFactory::getApplication();

// get the view, make sure it's details not form
$view = $app->input->get('view', 'form');
if ($view === 'article') {
    // we're in a details view, so get the rowid
    $rowid = $app->input->get('rowid', '');

    if (!empty($rowid)) {
        // we got a rowid, so look the row up and get the ebp_application_id

        $db = JFactory::getDbo();
        $db->setQuery('SET sql_big_selects=1');
        $db->query();
        // form the query
        $query[] = 'select parent_id';
        $query[] = 'from uspe5_ebp_lubricants_repeat_ebp_application_id';
        $query[] = 'where ebp_application_id in';
        $query[] = '(select ebp_application_id from uspe5_ebp_lubricants_repeat_ebp_application_id where parent_id=' . (int)$rowid . ')';
        $query = implode(' ', $query);

        $db->setQuery($query);

        // load the results as a single column array
        $results = $db->loadColumn();
  
        // Set results to a comma delimited string
        $lubid=implode(",",$results);

        // if we got a lubid, output a content plugin string for the list filtering it by id
        if (!empty($results)) {
            echo "{fabrik view=list id=10 uspe5_ebp_lubricants___id[value]=$lubid uspe5_ebp_lubricants___id[condition]=IN}";
        }
    }
}
?>
{/source}
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top