Tuesday, January 29, 2013

Display All files and Contents in a Directory.

Display all files name with content.

(Using RecursiveDirectoryIterator PHP >=5.1.2)

-is_file check the each object values in directory list. suppose file name is occurred display the filename and content.
-highlight_file() display the file content.

<?php

 $path = "C:/xampp/htdocs/samplejoomla/application/controllers/administrator/";

      $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path),  RecursiveIteratorIterator::SELF_FIRST);

      foreach($objects as $name => $object){
          echo '<b>'.$name."</b> \n <br/>";
          if(is_file($name))
          {
            echo highlight_file($name);
           
          }
           echo "<br/><br/><br/>";

          }

?>

How to display the file and directory list.

This code run php versions >= 5.1.2

This code is very useful and simple display the particular path directory and file list.

<?php

$path = "C:/xampp/htdocs/samplejoomla/application/controllers/administrator/";


$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);

foreach($objects as $name => $object){
   echo "$name\n"."<br/>";
  }


?>

How to display PHP code

In My Php File content display the html page use this code.

Create two files one is sample.php and another one is display.php

My file name : sample.php

Path : E:/xampp/htdocs

sample.php  (Content ):

<?php
  echo "test";
?>

Code :

This file name dispaly.php

<?php

$fname = $_SERVER['DOCUMENT_ROOT']."/sample.php";
echo "Php Code Display <br/>";
echo highlight_file($fname);

?>

 you run this url in your local server  http://localhost/dispaly.php

Output :


 Php Code Display


<?php
  echo "test";
?>


Wednesday, January 23, 2013

MySQL - UPDATE query with LIMIT

Create a New Table .Table name is employee.

Query:

CREATE TABLE `testing`.`employee` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 255 ) NOT NULL ,
`dept` VARCHAR( 255 ) NOT NULL ,
`salary` DOUBLE NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;


Insert the 6 rows:

Query:


INSERT INTO `testing`.`employee` (`id`, `name`, `dept`, `salary`) VALUES (NULL, 'peter', 'dept-A', '4000'), (NULL, 'prince', 'dept-A', '5000');
INSERT INTO `testing`.`employee` (`id`, `name`, `dept`, `salary`) VALUES (NULL, 'palani', 'dept-A', '4000'), (NULL, 'raja', 'dept-A', '5000');
INSERT INTO `testing`.`employee` (`id`, `name`, `dept`, `salary`) VALUES (NULL, 'nagaraj', 'dept-A', '8000'), (NULL, 'vasanth', 'dept-A', '10000');

Query: SELECT * FROM `employee`;

Result:

id     name      dept           salary
----------------------------------------
1     peter      dept-A         4000
2     prince      dept-A         5000
3     palani      dept-A         4000
4     raja      dept-A         5000
5     nagaraj  dept-A         8000
6     vasanth  dept-A         10000


Query: SELECT dept , count(dept) FROM `employee` group by dept;

Result:

dept     count(dept)
-----------------------
dept-A    6


I want to change the last 3 rows dept field values from 'dept-A'  to 'dept-B'.

Use following query.This query implement limit option in update query.

Query :
UPDATE employee as a , (SELECT id,dept FROM `employee` where dept='dept-A' limit 3,3) as b SET a.dept='dept-B'  where a.id=b.id;

After run this query  see the all results using below query.


Query: SELECT * FROM `employee`;


Result:
id     name      dept           salary
----------------------------------------
1     peter      dept-A         4000
2     prince      dept-A         5000
3     palani      dept-A         4000
4     raja      dept-B         5000
5     nagaraj  dept-B         8000
6     vasanth  dept-B         10000


Query : SELECT dept , count(dept) FROM `employee` group by dept;

Result:

dept     count(dept)
-----------------------
dept-A    3
dept-B    3

Monday, January 21, 2013

Joomla Login redirect back to page to the previous page


If you want to redirect after login to the same page please use the code and enjoy ...

$return = JURI::getInstance()->toString();
$url    = 'index.php?option=com_user&view=login';
$url   .= '&return='.base64_encode($return);
$this->setRedirect($url, XiText::_('YOU_MUST_LOGIN_FIRST'));  // if you are in controller

or

JFactory::getApplication()->redirect($url, XiText::_('YOU_MUST_LOGIN_FIRST'));