Thanks for your encouragements.
on the example with input6, you link to template input3, small mistake on your side I think
Yes the txt files are not totally in line with template html files. I will correct this.
Find below the selectTv (renamed AsSelectTv). This snippet like the snippet radioButton or inputText will be provided with AjaxSearch 1.9.0 in the toolbox. SelectTv get from the database the values of a TV to set up the appropriate option list for the <select></select>. All the parameters are explained in the header of file. &filter allows to filter some values, &first allow to add a first non-database value.
I use this snippet, to update the selected value when I display the results on the same page as the form. Like on the demo site (I economize the number of pages
) If you display the resuls on a landing page, this snippet is useless and you could write your <select></select> tag as you want.
<?php
/* -----------------------------------------------------------------------------
* Snippet: AsSelectTv
* ------------------------------------------------------------------------------
*
* @author Coroico - www.modx.wangba.fr
* @version 1.0.0
* @date 04/04/2010
*
* Purpose:
* output the values of a tv as a select tag with options
*
* tplName : name of the template
* tvName : name of the tv
* filter : a comma separated list of values to exclude from the select
* first : a string value to add in first position in the select
* class : a class to style the select tag
*
* output : a <select></select> tag
*
* e.g: [[AsSelectTv? &tvName=`articleCategory` &tplName=`wgbArticle` &filter=`Architecture` &first=`` &class=`select1`]]
*
*/
// =============================================================================
if (!class_exists('AsSelectTv')) {
class AsSelectTv {
function AsSelectTv() {
}
function display($name, $tplName, $tvName, $filter, $first, $valueSelected, $class) {
$valuesTv = $this->getValuesTv($tplName,$tvName); // get the values from the tv
$filtered = explode(',',$filter);
$valuesTv = array_diff($valuesTv, $filtered); // filter the list of tvs
sort($valuesTv); // sort the array
$class = ($class) ? 'class="' . $class . '"' : '';
$output = '<select name="' . $name . '" ' . $class . '>';
if ($first != 'no first') { // add a first item
$selected = ($valueSelected == $first) ? "selected" : "";
$output .= '<option ' . $selected . '>' . $first . '</option>'. "\n\r";
}
foreach($valuesTv as $tv) {
$selected = ($valueSelected == $tv) ? "selected" : "";
$output .= '<option ' . $selected . '>' . $tv . '</option>'. "\n\r";
}
$output .= '</select>';
return $output;
}
/*!
* getValuesTv : return an array of all distinct values of a tv
*/
function getValuesTv($tplName, $tvName) {
global $modx;
$valuesTv = array();
$sql = "SELECT DISTINCT tvc.value AS name ";
$sql .= "FROM " . $modx->getFullTableName('site_templates') . " st , " . $modx->getFullTableName('site_tmplvars'). " tv ";
$sql .= "INNER JOIN " . $modx->getFullTableName('site_tmplvar_templates')." tvtpl ON tvtpl.tmplvarid = tv.id ";
$sql .= "INNER JOIN " . $modx->getFullTableName('site_tmplvar_contentvalues')." tvc ON tvc.tmplvarid=tv.id ";
$sql .= "WHERE st.templatename = '" . $tplName . "' AND tv.name = '" . $tvName . "' ";
$sql .= "ORDER by tvc.value";
$rs= $modx->db->query($sql);
while($row = $modx->db->getRow($rs)) {
$tvs = explode(',',$row['name']);
$tvs = array_map("trim", $tvs); // trim each value
$valuesTv = array_merge($valuesTv, $tvs);
}
$valuesTv = array_unique($valuesTv);
return $valuesTv;
}
}
}
// =============================================================================
if (!isset($name)) {
echo "<h2>a name should be specified for the select !</h2>";
return;
}
if (!isset($tvName)) {
echo "<h2>a tv name should be specified !</h2>";
return;
}
if (!isset($tplName)) {
echo "<h2>a template name should be specified !</h2>";
return;
}
$first = isset($first) ? $first : 'no first';
$class = isset($class) ? $class : '';
if (isset($_POST[$name]) && ($_POST[$name])) $valueSelected = $_POST[$name];
else if (isset($_GET[$name]) && ($_GET[$name])) $valueSelected = $_GET[$name];
else $valueSelected = '';
$stv = new AsSelectTv();
$output = $stv->display($name, $tplName, $tvName, $filter, $first, $valueSelected, $class);
echo $output;
?>