PHP : How to add condition in array?

protected function getHeadRowValues()
{
    return array(
        'Order Number',
        'Order Date',
        'Order Status'
      )
}
=============================
protected function getHeadRowValues()
{

    $nArray = array();
    if (true) {
        $nArray[] = $order->getRealOrderId();
    }
    if (strpos($exportOrderData2, 'Order Date') !== false) {
        $nArray[] = Mage::helper('core')->formatDate($order->getCreatedAt(), 'medium', true);
    }
    if (strpos($exportOrderData2, 'Order Status') !== false) {
        $nArray[] = $order->getStatus();
    }
    return $nArray;
}

PHP : How to detect browser and operating system (os) with php

<?PHP
class OS_BR{

private $agent = “”;
private $info = array();

function __construct(){
$this->agent = isset($_SERVER[‘HTTP_USER_AGENT’]) ? $_SERVER[‘HTTP_USER_AGENT’] : NULL;
$this->getBrowser();
$this->getOS();
}

function getBrowser(){
$browser = array(“Navigator”            => “/Navigator(.*)/i”,
“Firefox”              => “/Firefox(.*)/i”,
“Internet Explorer”    => “/MSIE(.*)/i”,
“Google Chrome”        => “/chrome(.*)/i”,
“MAXTHON”              => “/MAXTHON(.*)/i”,
“Opera”                => “/Opera(.*)/i”,
);
foreach($browser as $key => $value){
if(preg_match($value, $this->agent)){
$this->info = array_merge($this->info,array(“Browser” => $key));
$this->info = array_merge($this->info,array(
“Version” => $this->getVersion($key, $value, $this->agent)));
break;
}else{
$this->info = array_merge($this->info,array(“Browser” => “UnKnown”));
$this->info = array_merge($this->info,array(“Version” => “UnKnown”));
}
}
return $this->info[‘Browser’];
}

function getOS(){
$OS = array(“Windows”   =>   “/Windows/i”,
“Linux”     =>   “/Linux/i”,
“Unix”      =>   “/Unix/i”,
“Mac”       =>   “/Mac/i”
);

foreach($OS as $key => $value){
if(preg_match($value, $this->agent)){
$this->info = array_merge($this->info,array(“Operating System” => $key));
break;
}
}
return $this->info[‘Operating System’];
}

function getVersion($browser, $search, $string){
$browser = $this->info[‘Browser’];
$version = “”;
$browser = strtolower($browser);
preg_match_all($search,$string,$match);
switch($browser){
case “firefox”: $version = str_replace(“/”,””,$match[1][0]);
break;

case “internet explorer”: $version = substr($match[1][0],0,4);
break;

case “opera”: $version = str_replace(“/”,””,substr($match[1][0],0,5));
break;

case “navigator”: $version = substr($match[1][0],1,7);
break;

case “maxthon”: $version = str_replace(“)”,””,$match[1][0]);
break;

case “google chrome”: $version = substr($match[1][0],1,10);
}
return $version;
}

function showInfo($switch){
$switch = strtolower($switch);
switch($switch){
case “browser”: return $this->info[‘Browser’];
break;

case “os”: return $this->info[‘Operating System’];
break;

case “version”: return $this->info[‘Version’];
break;

case “all” : return array($this->info[“Version”],
$this->info[‘Operating System’], $this->info[‘Browser’]);
break;

default: return “Unkonw”;
break;

}
}
}

// using
// create an new instant of OS_BR class
$obj = new OS_BR();
// // if you want to show one by one information then try showInfo() function

// get browser
$obj->showInfo(‘browser’);

// get browser version
$obj->showInfo(‘version’);

// get Operating system
$getOs = $obj->showInfo(‘os’);

// get all information and it returns an array

?>

tow column dynamic logic in this code

<?php
$result = $this->getBestsellerProduct();
$itemPerRow = ($this->getItemsPerRow()) ? $this->getItemsPerRow() : 2 ;
//getting product model

$model = Mage::getModel(‘catalog/product’);
$collectionSize = count($result);
$firstDiv = round($collectionSize/2);
$_columnCount = 2;
?>
<div>
<div>
<div> <h1><?php echo $this->__(‘Best Seller’); ?></h1></div>
<div>Below the top selling products</div>
</div>

<div>
<?php
//echo $result->count();
if($result->count())
{
$j=1;$cnt = 0;
//for($cnt = 0; $cnt<count($result); $cnt++)
foreach($result as $_products)
{
$_products=Mage::getModel(‘catalog/product’)->load($_products->getId());
//$_product = $model->load($result[$cnt]); //getting product object for particular product id
?>
<?php if ($cnt <= $firstDiv-1)
{ ?>
<?php if($cnt==0) {?>
<div>
<ul>
<?php } ?>
<li><a href=”<?php echo $this->getBaseUrl().$_product[‘url_path’] ?>” title=”<?php echo $_products->getName() ?>”><?php echo $_products->getName() ?> </a></li>
<?php if($cnt==$firstDiv-1) {?>
</ul>
</div>
<?php }?>
<?php }
else
{ ?>
<?php if($j==1) { ?>
<div>
<ul>
<?php }?>
<li><a href=”<?php echo $this->getBaseUrl().$_product[‘url_path’] ?>” title=”<?php echo $_products->getName() ?>”><?php echo $_products->getName() ?> </a></li>
<?php if($cnt== $collectionSize-1) {?>
</ul>
</div>
<?php }?>
<?php $j++; }
$cnt ++;
}
}
else {?>
<div align=”center”><strong>There are no products to show.</strong></div>
<?php }
?>
</div>
</div>

PHP redirect without header()


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function redirect($url)
{
    if (!headers_sent())
    {
        header('Location: '.$url);
        exit;
    }
    else
    {
        echo '<script type="text/javascript">';
        echo 'window.location.href="'.$url.'";';
        echo '</script>';
        echo '<noscript>';
        echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
        echo '</noscript>'; 
        exit;
    }
}

PHP – Uploads file to FTP server programmatically using PHP script

PHP it self provide us the FTP functions library which allow to implement client access to files servers (FTP). This library providing a wide range of control to executing script including read or write file to FTP server. This post will particularly post show you how to Send Files to send file using php script. Here is the  script which allow you to upload file to FTP server,

<?php
    // Connect to server
    $conn = ftp_connect("ftp.yourserver.com") or die("Could not connect");

    ftp_login($conn,"username","password") or die("invelid username or password");

    // move to path where you need to upload file
    ftp_chdir($conn, "/upload/") or die("could not find dir");

    // upload file to particular path
    $upload = ftp_put($conn,"allproduct.xml","allproduct.xml",FTP_BINARY);

    if (!$upload) { echo 'FTP upload failed!'; }

    ftp_close($conn);
?>

Some time you need to create dir dynamically on server, in that case you can use bellow code instead of change dir,

if(!ftp_chdir($conn, $folder_path))
{
    $create_folder = ftp_mkdir($conn, $folder_path);
    ftp_chmod($conn, 0777, $folder_path);
    ftp_chdir($conn, $folder_path);
}

Above part of script will check for particular dir. If it could not find, it will create that dir and moved to particular dir path. Then you can continue to upload part.