Tuesday, January 19, 2016

CodeIgniter Remove index.php By .htaccess

Steps To Remove index.php using .htaccess:-

Step:-1  Open the file config.php located in application/config path.  Find and Replace the below code in config.php  file.

//  Find the below code

$config['index_page'] = "index.php"

//  Remove index.php

$config['index_page'] = ""

Step:-2  Go to your CodeIgniter folder and create .htaccess  file.


 Path:

Your_website_folder/
application/
assets/
system/
user_guide/
.htaccess <--------- this file
index.php
license.txt

Step:-3  Write below code in .htaccess file

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Step:-4  In some cases the default setting for uri_protocol does not work properly. To solve this issue just open the file config.php located in application/config and then find and replace the code as:

//  Find the below code

$config['uri_protocol'] = "AUTO"

//  Replace it as

$config['uri_protocol'] = "REQUEST_URI" 

Thursday, January 14, 2016

Repeatable Form Field for Joomla

Joomla 3.2 added a nice feature introducing repeatable standard form field. Now it’s easy to add repeat fields just defined from the standard xml file of any joomla extensions. For each repeat box you can add multiple fields (joomla standard form fields ), means you can add fields under fields !

Now we will go more technical but will try our best to write it for new joomla developer.


<field name="list_templates" type="Repeatable" icon="list" description="PLG_TINY_FIELD_TEMPLATE_FIELD_ELEMENTS_DESC"
label="PLG_TINY_FIELD_TEMPLATE_FIELD_ELEMENTS_LABEL">
<fieldset hidden="true" name="list_templates_modal" repeat="true">
<field name="logoFile1" class="" type="media" default="" label="TPL_PROTOSTAR_LOGO_LABEL" description="TPL_PROTOSTAR_LOGO_DESC" />
</fieldset>
</field>

How to use two models in one view - Joomla 3


Sometimes you may wish to reuse a certain function that resides in a model outside of your current scope. This helps you save time and reduce duplication. This can be achieved easily by adding the following codes.

Assuming you are trying to call a model "Categories" (/components/com_mycomponent/models/categories.php) belonging to com_mycomponent (this can be called from either within or outside of the com_mycomponent):

jimport('joomla.application.component.model');
JModelLegacy::addIncludePath(JPATH_SITE.'/components/com_mycomponent/models');
$categoriesModel = JModelLegacy::getInstance( 'Categories', 'MyComponentModel' );
$categoriesModel->getCategories();
<?php
JLoader::import('joomla.application.component.model');
// file name, full path
JLoader::import( 'product', JPATH_ADMINISTRATOR . DS . 'components' .
 DS . 'com_virtuemart' . DS . 'models' );

$productModel = JModel::getInstance( 'Product', 'VirtueMartModel' );
?>

Wednesday, October 7, 2015

Category Sub Category tree with PHP and mysql


Category Sub Category recursive function, To display category sub category on page.


Table for Category:

CREATE TABLE IF NOT EXISTS `category` (
  `cid` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `parent` int(11) NOT NULL,
  PRIMARY KEY (`cid`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Insert some dummy records. Parent columns with zero value denotes its the base/root category.

Example
INSERT INTO `category` (`cid`, `name`, `parent`) VALUES
(1, 'Hardware', 0),
(2, 'Software', 0),
(3, 'Movies', 0),
(4, 'Clothes', 0),
(5, 'Printers', 1),
(6, 'Monitors', 1),
(7, 'Inkjet printers', 5),
(8, 'Laserjet Printers', 5);

Creating category Tree using recursion


function fetchCategoryTree($parent = 0, $spacing = '', $user_tree_array = '') {

  if (!is_array($user_tree_array))
    $user_tree_array = array();

  $sql = "SELECT `cid`, `name`, `parent` FROM `category` WHERE 1 AND `parent` = $parent ORDER BY cid ASC";
  $query = mysql_query($sql);
  if (mysql_num_rows($query) > 0) {
    while ($row = mysql_fetch_object($query)) {
      $user_tree_array[] = array("id" => $row->cid, "name" => $spacing . $row->name);
      $user_tree_array = fetchCategoryTree($row->cid, $spacing . '&nbsp;&nbsp;', $user_tree_array);
    }
  }
  return $user_tree_array;

}

Display the category tree in a dropdown list.

<?php 
$categoryList = fetchCategoryTree();
?>
<select>
<?php foreach($categoryList as $cl) { ?>
  <option value="<?php echo $cl["id"] ?>"><?php echo $cl["name"]; ?></option>
<?php } ?>

</select>

Displaying Category tree in list format

function fetchCategoryTreeList($parent = 0, $user_tree_array = '') {

    if (!is_array($user_tree_array))
    $user_tree_array = array();

  $sql = "SELECT `cid`, `name`, `parent` FROM `category` WHERE 1 AND `parent` = $parent ORDER BY cid ASC";
  $query = mysql_query($sql);
  if (mysql_num_rows($query) > 0) {
     $user_tree_array[] = "<ul>";
    while ($row = mysql_fetch_object($query)) {
 $user_tree_array[] = "<li>". $row->name."</li>";
      $user_tree_array = fetchCategoryTreeList($row->cid, $user_tree_array);
    }
$user_tree_array[] = "</ul>";
  }
  return $user_tree_array;

}

<ul>
<?php
  $res = fetchCategoryTreeList();
  foreach ($res as $r) {
    echo  $r;
  }
?>

</ul>

Tuesday, September 29, 2015

Create Slug


If you want to make a string seo friendly slug in joomla then copy and paste the following function


public function createSlug($name) {
$jinput = JFactory::getApplication()->input;
$template = $jinput->get('template', null, null);

// Transliterate
$lang = new JLanguage($template->get('language', 'general', '', null, 0, false));
$str = $lang->transliterate($name);

// Trim white spaces at beginning and end of alias and make lowercase
$str = trim(JString::strtolower($str));

// Remove any duplicate whitespace, and ensure all characters are alphanumeric
$str = preg_replace('/(\s|[^A-Za-z0-9\-])+/', '-', $str);

// Trim dashes at beginning and end of alias
$str = trim($str, '-');

// If we are left with an empty string, make a date with random number
if (trim(str_replace('-', '', $str)) == '') {
$jdate = JFactory::getDate();
$str = $jdate->format("Y-m-d-h-i-s").mt_rand();
}
return $str;
}

Friday, September 5, 2014

Iframe image upload php

You can download the source code

Index.php

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>Uploader</title>
   <link href="style/style.css" rel="stylesheet" type="text/css" />

<script language="javascript" type="text/javascript">
<!--
function startUpload(){
      document.getElementById('f1_upload_process').style.visibility = 'visible';
      document.getElementById('f1_upload_form').style.visibility = 'hidden';
      return true;
}

function stopUpload(success){
      var result = '';
      if (success == 1){
         result = '<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';
      }
      else {
         result = '<span class="emsg">There was an error during file upload!<\/span><br/><br/>';
      }
      document.getElementById('f1_upload_process').style.visibility = 'hidden';
      document.getElementById('f1_upload_form').innerHTML = result;
      document.getElementById('f1_upload_form').style.visibility = 'visible';      
      return true;   
}
//-->
</script>   
</head>
<body>
       <div id="container">
            <div id="header"><div id="header_left"></div>
            <div id="header_right"></div></div>
            <div id="content">
                <form action="upload.php" method="post" enctype="multipart/form-data" target="upload_target" onSubmit="startUpload();" >
                     <p id="f1_upload_process" style="visibility:hidden;">Loading...<br/><img src="loading.gif" /><br/></p>
                     <p id="f1_upload_form" align="center"><br/>
                         <label>File:  
                              <input name="myfile" type="file" size="30" />
                         </label>
                         <label>
                             <input type="submit" name="submitBtn" class="sbtn" value="Upload" />
                         </label>
                     </p>
                     <iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
                 </form>
             </div>             
         </div>

</body>   

</html>
---------------------------

Upload.php

<?php
   // Edit upload location here
   $destination_path = getcwd().DIRECTORY_SEPARATOR;

   $result = 0;

   $target_path = $destination_path . basename( $_FILES['myfile']['name']);

   if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
      $result = 1;
   }

   sleep(1);
?>

<script language="javascript" type="text/javascript">window.top.window.stopUpload(<?php echo $result; ?>);</script>   

Thursday, September 4, 2014

Extracting ZIP file directories in PHP

Here is the code.

<?php  $toDir= getcwd();
$fromFile = "joomlacomponent.zip";
if (!file_exists($toDir))
mkdir($toDir, 0777);
if (class_exists('ZipArchive', false))
{
$zip = new ZipArchive();
if ($zip->open($fromFile) === true AND $zip->extractTo($toDir) AND $zip->close())
return true;
return false;
}
die();
?>