Skip to main content

Export CSV in cakephp

1. Download XLS sheet in CakePHP
In the controller function

function downloadXLS(){
        $this->set('contacttracker', $this->paginate());
$this->layout = false;
header ("Expires: Mon, 28 Oct 2008 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header ("Content-type: application/vnd.ms-excel");
header ("Content-Disposition: attachment; filename=contacts_".date('d-m-Y').".xls");
header ("Content-Description: Generated Report" );
}

In the view section download_xls.ctp

<STYLE type="text/css">
.table{font-family:arial; font-size:12px;}
.tdheading{font-weight:bold;color:#fff;background-color:#2E2E2E;border-width: 0.5pt;border: solid;border-color:#fff; }
.tdcontent{border-width:0.5pt; border: solid;}
</STYLE>
<table class="table">
<tr>
<td colspan="17" style="font-size:20px;font-weight:bold;">Contacts Summary</td>
</tr>
<tr>
<td><b>Number of contacts:</b></td>
<td style="text-align:left" colspan="16"><?php echo count($contacttracker);?></td>
</tr>
<tr>
<td><b>Date:</b></td>
<td style="text-align:left" colspan="16"><?php echo date('d/m/Y'); ?></td>
</tr>
<tr>
<td colspan="10"></td>
</tr>
<tr VALIGN="TOP">
<td class="tdheading">Company Name</td>
<td class="tdheading">Title</td>
<td class="tdheading">First Name</td>
<td class="tdheading">Last Name</td>
<td class="tdheading">Email</td>
<td class="tdheading">Function</td>
<td class="tdheading">Designation</td>
<td class="tdheading">Country</td>
<td class="tdheading">State</td>
<td class="tdheading">City</td>
<td class="tdheading">Contact Type</td>
<td class="tdheading">Category</td>
<td class="tdheading">Level</td>
<td class="tdheading">Mobile</td>
<td class="tdheading">Desk Number</td>
<td class="tdheading">Main Number</td>

<td class="tdheading">Stage</td>

<td class="tdheading">Date of Birth</td>
<td class="tdheading">Alternate Email</td>
<td class="tdheading">SBU</td>
<td class="tdheading">Address</td>
<td class="tdheading">Postal Code</td>
<td class="tdheading">LinkedIn</td>
<td class="tdheading">Facebook</td>
<td class="tdheading">Twitter</td>
<td class="tdheading">IM id</td>
</tr>
<?php foreach($contacttracker as $clientprofile) {
$stage = $clientprofile['Contacttracker']['stage'];
if($clientprofile['Contacttracker']['active']==0) $stage = "Inactive";
if($clientprofile['Contacttracker']['stage'] == "") $stage = "No Contract";
if($clientprofile['Contacttracker']['stage'] == "Reqs Received") $stage = "Opening";

//$stage = ($clientprofile['Contacttracker']['active']==0)?'Inactive':(($clientprofile['Contacttracker']['stage']=='')?'Contract':$clientprofile['Contacttracker']['stage']);

?>
<tr>
<td class="tdcontent"><?php echo $clientprofile['Company']['name']; ?></td>
<td class="tdcontent"><?php echo $clientprofile['Clientcontact']['address_title']; ?></td>
<td class="tdcontent"><?php echo $clientprofile['Clientcontact']['firstname']; ?></td>
<td class="tdcontent"><?php echo $clientprofile['Clientcontact']['lastname']; ?></td>
<td class="tdcontent"><?php echo $clientprofile['Clientcontact']['email']; ?></td>
<td class="tdcontent"><?php echo $clientprofile['Clientcontact']['Discipline']['name']; ?></td>
<td class="tdcontent"><?php echo $clientprofile['Clientcontact']['title'];?></td>

<td class="tdcontent"><?php echo $clientprofile['Clientcontact']['Country']['name']; ?></td>
<td class="tdcontent"><?php echo $clientprofile['Clientcontact']['State']['name']; ?></td>
<td class="tdcontent"><?php echo $clientprofile['Clientcontact']['City']['name']; ?></td>
<td class="tdcontent"><?php echo $clientprofile['Clientcontact']['Companytype']['name']; ?></td>
<td class="tdcontent"><?php echo $clientprofile['Clientcontact']['headquarter'];?></td>
<td class="tdcontent"><?php echo $clientprofile['Clientcontact']['Level']['name']; ?></td>
<td class="tdcontent"><?php echo $clientprofile['Clientcontact']['mobcountry'].' '.$clientprofile['Clientcontact']['mobile']; ?></td>
<td class="tdcontent"><?php echo $clientprofile['Clientcontact']['phonecountry'].' '.$clientprofile['Clientcontact']['phstd'].' '.$clientprofile['Clientcontact']['phone']; ?></td>
<td class="tdcontent"><?php echo $clientprofile['Clientcontact']['bcountry'].' '.$clientprofile['Clientcontact']['boardstd'].' '.$clientprofile['Clientcontact']['board'].' '.$clientprofile['Clientcontact']['extn']; ?></td>
<td class="tdcontent"><?php echo $stage; ?></td>

<td class="tdcontent">
<?php
if(!empty($clientprofile['Clientcontact']['dob']) && $clientprofile['Clientcontact']['dob'] != "0000-00-00") {
$clientprofile['Clientcontact']['dob'] = str_replace("0000", date("Y"), $clientprofile['Clientcontact']['dob']);
$dob = date('d - M', strtotime($clientprofile['Clientcontact']['dob']));
echo $dob;
}

?>
</td>
<td class="tdcontent"><?php echo $clientprofile['Clientcontact']['email1']; ?></td>
<td class="tdcontent"><?php echo $clientprofile['Clientcontact']['sbu'] ; ?></td>

<td class="tdcontent"><?php echo $clientprofile['Clientcontact']['address']; ?></td>
<td class="tdcontent"><?php echo $clientprofile['Clientcontact']['pincode'] ; ?></td>
<td class="tdcontent"><?php echo $clientprofile['Clientcontact']['linkedin'];?></td>
<td class="tdcontent"><?php echo $clientprofile['Clientcontact']['facebook'];?></td>
<td class="tdcontent"><?php echo $clientprofile['Clientcontact']['twitter'];?></td>
<td class="tdcontent"><?php echo $clientprofile['Clientcontact']['im_id'];?></td>
</tr>
<?php } ?>
</table>

2. Download CSV sheet with PHP

           $con = mysqli_connect("localhost","root","","sudhir_demo") or die(mysqli_error());   
            ini_set('memory_limit','300M'); // mem
            ini_set('max_execution_time', 3000); // time
            header('Content-Type: text/csv; charset=utf-8');
            header('Content-Disposition: attachment; filename=data.csv');
            $output = fopen('php://output', 'w');
            fputcsv($output, array('ID', 'Company Name', 'Created Date'));
            $rows = mysqli_query($con,'SELECT id,company_name, company_createddate FROM company');
            while ($row = mysqli_fetch_array($rows,MYSQLI_ASSOC)) fputcsv($output, $row);
 

Comments

Popular posts from this blog

6 guaranteed steps how to create CRON JOB FUNTION in wordpress

Create Cron Job function in Wordpress plugin Step 1: Register function on plugin activate  register_activation_hook(__FILE__, 'activate_one'); Step 2: add_filter function for interval //Filter for Adding multiple intervals add_filter( 'cron_schedules', 'intervals_schedule' ); // define interval function   function intervals_schedule($schedules) {   $schedules['everyminute'] = array(    'interval' => 60, // Every 1 minutes    'display'  => __( 'Every 1 minutes' )   );   return $schedules;  } Step 3: Activate hook function   //Schedule a first action if it's not already scheduled   function activate_one() {   if (!wp_next_scheduled('wpyog_cron_action')) {    wp_schedule_event( time(), 'everyminute', 'wpyog_cron_action');   }  } Step 4: Cron hook function   //Hook into that action that'll fire after 1 minutes   add_action('wpyog_cron_action', 'execute_

How to Create a jQuery Autocomplete in Wordpress

How to Create a jquery-ui Autocomplete in wordpress. Autocomplete provides suggestions while you type into the text field. In Wordpress we fetch dynamically matched pattern. Include javascript and css files in header. Create action inside functions.php or inside plugin code. add_action('wp_head', 'custom_register_scripts'); function custom_register_scripts(){ wp_register_style( 'techsudhir_jquery_ui_css', plugin_dir_url(__FILE__) . 'css/jquery-ui.css', false,'1.0.0' ); wp_enqueue_style( 'techsudhir_jquery_ui_css' ); wp_register_script('techsudhir_jquery_ui_js',plugin_dir_url(__FILE__) . 'js/jquery-ui.js',array('jquery'),'1.1', false); wp_enqueue_script('techsudhir_jquery_ui_js'); wp_localize_script( 'techsudhir_autocomplete', 'jqueryAutocomplete', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); wp_enqueue_script( 'techsudhir_

Facebook Login

Step 1: Go on Apps & create new Apps Step 2: Enter the app name & choose category Step 3: Enter the site domain Script code <div id="fb-root"></div> <script   src="http://connect.facebook.net/en_US/all.js#appId=<?php echo '603609636351597';?>"> </script> <script>    FB.init({              appId:'<?php echo '603609636351597';?>', cookie:true,              status:true, xfbml:true           });   FB.getLoginStatus(function(response) {   }); Login function call on click function faceLogin(){  FB.login(function(response) {    if (response.authResponse) {  FB.api('/me', function(response) { $('input[id=facebook_login_fb]').val(response['id']); $('#fb_login_form').submit();  });    } else {  console.log('User cancelled login or did