Get dropdown value shown in form within PHP script

Status
Not open for further replies.

VOI

Member
Is there an easy way to get the label value of a dropdown field instead of the raw value within a PHP script?

When using $this->data['dropdownname'] I get the raw value (e.g. "fb") instead of the label value shown in the HTML form (e.g. "foo bar").

Cheers

Chris
 
Its within a PHP template for the email plugin.

I could use the placeholders, e.g. {tablename___dropdownname}, but this gives me the dropdown label value including a HTML bullet point, which I need to avoid.

Chris
 
friendly bump

I need this function not only in the email plugin mentioned, but also to retrieve the label of a dropdown element which I need to save in a additional calc field. Using 'return {tablename___elementname}' in the calc field returns nothing, so I'm using {tablename___elementname_raw}, which gives me the value (e.g. 'fb').

Is there any PHP command to get me the corresponding label (e.g. 'foobar')?

I had a look at "dropdown.php" in "/plugins/fabrik_element/dropdown" - but the only function close to this "replaceLabelwithValues" is protected and does just the opposite of what I am looking for.

Chris
 
I already tried that command - with no succes ("strip_tags" btw. "striptags" produces a HTTP 500 error).
I also used the PHP replace function to get rid of all tags - still no success. The HTML Code of the mail shows the follwing for each time I echo a dropdown label:

<ul><li>option label</li></ul>

Any ideas?

Still, this would only solve my problem concering the email plugin.

By now I have come by various situations where I need to get the dropdown label for a dropdown element within a PHP script. I understand that the mapping of the values and labels is stored in the params of the element. Is there any fabrik pre-defined php function to access that mapping?

Thanks for your help.

Chris
 
Created a separate table 'jos_fabrik_dropdowns' which I populate using Fabirk Schedule and a php script (which was the hardest part to program) on a daily basis with the values and labels of all my dropdown elements.
In the PHP scripts and calc elements I am using this mapping table to translate the values returned by {tablename___element_raw} into the dropdown label.

Its more complicated than I hoped for but now that I got it up and running I am quite happy with the solution.

Here's the PHP script I created:

PHP:
<?php
# Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();


# empty current dropdowns table
$db = JFactory::getDbo();
$query = "TRUNCATE `jos_fabrik_dropdowns`"; 
$db->setQuery($query);

# get dropdowns and their parameters from $query = $db->getQuery(true);
$query = $db->getQuery(true);
$query = "SELECT `jos_fabrik_elements`.`name`, `jos_fabrik_elements`.`params` FROM `jos_fabrik_elements` WHERE `jos_fabrik_elements`.`plugin`= 'dropdown'";
$db->setQuery($query);
$dropdowns = $db->loadObjectList();

# start of query to insert dropdown values and labels
$query = $db->getQuery(true);
$query = "INSERT INTO `jos_fabrik_dropdowns` (`name`, `value`, `label`) VALUES ";

# do for each dropdown
foreach ($dropdowns as $dropdown) {  
  # decode json encoded parameters
  $dropdown_map = json_decode($dropdown->params, true);
  # array with all values of this dropdown
  $values = $dropdown_map['sub_options']['sub_values'];
  # array with all labels of this dropdown
  $labels = $dropdown_map['sub_options']['sub_labels'];
  for($i = 0; $i < count($values); $i++) {
    # create recordset for SQL statement
    $query .= "('".mysql_real_escape_string($dropdown->name)."','".$values[$i]."','".mysql_real_escape_string($labels[$i])."'),";
  }
}

# remove trailing comma
$query = rtrim($query, ',');
    
# insert records in table
$db->setQuery($query);
$result = $db->query();

?>

Chris
 
I just tend to use join elements rather than dropdown elements. So instead of creating your value/label pairs as "proprietary" attributes in the Fabrik elements table as part of a dropdown element, create them as data in a table, with a join element.

Table: which_color
id, color
1, Blue
2, Red
3, Green

Create join element "Favorite Color", join to which_color table, key 'id', label 'color'

Makes any kind of post-processing of your data easier.

-- hugh
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top