Tuesday, March 1, 2016

How to shift php array key pair value to top or bottom

MOVE KEY VALUE PAIR TO THE TOP/BOTTOM OF AN ARRAY

<?php
class demo{
   public function arratData(){
      $myArray= array (
                 'bk101' => array (
                                'author' => 'Gambardella, Matthew',
                                'title' => 'XML Developer\'s Guide',
                                'genre' => 'Computer',
                                'price' => '44.95',
                                'publish_date' => '2000-10-01',
                                'id' => 'bk101'
                                  ),
                 'bk102' => array (
                                'id' => 'bk102',
                                'author' => 'Ralls, Kim',
                                'title' => 'Midnight Rain',
                                'genre' => 'Fantasy',
                                'price' => '5.95',
                                'publish_date' => '2000-12-16',
                                'description' => 'A former architect battles corporate zombies...',
                                  ),
                        );
 
    echo '<pre>Actual Array set <br />';
    print_r($myArray);
 
    // call the function move_to_top
    // i want ID of "bk101" should be at top
 
    $top = $this->move_to_top($myArray['bk101'],'id');
 
    echo '<h4>ID of "bk101 will be at top which is at bottom in actual array"</h4>';
    print_r($top);
 
 
    // Now call the function move_to_bottom
    // i want ID of "bk102" should be at bottom which is at top right now
 
    $bottom = $this->move_to_bottom($myArray['bk102'],'id');
 
   echo '<h4>ID of "bk102 will be at bottom which is at top in actual array"</h4>';
    print_r($bottom);
 
 
 
 
   }
 
    public  function move_to_top(&$array, $key) {
        $temp = array($key => $array[$key]);
        unset($array[$key]);
        return $array = $temp + $array;
    }

    public  function move_to_bottom(&$array, $key) {
         
            $temp = array($key => $array[$key]);
            unset($array[$key]);
            return $array = $array + $temp;
    }
}

$a=new demo;
$show_data=$a->arratData();
echo $show_data;
?>

Output





Download File

Tuesday, February 9, 2016

How to Deploy Symfony 2.8 or 3.0 Project on Linux Hosting Server.

After spending several day finally i success to launch my symfony project on share hosting.
Guys ! Its very diffrent to work in xampp / Wamp than Production server in symfony case.


I will tell you story step by step.
I developed a small project in symfony 2.8 which need some ajax call, some CRUD operation.
that project was running like a charm at my localhost. Then finally I upload all files into my shared server under  "public_html" (which turn wrong for me). using this method, some time my page was blank or some time i was geeing un-understandable  error :-).

Finally i got one solution to that work for me on Linux shared hosting .

Follow Below steps
  • Before uploading any thing delete everything under "App/Chache"
  • upload "WEB" folder which is inside of  your symfony Project  into "/public_html"
  • Now Go up one label from /public_html. you  will see  "/"  in remote site path 
  • upload all Remain Folders of your symfony project here. 
  • Never forgot to update your DB Details in " app/config/parameters.yml

Thats it. Go to the url, you will must see the running page.

Note-: i will brief you my Shared Hosting Details which i performed.
  •            Server -: Linux
  •            Type -:    Share hosting at Hioxindia pvt ltd
  •            Framework version-: Symfony 2.8
  •            Localy developed with  Wamp + PHPMyadmin



Here is snap shot of my Test Project 



Tuesday, February 2, 2016

Make Mysql Database Connection using PDO || Very Easy to learn

1.Config.php
<?php
     
        error_reporting(E_ALL);
        try
        {
 $pdo = new PDO('mysql:host='. DB_HOST .';dbname='.$selected_db, DB_USER, DB_PASSWORD);  // with db connect
             $db = new PDO( "mysql:host=localhost", 'root', '' ); without DB.. just an open  connection
        }
        catch (PDOException $e)
        {
            $db='Error: '.$e->getMessage();
            die($db);
        }
        return $db;
?>



2. logic.php
 
   <?php
       $db = include_once('config.php');
        try{
         
         $q_object = $db->query("select password('my_hash_password') as m_password");
     
         $get_data = $q_object->fetch(PDO::FETCH_ASSOC);
         echo '<pre>';
         print_r($get_data);
      }
      catch(PDOException $e){
          echo 'Some Error Found<br />'.$e->getMessage();
          die();
      }

Thursday, January 28, 2016

How to create table backup using native Php Script ( Using PHP/PDO )

Take database backup using PHP Script

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

<?php
session_start();
$con = include("connection.php");


function backup_db($con){
/* Store All Table name in an Array */
$allTables = array();

$query = $con->query('SHOW TABLES');

$result=$query->fetchAll(PDO::FETCH_COLUMN);
$allTables=$result;    

$return ='';
foreach($allTables as $table){
$result_tab = $con->query('SELECT * FROM '.$table);
$num_fields = $result_tab->columnCount();

$return.= 'DROP TABLE IF EXISTS '.$table.';';

$create_query = $con->query('SHOW CREATE TABLE '.$table);
$row2 = $create_query->fetch(PDO::FETCH_NUM);
$return.= "\n\n".$row2[1].";\n\n";

for ($i = 0; $i < $num_fields; $i++)
    {
        $eachRow =$result_tab->fetchAll(PDO::FETCH_NUM);
        foreach($eachRow as $keys=>$row)
        {
                $return.= 'INSERT INTO '.$table.' VALUES(';
                for($j=0; $j<$num_fields; $j++){
                    $row[$j] = addslashes($row[$j]);
                    $row[$j] = str_replace("\n","\\n",$row[$j]);
                        if (isset($row[$j]))
                            {
                                $return.= '"'.$row[$j].'"' ;
                            }
                            else
                            {
                               $return.= '""';
                            }
                if($j<($num_fields-1))
                {
                    $return.= ',';
                }
                }
            $return.= ");\n";
         }
    }
    $return.="\n\n";
}

// Create Backup Folder
$folder = 'sql/';
if (!is_dir($folder))
mkdir($folder, 0777, true);
chmod($folder, 0777);

$date = date('m-d-Y-H-i-s', time());
$filename = $folder."db-backup-".$date.'.sql';
$_SESSION['backupFile'] = $filename; // you can found out this file path while importing
$handle = fopen($filename,'w+');
fwrite($handle,$return);
fclose($handle);
}

// Call the function
backup_db($con);
?>



 How to import sql backup using php script ( Using PHP/PDO )