6 thoughts on “Php

  1. Creating a CSV file with database results

    $sql = “SELECT * from Table”;
    $values_exp = mysql_query($sql,$conn); $result_count = mysql_num_rows($values_exp);
    $csv_output = ”;
    if($result_count > 0 ){

    // content for the CSV file
    $csv_output .= “businessname, fullname, postcode, email, easting, northing, username, adminpassword, registrationtime, detailspagestats, lastmonthshits”;
    $csv_output .= “\n”;
    while ($rowr_exp = mysql_fetch_row($values_exp)) {

    $detailspagestats = 0; $details_30_pagestats = 0;
    for ($j=0;$j<10;$j++) {
    $csv_output .= str_replace(',', ' ', $rowr_exp[$j]).", ";
    }

    $csv_output .= $detailspagestats.", ";
    $csv_output .= $details_30_pagestats.", ";
    $csv_output .= "\n";

    }
    $csv_filename = $member_type."_".date("Y-m-d_H-i",time()).".csv";
    $fd = fopen ($csv_filename, "w");
    $fileContent=str_replace("\n\n","\n",$csv_output);
    if(fputs($fd, $fileContent)){
    fclose($fd);
    return $csv_filename;
    }else{
    fclose($fd);
    return false;
    }

  2. Reading data from csv file

    function executeUpload(sfWebRequest $request)
    {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
    $this->form = new productForm();
    $file = $request->getFiles($this->form->getName());
    if($file[‘error’]==0){
    $fname = $file[‘name’];
    $chk_ext = explode(“.”,$fname);
    if(strtolower($chk_ext[1]) == “csv”){
    $filename = $file[‘tmp_name’];
    $filestream = fopen($filename, “r”);
    while (($data = fgetcsv($filestream, 1000, “,”)) !== FALSE)
    {
    $productList[]=$data;
    }
    fclose($filestream);
    if(count($productList[0])!=3){
    $this->getUser()->setFlash(‘error’, ‘Upload correct CSV files for products.’);
    $this->redirect(‘fileuploads/index’);
    }
    array_shift($productList);
    foreach ($productList as $product){
    if(trim($product[0])!=”){
    if($this->checkField($product)){
    $this->form = new ProductForm();
    if(strtolower($product[2])==’yes’){
    $active = 1;
    }else{
    $active = 0;
    }
    $values = array( “id” => ”, “product_name” => trim($product[0]), “product_description” => $product[1], “is_active” => $active);
    $this->form->bind($values, array());
    if ($this->form->isValid()){
    $this->form->save();
    }
    }
    }
    }
    $this->redirect(‘product/index’);
    }else{
    $this->getUser()->setFlash(‘error’, ‘Upload only CSV files.’);
    $this->redirect(‘fileuploads/index’);
    }
    }else{
    $this->getUser()->setFlash(‘error’, ‘Upload a CSV file.’);
    $this->redirect(‘fileuploads/index’);
    }

  3. generating CSV file and downloading

    // content for the CSV file
    $csv_output=”;
    $csv_output .= ” businessname, fullname, postcode, email, easting, northing, username, adminpassword, registrationtime, detailspagestats, lastmonthshits”;
    $csv_output .= “\n”;

    // Getting records of all the producers who are either members or non members

    $sql = “SELECT * from Table “;

    $values_exp = mysql_query($sql,$conn);

    while ($rowr_exp = mysql_fetch_row($values_exp)) {
    $detailspagestats = 0; $details_30_pagestats = 0;
    for ($j=0;$j<10;$j++) {
    $csv_output .= str_replace(',', ' ', $rowr_exp[$j]).", ";
    }

    $csv_output .= $detailspagestats.", ";
    $csv_output .= $details_30_pagestats.", ";
    $csv_output .= "\n";
    }

    $filename = $_GET['type']."_".date("Y-m-d_H-i",time());
    header("Content-type: application/vnd.ms-excel");
    header("Content-disposition: csv" . date("Y-m-d") . ".csv");
    header("Content-disposition: filename=".$filename.".csv");

    print $csv_output;
    exit;

  4. function ResizeImage($uploadedfile, $ResizeSizeX, $ResizeSizeY, $ActualFileName)
    {
    try
    {
    if(strpos($ActualFileName, “.jpg”)==TRUE)
    $src = imagecreatefromjpeg($uploadedfile); // Create an image object
    else if(strpos($ActualFileName, “.gif”)==TRUE)
    $src = imagecreatefromgif($uploadedfile);
    else if(strpos($ActualFileName, “.png”)==TRUE)
    $src = imagecreatefrompng($uploadedfile);
    else
    return FALSE;
    list($Originalwidth, $Originalheight) = getimagesize($uploadedfile); // get current image size
    if($ResizeSizeY==0 && $ResizeSizeX!=0) // if only the width was specified
    $ResizeSizeY = ($Originalheight/$Originalwidth) * $ResizeSizeX;
    else if($ResizeSizeX==0 && $ResizeSizeY!=0) // if only the height was specified
    $ResizeSizeX = ($Originalwidth/$Originalheight) * $ResizeSizeY;
    $tmp = imagecreatetruecolor($ResizeSizeX, $ResizeSizeY); // create new image with calculated dimensions
    imagecopyresampled($tmp, $src, 0, 0, 0, 0, $ResizeSizeX, $ResizeSizeY, $Originalwidth, $Originalheight); // resize the image and copy it into $tmp image
    if(strpos($ActualFileName, “.jpg”)==TRUE)
    imagejpeg($tmp, $ActualFileName, 100);
    else if(strpos($ActualFileName, “.gif”)==TRUE)
    imagegif($tmp, $ActualFileName);
    else if(strpos($ActualFileName, “.png”)==TRUE)
    imagepng($tmp, $ActualFileName, 100);
    imagedestroy($src);
    imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request has completed.
    }
    catch(Exception $e)
    {
    echo $e;
    return FALSE;
    }
    return TRUE;
    }

  5. $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, ‘ISO-8859-1’, false);

    ini_set(‘max_execution_time’, 123456);

  6. SugarCRM

    Password Reset

    minimum validation
    1) At least one uppercase
    2) At least one numeric.

    To change the above validation rule, Please edit the below file

    plz edit config_override.php

    $sugar_config[‘passwordsetting’][‘oneupper’] = false;
    $sugar_config[‘passwordsetting’][‘onenumber’] = false;

Leave a Reply

Your email address will not be published. Required fields are marked *