Monday 25 February 2013

Print the query string in Joomla

Print the query result into a string in Joomla
// Then the query result into a string.
print_r($query->dump()); die;

Monday 18 February 2013

301 Redirection from Multiple Domains

If you have domains: domain1.com, domain2.com and domain3.com and you want to do 301 redirection.
In this case, first:
  1. Just host the domain1.com into the server.
  2. Park the domain2.com and domain3.com under the domain1.com hosting account. 
  3. Then, create a .htaccess file on the domain1.com /public_html folder in the server. 
  4. And, the following code into the .htaccess file which is for making 301 redirection (page to page redirection) to domain1.com.
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain1\.co\.uk
RewriteRule (.*) http://www.domain1.co.uk/$1 [R=301,L]

Saturday 9 February 2013

PHP Code to Clear the Browser (Local) Cache

PHP Code to Clear the Browser (Local) Cache including Joomla and Wordpress CMS


Thursday 7 February 2013

Insert Multiple Records using Single Query in Joomla

Insert multi-record using single query in Joomla.

 /**
  * Insert the attributes into website.
  *
  * @return boolean If found return true else falase.
  */
 protected function saveAttributes($member_id, $data) 
 {
  // Build the values for multi-row insert.
  $values = array();
  foreach ($data as $attribute_id => $value)
  {
   $values[] = "('".$member_id."','".$attribute_id."','".$value."', '1')";
  }
  $values = implode(',', $values);
  
  // Build the insert query
  $sql = 'INSERT INTO #__bmember_attribute_vals (`mid`, `attribute_id`, `value`, `state`)
    VALUES'.$values;
  
  $db  = $this->getDbo();
  $query = $db->getQuery(true); // Reset the $query variable.
  $db->setQuery($sql);
  $db->query();
 }

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;
 }


Tuesday 5 February 2013

To clear $query object, to dump the query and nesting loop in Joomla

To clear the previous values from the query var.
/* This needs to be added while using nesting loop to clear the previous query in Joomla Component Writing. */
$query = $db->getQuery(true);
This is to print the built query or dump query.
/* This is to print the built query or dump query. */
echo $query->dump();
Find the sample query as below;
  $data = array();       
  $query->select('c.id, c.alias, c.title');
  $query->from('#__categories c');
  $query->where('c.alias not in ("admin") and c.extension="com_bmember" and published="1"');
  $query->order('c.title asc');
  $db->setQuery((string)$query);
  $categories = $db->loadObjectList();
  
  if (is_array($categories) && ($categories)>0)
  {
   $i=0;
   foreach ($categories as $category)
   {
    $data[$i]['catid'] = $category->id;
    $data[$i]['category'] = $category->title;
    $data[$i]['attributes'] = array();
    
    $query = $db->getQuery(true); // This needs to be added while using nesting loop to clear the previous query.
    $query->select('af.id, af.attribute, af.alias');
    $query->from('#__bmember_attribute_fields af');
    $query->where('af.catid="'.$category->id.'" and af.state="1"');
    $db->setQuery((string)$query);
    $rows = $db->loadObjectList();
    $query->order('af.attributes asc');
    $i++;
   }
  }