Header menu

_________________________________________________________________________________

Tuesday, 24 September 2013

Could not create directory. /opt/lampp/htdocs/wordpress-test/wp-content/upgrade(Theme installation problem in wordpress)

Could not create directory. /opt/lampp/htdocs/wordpress-test/wp-content/upgrade

 Could not create directory. /opt/lampp/htdocs/wordpress-test/wp-content/upgrade

This error occur when you try to install some new theme on wordpress .
And your wp-content is not writable.





To correct this error

Change permissions on the /wp-content directory to 0775
 

if not work Change permissions on the /wp-content directory to 0777.

And dont forget to restore your permission after work is done as security measure.

Monday, 23 September 2013

Fatal error : Call to undefined function add_menu_page()

Fatal error : Call to undefined function add_menu_page()

If you want to add menu to admin section of wordpress through custom plugin and you got
"Fatal error : Call to undefined function add_menu_page() " error.

Then reason for error might be there is no add_action function found in plugin

Try this code to solve this error in your plugin file.

<?php

/*
Plugin Name: Myplugin

Description:
Version: 2.5.324
Author: Amit kumar


*/
add_action('admin_menu', 'test');

function test(){
    add_menu_page(__('My Menu Page'), __('My Menu'), 'edit_themes', 'my_new_menu', 'my_test_function', '', 7);
}

function my_test_function(){
    echo "hello world .. !!";
}

Saturday, 21 September 2013

Upload file in cakephp using move_uploaded_file function

move_uploaded_file()

Easiest way to upload file in cakephp is by using move_uploded_file function.
But it beneficial when you have to put file uploader at few place. If you need
to place large number of form which upload file then use component method otherwise
this is easier and much faster way.

I need one multipart form which can select file to upload i have  created one in
index.ctp and rest of the work will be done by index function in controller.



This is my controller file

<?php
class ContactsController extends ContactManagerAppController {
    public $uses = array('ContactManager.Contact');

    public function index() {
        if($this->request->is('post')){
            pr($this->request->data);
            $filePath = "./img/".$this->request->data['Contact']['image_file']['name']; 
            if(move_uploaded_file($this->request->data['Contact']['image_file']['tmp_name'], $filePath)){
            echo "File uploaded Successfully";
            }
       }
     } 
}








This is view for my Controller


index.ctp
<?php
    echo $this->Form->create('Contact', array('enctype' => 'multipart/form-data', 'class'=>'form-horizontal'));
?>
    <label class="control-label" for="inputError">Image</label>
<?php
    echo $this->Form->file('image_file');
    echo $this->Form->submit(__('Submit') , array('class'=>'btn btn-primary'));
?>




Thursday, 19 September 2013

Implode and Explode function of Php

Implode and Explode function of Php


<?php
// function 1:  implode()
echo "<h1>IMPLODE</h1>";
$arr=array("test1","test2","test3","test4","test5","test6","test7","test7");
echo "<pre>";  print_r($arr); echo "</pre>";

//implode(glue, pieces)

echo "Array with glue=none";
echo "</br>";echo "</br>";
$str=implode('', $arr);
echo $str;

echo "</br>";echo "</br>";
echo "Array with  ',' as glue to get comma separarted value from array ";
echo "</br>";echo "</br>";
$str=implode(',', $arr);
echo $str;
echo "</br>";echo "</br>";
// implode() function is used to join array elements and return string

// function 2:  explode(delimiter, string)
echo "<h1>EXPLODE</h1>";
echo $str="This is my string i am going to explode";
echo "</br>";echo "</br>";
echo "First using space as delimiter";
echo "<pre>";  print_r($arr); echo "</pre>";
$arr=explode(' ',$str);

echo "</br>";echo "</br>";
echo "Now using t as delimiter";
$arr=explode('t',$str);
echo "<pre>";  print_r($arr); echo "</pre>";
// explode breaks string into array where delimiter comes

?>

Array() / Array_fill() / Array_Keys() in php

Array() / Array_fill() / Array_Keys() in php

<?php
// function 1:  array()
$arr=array();
echo "Empty array created";
echo "<pre>";  print_r($arr); echo "</pre>";
// array() create an empty array

// function 2:  array_fill(start_index, num, value)
$arr=array_fill(0, 5, "test");
echo "Array filled with value";
echo "<pre>";  print_r($arr); echo "</pre>";
$arr[1]="failed";
$arr[3]="failed";
echo "Array after changes";
echo "<pre>";  print_r($arr); echo "</pre>";
// array_fill() fill the array with value given in parameter

// function 3:  arrays_keys(array,value)
echo "Keys where value is test in array";
$keyz=array_keys($arr,"test");
echo "<pre>";  print_r($keyz); echo "</pre>";
// arrays_keys() return the array of keys where value is same as parameter

?>

Monday, 16 September 2013

Send and receive data in ajax/jquery

How to send and receive data in ajax/jquery 

Sample Ajax tutorial in php



We will need two file for this tutorial ...
1. which send request and receive responce (test.php)
2. which receive request and send response (page.php)

On click of button ..the id of button will be send to page.php

And the response which we will get will replace html content of span id "val" .

test.php

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" >
$(document).ready(function(){
    $( ".button" ).on("click",function(){

        $.ajax({
        type:"post",
url: "page.php",
data: {id:$(this).attr('id')},
success: function( data ) {
$( "#val" ).html( "<strong>" + data + "</strong> " );
}
});
        });
});
</script>
</head>
<body>
<button type="button" id="b1" class="button">Button-1</button>
<button type="button" id="b2" class="button">Button-2</button>
<button type="button" id="b3" class="button">Button-3</button>
</br></br></br>
<span id="val" style="background-color:#454282;">Which button is clicked ?<span>

</body>

</html>

---------------------------------------------------------------------------------------------------------------------------
page.php

<?php
$id=$_POST['id'];
if($id=="b1"){
echo "Button 1 is clicked ";
}
if($id=="b2"){
echo "Button 2 is clicked ";
}
if($id=="b3"){
echo "Button 3 is clicked ";
}

?>

Friday, 13 September 2013

Append some (text/img) after some html element using jquery

How to append some (text/img) after some html element using jquery 

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script type="text/javascript" >
        $(document).ready(function(){
var i=0;
        $("#sub").on("click",function(){
     $( "#1" ).append("Test "+i+" ... ");
         i++;
         });      
         });
</script>
</head>
<body>
<div id="1">This is my test...</div>
<button type="button"  id="sub">Append</button>
</form>
</body>
</html>

Prepend some (text/img) before some html element using jquery

How to prepend some (text/img) before some html element using jquery 

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script type="text/javascript" >
        $(document).ready(function(){
var i=0;
            $("#sub").on("click",function(){
$( "#1" ).prepend("Test "+i+" ... ");
                 i++;
                 });      
            });
 
        </script>
        </head>
    <body>
<div id="1">This is my test...</div>
<button type="button"  id="sub">Prepend</button>
</form>
</body>

</html>

Thursday, 12 September 2013

Write sql queries in cakephp controller

How to write sql queries in cakephp controller 

public function index(){
    $result= $this->User->query("SELECT * FROM users ;");  
}

User :- It is the name of my Model
users:- Table name in database

$result:- contain array of result of sql query

Get session information in cakephp

How to get session information in cakephp 

In the controller you can use

$this->Session->read();

to get all details of session variable . And you can use

$this->set('session',$this->Session->read());


to use these values in corresponding view page.

-----------------------------------------------------------------------------------------------------------------------

If you wish to use session variable in each page then use set method in appcontroller to set its value

public function beforefilter(){
$this->set('session',$this->Session->read());
}

Now this session variable is accessible in every page of view.

Note: this should be placed in Appcontroller...

Using some other file as index file when no index file is in the directory using .htaccess

Alternative index file using .htaccess

When there is no index file in php directory and you want that some other file act as index file then this code can be helpful
search is done from left to right ... whichever file is found first will be shown as index file


DirectoryIndex index.php index1.php index2.php index3.php

# u can use any name you want in place of index1.php

Commenting in the .htaccess file

Commenting in the .htaccess file

#Commenting in the .htaccess file is done by adding a # at the beginning of the line that you wish to comment.
#This instructs the server to ignore that line in the .htaccess file.

Hide directory information in php when index file is not present using .htaccess.

When no index file is present in directory and you want to hide listing on other files in php then .htaccess can be used to hide directory information.
Just copy the below lines in .htaccess file and put .htaccess file in directory for which you want to hide files list.

Options -Indexes

Wednesday, 11 September 2013

Cakephp 1.2 - include problem of css and js , and improper functioning

Changes to made to be made cake php version 1.2 properly :

1. uncomment the statement in file apache2/conf/httpd.config on wamp server

LoadModule rewrite_module modules/mod_rewrite.so

2. In the same file make sure

 AllowOverride all (if there is none in place of all .. change it to all for your "C:/wamp/www" directory)

3. Make sure that all three .htaccess files have default configuration .. there must not me any change ..

Tuesday, 10 September 2013

Create backup of database using php script


<?php
define('BACKUP_DIR', './Backups' ) ;
// Define  Database Credentials
define('HOST', 'localhost' ) ;
define('USER', 'amit' ) ;
define('PASSWORD', '' ) ;
define('DB_NAME', 'databse' ) ;
/*
Define the filename for the sql file
*/
$fileName = 'mysqlbackup--' . date('d-m-Y') . '@'.date('h.i.s').'.sql' ;
// Set execution time limit
if(function_exists('max_execution_time')) {
if( ini_get('max_execution_time') > 0 )     set_time_limit(0) ;
}

// Check if directory is already created and has the proper permissions
if (!file_exists(BACKUP_DIR)) mkdir(BACKUP_DIR , 0700) ;
if (!is_writable(BACKUP_DIR)) chmod(BACKUP_DIR , 0700) ;
// Create an ".htaccess" file , it will restrict direct accss to the backup-directory .
$content = 'deny from all' ;
$file = new SplFileObject(BACKUP_DIR . '/.htaccess', "w") ;
$file->fwrite($content) ;

$mysqli = new mysqli(HOST , USER , PASSWORD , DB_NAME) ;
if (mysqli_connect_errno())
{
   printf("Connect failed: %s", mysqli_connect_error());
   exit();
}
 // Introduction information
$return = "--\n";
$return .= "--  Mysql Backup \n";
$return .= "--\n";
$return .= '-- Export created: ' . date("Y/m/d") . ' on ' . date("h:i") . "\n\n\n";
$return = "--\n";
$return .= "-- Database : " . DB_NAME . "\n";
$return .= "--\n";
$return .= "-- --------------------------------------------------\n";
$return .= "-- ---------------------------------------------------\n";
$return .= 'SET AUTOCOMMIT = 0 ;' ."\n" ;
$return .= 'SET FOREIGN_KEY_CHECKS=0 ;' ."\n" ;
$tables = array() ;
// Exploring what tables this database has
$result = $mysqli->query('SHOW TABLES' ) ;
// Cycle through "$result" and put content into an array
while ($row = $result->fetch_row())
{
$tables[] = $row[0] ;
}
// Cycle through each  table
 foreach($tables as $table)
 {
// Get content of each table
$result = $mysqli->query('SELECT * FROM '. $table) ;
// Get number of fields (columns) of each table
$num_fields = $mysqli->field_count  ;
// Add table information
$return .= "--\n" ;
$return .= '-- Tabel structure for table `' . $table . '`' . "\n" ;
$return .= "--\n" ;
$return.= 'DROP TABLE  IF EXISTS `'.$table.'`;' . "\n" ;
// Get the table-shema
$shema = $mysqli->query('SHOW CREATE TABLE '.$table) ;
// Extract table shema
$tableshema = $shema->fetch_row() ;
// Append table-shema into code
$return.= $tableshema[1].";" . "\n\n" ;
// Cycle through each table-row
while($rowdata = $result->fetch_row())
{
// Prepare code that will insert data into table
$return .= 'INSERT INTO `'.$table .'`  VALUES ( '  ;
// Extract data of each row
for($i=0; $i<$num_fields; $i++)
{
$return .= '"'.$rowdata[$i] . "\"," ;
 }
 // Let's remove the last comma
 $return = substr("$return", 0, -1) ;
 $return .= ");" ."\n" ;
 }
 $return .= "\n\n" ;
}
// Close the connection
$mysqli->close() ;
$return .= 'SET FOREIGN_KEY_CHECKS = 1 ; '  . "\n" ;
$return .= 'COMMIT ; '  . "\n" ;
$return .= 'SET AUTOCOMMIT = 1 ; ' . "\n"  ;
$zip = new ZipArchive() ;
$resOpen = $zip->open(BACKUP_DIR . '/' .$fileName.".zip" , ZIPARCHIVE::CREATE) ;
if( $resOpen ){
$zip->addFromString( $fileName , "$return" ) ;
    }
$zip->close() ;
$fileSize = get_file_size_unit(filesize(BACKUP_DIR . "/". $fileName . '.zip')) ;
$message = "BACKUP  completed ";
echo $message ;


function get_file_size_unit($file_size){
switch (true) {
    case ($file_size/1024 < 1) :
        return intval($file_size ) ." Bytes" ;
        break;
    case ($file_size/1024 >= 1 && $file_size/(1024*1024) < 1)  :
        return intval($file_size/1024) ." KB" ;
        break;
    default:
    return intval($file_size/(1024*1024)) ." MB" ;
}
}

?>

Monday, 9 September 2013

Session variable in cakephp

PHP session  allows you to store user information on the server for later use (i.e. username, time,visited urls etc), this information is temporary and
will be deleted after the user has left the website.

To access session variable in cakephp... Go to controller in which you want to see session variable and add following two lines in code..

$session=$this->Session->read();
pr($session);

Join us ...

Design form in cakephp

Note:Input name should be same as field name in database

Form to be written in .ctp in view

<?php
echo $this->Form->create('Model_name');
echo $this->Form->input('field1');
echo $this->Form->input('field2', array('rows' => '3'));
echo $this->Form->end('submit');
?>

Friday, 6 September 2013

Create two different url for admin and front site in cakephp

Most easy way to do this is by the use of scaffold variable.

Open the controller for which you want to make different admin page
and add the following statement:

public $scaffold='admin';

Now to go to app /config/core.php
and add the following statement:

Configure::write('Routing.prefixes', array('admin'));

Now create function for admin in controller like
public function admin_index(){
   
}


Now you access your admin using http://www.domain.com/admin/controller_name
and site using http://www.domain.com/controller_name

Thursday, 5 September 2013

Find position of string in a given string

Strpos Function


<html>
<body>
    <?php
        $str1="test";
        $str2="This is a testing string ";
        $pos=strpos($str2,$str1);
        echo "Given String : ".$str2."</br>";
        echo "String to be matched : ".$str1."</br>";
        echo "Match found at position : ".$pos;
        ?>
    </body>
</html>

Tuesday, 3 September 2013

How to block an ip address from visiting your website using .htaccess ?

1. Make a .htaccess file and save it in your root directory.
2. copy the code below:

order allow,deny
deny from 255.0.0.0   (blocked address)
deny from 192.168.1.96  (blocked address)
allow from all




3.  Paste in file and change the ip address to which you want to block.

4.  save the file


How to send Email with Attachment in PHP ?


Follow the link below and you will get the best tutorial for sending email with file attachment ...


Download Link:

Tutorial to send email with attachment

Monday, 2 September 2013

Access html form field using their name in jquery

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script type="text/javascript" >
        $(document).ready(function(){
            $("#sub").on("click",function(){
                $('input[name="bio[]"]').each(function() {
                    console.log($(this).val());
            });return false;
        });
    });
        </script>
        </head>
    <body>
<form action="#">
First name: <input type="text" name="bio[]"><br>
Last name : <input type="text" name="bio[]"><br>
Phone     : <input type="text" name="bio[]"><br>
Address   : <input type="text" name="bio[]">
<input type="submit" value="submit" id="sub"><br>
</form>
</body>
</html>