Thursday 7 February 2013

Generate the Unique Alias or Unique string in PHP or Joomla

Generate the Unique Alias or Unique string in PHP or Joomla The below code is to check the Alias in joomla 2.8
/**
  * Find unique Alias name if current doesn't exist.
  * @param string $alias
  * 
  * @return string Return the unique alias value.
  */
 protected function getUniqueAlias($alias)
 {
  $alias_ini = $alias;
  $i = 0;
  
  while ($this->isAliasExist($alias))
  {
   $alias = $alias_ini .'-'.++$i;   
  }
  return $alias;
 }
 
 /**
  * Check the 'alais' in the database.
  *
  * @return boolean If found return true else falase.
  */
 protected function isAliasExist($alias) 
 {
  // Initialise variables.
  $db  = $this->getDbo();
  $query = $db->getQuery(true);
  
  $query->select('m.id, m.alias');
  $query->from('#__bmembers m');
  $query->where('m.alias="'.$alias.'"');
  $db->setQuery((string)$query);
  
  $results = $db->loadObjectList();
  
  return (count($results)>0) ? true : false;
 }


No comments:

Post a Comment

Please post any queries and comments here.