Retrieving item data e.g. for use in RSforms

  • Published by
    George Papadakis
  • Last modified
    28 October 2013
  • Up to date
    Yes
  • Profile concerned
    Developer
  • Concerns
    Component
  • Since Version
    1.5.6
  • Voting
    Average rating
    7 votes
    • 1
    • 2
    • 3
    • 4
    • 5
  • Favourites
    244 Retrieving item data e.g. for use in RSforms /documentation/tutorials-english/79-forms-and-submission/244-retrieving-item-data-eg-for-use-in-rsforms.html

  1. Retrieving a basic field of the item, like title and category title:

    $db = & JFactory::getDBO();
    // Set a different item id if you don't want the current item ...
    $fcitem_id= JRequest::getInt('id',0);
    $fccat_id= JRequest::getInt('cid',0);
    $view= JRequest::getVar('view');
    if ($view!='items' && $view!='item' && $view!='article') {
       return "not in items view";
    }
     
    // RETRIEVE item data
    if ($fcitem_id) {
       $query = 'SELECT c.*'
          .' FROM '.'#'.'__content AS c '
          .' WHERE c.id = ' . $fcitem_id;
     
       $db->setQuery($query);
       $itemdata = $db->loadObject();
       $item_title = $itemdata->title;
    }
     
    // NOTE $itemdata now CONTAINS all item's basic fields
    // (but not values for FLEXIcontent fields)
     
    // RETRIEVE item's category data
    if ($fccat_id) {
       $query = 'SELECT c.*'
          .' FROM '.'#'.'__categories AS c'
          .' WHERE c.id = ' . $fccat_id;
     
          $db->setQuery($query);
          $catdata = $db->loadObject();
          $cat_title = $catdata->title;
    }
     
    // NOTE $catdata now CONTAINS all item's category basic fields
     
    // FINAL STEP: --IF-- your code is for using in a RSform field
    // then you need to return it:
    // so, USE ONE of the following return statements
     
    // e.g.
    return $item_title;
    //e.g.
    return $cat_title;
    // e.g.
    return $item_title ." - ".$cat_title;
     

     

  2. Retrieving the value of a custom field:

    $db = & JFactory::getDBO();
    // Set a different item id if you don't want the current item ...
    $fcitem_id= JRequest::getInt('id',0);
    $fccat_id= JRequest::getInt('cid',0);
    $view= JRequest::getVar('view');
    if ($view!='items' && $view!='item' && $view!='article') {
       return "not in items view";
    }
     
    $field_id=45;  // CHANGE THIS TO THE ID OF YOUR FIELD
    if ($fcitem_id) {
       $query = 'SELECT c.title, c.alias, f.value'
          .' FROM '.'#'.'__content AS c '
          .' LEFT JOIN '.'#'.'__flexicontent_fields_item_relations AS f'
          .' ON f.item_id = c.id AND f.field_id='.(int)$field_id 
          .' WHERE c.id = ' . $fcitem_id;
     
       $db->setQuery($query);
       $itemdata = $db->loadObject();
       $item_title = $itemdata->title;
    }
    return $itemdata->value; 
     
To get the label instead of the value of indexed fields (fields that have a value::label) like radio, radioimage, checkbox, checkboximage, select, selectmultiple
replace:
return $itemdata->value;
with:
require_once (JPATH_ADMINISTRATOR.DS.'components/com_flexicontent/defineconstants.php');
require_once("components/com_flexicontent/classes/flexicontent.fields.php");
require_once("components/com_flexicontent/classes/flexicontent.helper.php");
 
$query = 'SELECT * FROM '.'#'.'__flexicontent_fields WHERE id='.(int)$field_id;
$db->setQuery($query);
$field = $db->loadObject();
$field->parameters = FLEXI_J16GE ? new JRegistry($field->attribs) : new JParameter($field->attribs);
 
$elements = FlexicontentFields::indexedField_getElements( $field, $itemdata );
$label_of_value = $elements[$itemdata->value]->text;
 
return $label_of_value;
 

NOTE: Click to make this item favourable to be notified of updates

ALSO read this article:

-- Retrieving the HTML DISPLAY of FC fields inside 3rd-party code