Show distinct column values

A collection of tutorials and posts that show additional functionality of MATE

Moderator: KarelB

Show distinct column values

Postby KarelB » Thu Apr 12, 2012 2:15 pm

This example will show how to create a small popup with the distinct column values for a column in the current MATE table, eg it will honor any filters or search criteria.

I am not going to explain the whole process but the setup should be very simple:
- add the column option 'distinct_info" and set it to true
- override the AjaxTableEditor class with the following class file (paid version):
Code: Select all
<?php
require_once('../mate/php/AjaxTableEditor.php');
class Distinct_AjaxTableEditor extends AjaxTableEditor
{
   function displayTableHtml()
   {
      $html = '';
      $numRows = 0;
      $this->extraOrderByInfo = empty($this->extraOrderByInfo) ? '' : ', '.$this->extraOrderByInfo;
      // Added tick marks using implod/explode in case custom order by columns have periods in them.
      $query = $this->selectClause.' '.$this->joinClause.' '.$this->whereClause.' order by '.$this->addTickMarks($this->orderByColumn).' '.$this->getAscOrDesc().' '.$this->extraOrderByInfo.' limit '.$this->start.', '.$this->displayNum;
      if($this->viewQuery)
      {
         $this->information[] = '<div id="mateViewQuery" align="left">'.nl2br($query).'</div>';
      }
      $result = $this->doQuery($query);
      $mysqlNumRows = mysql_num_rows($result);
      $html .= '<div><form id="'.$this->varPrefix.'_table_form" style="margin: 0px;"><table '.$this->tableInfo.'>';
      if($mysqlNumRows > 0 || stristr($this->permissions,'F'))
      {
         $html .= '<tr id="'.$this->varPrefix.'_header_row" class="header">';
         if(stristr($this->permissions,'M'))
         {
            $html .= '<td id="select_all_cb_cell" width="40" align="left"><input type="checkbox" id="select_all_cb" onclick="selectCbs(this,\''.$this->varPrefix.'\');" /></td>';
         }
         
         if(stristr($this->permissions,'I') && $this->iconColPosition == 'first')
         {
            $html .= strlen($this->iconTitle) > 0 ? '<td>'.$this->iconTitle.'</td>' : '<td>&nbsp;</td>';
         }
         
         foreach($this->tableColumns as $col => $info)
         {
            if(stristr($info['perms'],'T'))
            {
               $colHeaderInfo = isset($info['col_header_info']) ? $info['col_header_info'] : '';
               if(stristr($this->permissions,'F') && stristr($info['perms'],'F'))
               {
                  $filterStr = isset($this->filterSearches[$col]) ? $this->filterSearches[$col] : '';
                  $filterData = $this->jsonEncode(array($col,$filterStr));
                  //var_dump($filterData);
                  //$colHeaderInfo .= ' headers="'.$filterData.'"';
                  $colHeaderInfo .= ' filterCol="'.$col.'" filterStr="'.htmlspecialchars($filterStr).'"';
               }
               
               $distinctInfo = $info['distinct_info'] ? $this->returnDistinctValues($col) : '';
               if($this->orderByColumn == $col)
               {
                  list($oppAscOrDesc,$arrow) = $this->ascOrDesc == 'asc' ? array('desc','&uarr;') : array('asc','&darr;');
                  $html .= '<td '.$colHeaderInfo.' >'.$distinctInfo.'<a href="javascript: toAjaxTableEditor(\'order_by_changed\', new Array(\''.$col.'\',\''.$oppAscOrDesc.'\'),{updateHistory: true});">'.$info['display_text'].'</a> '.$arrow.'</td>';
               }
               else
               {
                  $html .= '<td '.$colHeaderInfo.' >'.$distinctInfo.'<a href="javascript: toAjaxTableEditor(\'order_by_changed\', new Array(\''.$col.'\',\'asc\'),{updateHistory: true});">'.$info['display_text'].'</a></td>';         
               }
            }
         }
         foreach($this->userColumns as $column)
         {
            $html .= isset($column['title']) ? '<td>'.$column['title'].'</td>' : '<td>&nbsp;</td>';
         }
         if(stristr($this->permissions,'I') && $this->iconColPosition == 'last')
         {
            $html .= strlen($this->iconTitle) > 0 ? '<td>'.$this->iconTitle.'</td>' : '<td>&nbsp;</td>';
         }
         $html .= '</tr>';
      }
      if($mysqlNumRows > 0)
      {
         $bgColor = $this->evenRowColor;
         while($row = mysql_fetch_assoc($result))
         {
            $numRows++;
            $rowInfo = array();
            $rowSets = array();
            $rowSets['class'] = 'ajaxRow';
            $bgColor = $bgColor == $this->oddRowColor ? $this->evenRowColor : $this->oddRowColor;
            $rowSets['bgcolor'] = $bgColor;
            $cb = '';
            if(stristr($this->permissions,'M'))
            {
               if(is_callable($this->disableMultCbFun) && call_user_func($this->disableMultCbFun,$row))
               {
                  //$cb = '<td>&nbsp;</td>';
                  $cb = '<td><input class="rowCheckBox" type="checkbox" id="cb_'.$numRows.'" value="'.$row[$this->primaryKeyCol].'" disabled="disabled" /></td>';
               }
               else
               {
                  $rowSets['onclick'] = 'cellClicked(\''.$numRows.'\');';
                  $rowSets['style'] = 'cursor: pointer;';
                  $cb = '<td><input class="rowCheckBox" type="checkbox" id="cb_'.$numRows.'" onclick="checkBoxClicked(this)" value="'.$row[$this->primaryKeyCol].'" /></td>';                  
               }
            }
            $extraRowInfo = strlen($this->extraRowInfo) > 0 ? str_replace(array($this->replaceWithId,'#rowNum#'),array($row[$this->primaryKeyCol],$numRows),$this->extraRowInfo) : '';
            
            if(isset($this->modifyRowSets) && is_callable($this->modifyRowSets))
            {
               $rowSets = call_user_func($this->modifyRowSets,$rowSets,$row,$numRows);
            }
            foreach($rowSets as $attr => $value)
            {
               $rowInfo[] = $attr.'="'.$value.'"';
            }
            
            $html .= '<tr id="row_'.$numRows.'" '.implode(' ',$rowInfo).' '.$extraRowInfo.'>'.$cb;
            if(stristr($this->permissions,'I') && $this->iconColPosition == 'first')
            {
               $html .= $this->formatIcons($row[$this->primaryKeyCol],$row);
            }            
            foreach($this->tableColumns as $col => $info)
            {
               if(stristr($info['perms'],'T'))
               {
                  $value = $row[$col];
                  $tableCellInfo = isset($info['table_cell_info']) ? $info['table_cell_info'] : '';
                  if(isset($info['table_fun']) && is_callable($info['table_fun']))
                  {
                     $value = call_user_func($info['table_fun'],$col,$value,$row);
                  }
                  $value = strlen(trim($value)) > 0 ? $value : '&nbsp;';
                  if(isset($info['sub_str']) && strlen($value) > $info['sub_str'])
                  {
                     $value = substr($value,0,$info['sub_str']).'...';
                  }
                  if($this->searchString && $this->useHighlight && $value != '&nbsp;')
                  {
                     $value = $this->highlightSearchString($this->searchString,$value);
                  }
                  $html .= '<td '.$tableCellInfo.'>'.$value.'</td>';
               }
            }
            foreach($this->userColumns as $column)
            {
               if(isset($column['call_back_fun']) && is_callable($column['call_back_fun']))
               {
                  $html .= call_user_func($column['call_back_fun'],$row);
               }
            }
            if(stristr($this->permissions,'I') && $this->iconColPosition == 'last')
            {
               $html .= $this->formatIcons($row[$this->primaryKeyCol],$row);
            }
            $html .= '</tr>';
         }
         $html .= '</table></form></div>';
      }
      else
      {
         $html .= '</table></form></div>';
         $html .= '<div><b>'.$this->langVars->ttlNoRecord.'</b></div>';
         /*
         if(stristr($this->permissions,'F'))
         {
            $this->retArr[] = array('layer_id' => 'filterLayer', 'where' => 'innerHTML', 'value' => '');
         }
         */
      }
      $this->retArr[] = array('layer_id' => 'tableLayer', 'where' => 'innerHTML', 'value' => $html);
   
      if($this->recordInfo)
      {
         if($this->numResults > 0)
         {
            $end = $this->displayNum + $this->start;
            $end = $end < $this->numResults ? $end : $this->numResults;
            $start = $this->start + 1;
            $recordHtml = '<div>'.sprintf($this->langVars->ttlDispRecs,$start,$end,number_format($this->numResults)).'</div>';
         }
         else
         {
            $recordHtml = '<div>'.$this->langVars->ttlDispNoRecs.'</div>';
         }
         $this->retArr[] = array('layer_id' => 'recordLayer', 'where' => 'innerHTML', 'value' => $recordHtml);
      }
      
      if(is_callable($this->tableScreenFun))
      {
         call_user_func($this->tableScreenFun);
      }
   }

    function returnDistinctValues($col)
   {
      $this->formatDistinctClause($col);
      
      $info = $this->tableColumns[$col];
      $dis_query = $this->distinctClause.' '.$this->joinClause.' '.$this->whereClause.' order by '.$this->addTickMarks($this->orderByColumn).' '.$this->getAscOrDesc().' '.$this->extraOrderByInfo.' limit '.$this->start.', '.$this->displayNum;
      if($this->viewQuery)
      {
         $this->information[] = '<div id="mateViewQuery" align="left">'.nl2br($dis_query).'</div>';
      }
      $dis_result = $this->doQuery($dis_query);
      $dis_mysqlNumRows = mysql_num_rows($dis_result);
      $distArr =array();
      while($dis_row = mysql_fetch_assoc($dis_result))
      {
         array_push($distArr,$dis_row[$col]);
      }
      sort($distArr);
      $distStr = implode(', ',$distArr);
      $distinctCmd = '<a href="javascript:alert(\''.$info['display_text'].' :'.$distStr.'\')" >
                               <img src="../images/list.gif" title="Distinct Values" border=0 style="vertical-align:middle" >
                               </a>&nbsp;&nbsp;';
      return $distinctCmd;
   }
   
    function formatDistinctClause($col)
   {
      $sets = array();
      $info = $this->tableColumns[$col];
      {
         if(isset($info['join']) && is_array($info['join']))
         {
            $info['join']['display_mask'] = isset($info['join']['display_mask']) ? $info['join']['display_mask'] : $info['join']['alias'].'.'.$info['join']['column'];
            $sets[] = $info['join']['display_mask'].' as '.$this->addTickMarks($col);
         }
         else if(isset($info['display_mask']))
         {
            $sets[] = $info['display_mask'].' as '.$this->addTickMarks($col);
         }
         else
         {
            $sets[] = $this->addTickMarks($this->dbAndTable.'.'.$col);
         }
      }
      $this->distinctClause = 'select distinct'.implode(', '.$this->queryLineBreak,$sets).$this->queryLineBreak.'from '.$this->dbAndTable.$this->queryLineBreak;
   }
}
?>


Check the paths in the first line of the class file for your setup. And create an small gif image and set the correct path in the line for the $distinctCmd (around line 203)

I leave it up to the reader to create a more stylish popup or dialog.
KarelB
 
Posts: 458
Joined: Mon Dec 01, 2008 11:49 pm
Location: The Netherlands

Re: Show distinct column values

Postby hexxamillion » Fri May 18, 2012 5:01 pm

i just noticed you stated that this is for paid version. Is there a solution if you do not have the paid version?
hexxamillion
 
Posts: 100
Joined: Fri Jul 08, 2011 11:36 pm

Re: Show distinct column values

Postby KarelB » Sat May 19, 2012 10:06 am

You probably cannot cut and paste the posted solution in the free version only in the paid version, hence the remark about the paid version.
But the method for the free version stays the same since it doesn't use any paid version features.
KarelB
 
Posts: 458
Joined: Mon Dec 01, 2008 11:49 pm
Location: The Netherlands


Return to How To

Who is online

Users browsing this forum: No registered users and 4 guests

cron