Monday 28 December 2015

Recent Product History Script in PHP For Shopping Cart

<?php
    @session_start();
    if(!isset($_SESSION["lastviewed"]))    
    {
      $_SESSION["lastviewed"] = array();
    }
   
    $maxelements = 5;
    if(isset($_GET['ProductID']) && $_GET['ProductID']<>"")
    {// if we have url parameter
   
        if (in_array($_GET['ProductID'],$_SESSION["lastviewed"]))
        { // if product id is already in the array
            $_SESSION["lastviewed"] = array_diff($_SESSION["lastviewed"],array($_GET['ProductID'])) ; // remove it
            $_SESSION["lastviewed"] = array_values($_SESSION["lastviewed"]); //optionally, re-index the array
        }
   
        if(count($_SESSION["lastviewed"]) >= $maxelements)
        {//check the number of array elements
            $_SESSION["lastviewed"] = array_slice($_SESSION["lastviewed"],1); // remove the first element if we have 5 already
            array_push($_SESSION["lastviewed"],$_GET['ProductID']);//add the current itemid to the array
        }
        else
        {
            array_push($_SESSION["lastviewed"],$_GET['ProductID']);//add the current itemid to the array
        }
    }
   
    echo "<pre>";
    print_r($_SESSION["lastviewed"]);

?>


Wednesday 4 November 2015

MySQL ordering results by specific field values

In MySQL we can sort the results in ascending or descending order very easily by using the ORDER BY clause. However, there are times when you want to sort the results in a specific order which cannot be done using the ASC or DSC. FIELD() of MySQL ORDER BY clause can be used to sort the results in a specific order.

You can use ORDER BY and FIELD function

SELECT * FROM table ORDER BY FIELD(ID,1,5,4,3)
 
 

Thursday 24 September 2015

move_uploaded_file ( ) - Move an uploaded file Sample

/* get file name here */
        $file_name=$_FILES['profile_img']['name'];
        $file_temp_name=$_FILES['profile_img']['tmp_name'];
       
        $path_name="profile/".$file_name;
       
        if($_FILES['profile_img']['error']>0)
        {
            echo "Return Code : ".$_FILES['profile_img']['error']."</br>";
        }
        else
        {
            $allowed =  array('gif','png' ,'jpg');
            $filename = $_FILES['profile_img']['name'];
            $ext = pathinfo($filename,PATHINFO_EXTENSION);
            if(!in_array($ext,$allowed))
            {
                echo 'File is not allowed';
            }
            else
            {
                /* Move file here */
                $move_result=move_uploaded_file($file_temp_name,$path_name);
                if($move_result==TRUE)
                {
                    /*  Write your success code here */   
                }
        }

Javascript Validation While Image Uploading

<script>
function Checkfiles()
{
    var fup = document.getElementById('profile_img');
    var fileName = fup.value;
    var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
    if(ext == "gif" || ext == "GIF" || ext == "JPEG" || ext == "jpeg" || ext == "jpg" || ext == "JPG" || ext == "doc")
    {
        return true;
    }
    else
    {
        document.getElementById("err_profile_img").innerHTML="Upload a valid image";
        return false;
    }
}
</script>

<input type="file" name="profile_img" id="profile_img" onchange="Checkfiles()"/>