box/spout - freeze 1st row (pane) of spreadsheet - php

Is it possible to freeze 1st row (freeze pane) of the spreadsheet using box/spout?
With PHPexcel I do like that:
$objPHPExcel=new PHPExcel();
$ActiveSheet=$objPHPExcel->getActiveSheet();
$ActiveSheet->freezePane('A2');
Cannot use PHPexcel, as I am working with big files.

Found a hack to add this feature.
Inside of Spout\Writer\XLSX\Manager\WorksheetManager.php : function startSheet
After this line
fwrite($sheetFilePointer, self::SHEET_XML_FILE_HEADER);
Add this line
fwrite($sheetFilePointer, '<sheetViews><sheetView showRowColHeaders="1" showGridLines="true" workbookViewId="0" tabSelected="1">'
.'<pane state="frozen" activePane="bottomLeft" topLeftCell="A2" ySplit="1"/>'
.'<selection sqref="A1" activeCell="A1" pane="bottomLeft"/></sheetView></sheetViews>');

<?php
/**
*
* Inspired by: https://github.com/box/spout/issues/368
*
* Simple helper class for Spout - to return rows indexed by the header in the sheet
*
* Author: Jaspal Singh - https://github.com/jaspal747
* Feel free to make any edits as needed. Cheers!
*
*/
class SpoutHelper {
private $rawHeadersArray = []; //Local array to hold the Raw Headers for performance
private $formattedHeadersArray = []; //Local array to hold the Formatted Headers for performance
private $headerRowNumber; //Row number where the header col is located in the file
/**
* Initialize on a per sheet basis
* Allow users to mention which row number contains the headers
*/
public function __construct($sheet, $headerRowNumber = 1) {
$this->flushHeaders();
$this->headerRowNumber = $headerRowNumber;
$this->getFormattedHeaders($sheet);//Since this also calls the getRawHeaders, we will have both the arrays set at once
}
/**
*
* Set the rawHeadersArray by getting the raw headers from the headerRowNumber or the 1st row
* Once done, set them to a local variable for being reused later
*
*/
public function getRawHeaders($sheet) {
if (empty($this->rawHeadersArray)) {
/**
* first get column headers
*/
foreach ($sheet->getRowIterator() as $key => $row) {
if ($key == $this->headerRowNumber) {
/**
* iterate once to get the column headers
*/
$this->rawHeadersArray = $row->toArray();
break;
}
}
} else {
/**
* From local cache
*/
}
return $this->rawHeadersArray;
}
/**
*
* Set the formattedHeadersArray by getting the raw headers and the parsing them
* Once done, set them to a local variable for being reused later
*
*/
public function getFormattedHeaders($sheet) {
if (empty($this->formattedHeadersArray)) {
$this->formattedHeadersArray = $this->getRawHeaders($sheet);
/**
* Now format them
*/
foreach ($this->formattedHeadersArray as $key => $value) {
if (is_a($value, 'DateTime')) { //Somehow instanceOf does not work well with DateTime, hence using is_a -- ?
$this->formattedHeadersArray[$key] = $value->format('Y-m-d');//Since the dates in headers are avilable as DateTime Objects
} else {
$this->formattedHeadersArray[$key] = strtolower(str_replace(' ' , '_', trim($value)));
}
/**
* Add more rules here as needed
*/
}
} else {
/**
* Return from local cache
*/
}
return $this->formattedHeadersArray;
}
/**
* Return row with Raw Headers
*/
public function rowWithRawHeaders($rowArray) {
return $this->returnRowWithHeaderAsKeys($this->rawHeadersArray, $rowArray);
}
/**
* Return row with Formatted Headers
*/
public function rowWithFormattedHeaders($rowArray) {
return $this->returnRowWithHeaderAsKeys($this->formattedHeadersArray, $rowArray);
}
/**
* Set the headers to keys and row as values
*/
private function returnRowWithHeaderAsKeys($headers, $rowArray) {
$headerColCount = count($headers);
$rowColCount = count($rowArray);
$colCountDiff = $headerColCount - $rowColCount;
if ($colCountDiff > 0) {
//Pad the rowArray with empty values
$rowArray = array_pad($rowArray, $headerColCount, '');
}
return array_combine($headers, $rowArray);
}
/**
* Flush local caches before each sheet
*/
public function flushHeaders() {
$this->formattedHeadersArray = [];
$this->rawHeadersArray = [];
}
}
And then in your main file you can do:
$reader = ReaderEntityFactory::createReaderFromFile($filePath);
$reader->open($filePath);
/**
* Now get the data
*/
foreach ($reader->getSheetIterator() as $sheet) {
$spoutHelper = new SpoutHelper($sheet, 1); //Initialize SpoutHelper with the current Sheet and the row number which contains the header
foreach ($sheet->getRowIterator() as $key => $row) {
if ($key == 1) {
//echo "Skipping Headers row";
continue;
}
//Get the indexed array with col name as key and col val as value`
$rowWithHeaderKeys = $spoutHelper->rowWithFormattedHeaders($row->toArray());
}
}
Source: https://gist.github.com/jaspal747/2bd515f9e318b0331f3ca3d2297742c5

It's not possible to freeze panes with Spout yet. But you can always fork the repo and implement this feature :)

Related

Simple PHP template class

I wanted separate my HTML from PHP so after searching I found a great little php class that just do the trick. Only issue that I try merging 2 templates together but it doesn’t work.
Here is the original class that i found from below website
http://www.broculos.net/2008/03/how-to-make-simple-html-template-engine.html#.WCsa8CTy2ng
class Template {
/**
* The filename of the template to load.
*
* #access protected
* #var string
*/
protected $file;
/**
* An array of values for replacing each tag on the template (the key for each value is its corresponding tag).
*
* #access protected
* #var array
*/
protected $values = array();
/**
* Creates a new Template object and sets its associated file.
*
* #param string $file the filename of the template to load
*/
public function __construct($file) {
$this->file = $file;
}
/**
* Sets a value for replacing a specific tag.
*
* #param string $key the name of the tag to replace
* #param string $value the value to replace
*/
public function set($key, $value) {
$this->values[$key] = $value;
}
/**
* Outputs the content of the template, replacing the keys for its respective values.
*
* #return string
*/
public function output() {
/**
* Tries to verify if the file exists.
* If it doesn't return with an error message.
* Anything else loads the file contents and loops through the array replacing every key for its value.
*/
if (!file_exists($this->file)) {
return "Error loading template file ($this->file).<br />";
}
$output = file_get_contents($this->file);
foreach ($this->values as $key => $value) {
$tagToReplace = "[#$key]";
$output = str_replace($tagToReplace, $value, $output);
}
return $output;
}
/**
* Merges the content from an array of templates and separates it with $separator.
*
* #param array $templates an array of Template objects to merge
* #param string $separator the string that is used between each Template object
* #return string
*/
static public function merge($templates, $separator = "\n") {
/**
* Loops through the array concatenating the outputs from each template, separating with $separator.
* If a type different from Template is found we provide an error message.
*/
$output = "";
foreach ($templates as $template) {
$content = (get_class($template) !== "Template")
? "Error, incorrect type - expected Template."
: $template->output();
$output .= $content . $separator;
}
return $output;
}
}
Code that work
$post = new Template("post.tpl");
$post->set("post_title", $post_title);
$post->set("post_description", $post_description);
$post->set("content", $post->output());
echo $post->output();
Even when I want to loop if I add the code it works fine. But then I try to merge two template files together
all_posts.tpl
<div class=”posts”>
<h1>[#page_title]</</h1>
[#display_posts]
</div>
display_posts.tpl
<div class=”post”>
<h2>[#display_title]</h2>
<p>[#display_description]</p>
</div>
So what I want to do now is to push display_posts.tpl to all_posts.tpl and replace the tag [#display_posts]
So in my php I did below
$post = new Template("all_posts.tpl ");
$post->set("page_title", "All Posts");
echo $post->output();
//created a array from a mysqli loop
$post_set = array();
while ($post_row = mysqli_fetch_array($posts)){
$post_title = $post_row['title'];
$post_description = $post_row['description'];
$post_set[] = array(
"display_title" => $post_title,
"display_description" => $post_description
);
}
foreach ($post_set as $posts) {
$row = new Template("list_users_row.tpl");
foreach ($posts as $key => $value) {
$row->set($key, $value);
}
$postsTemplates[] = $posts;
}
// here i try to merge template
$postsContents = Template::merge($postsTemplates);
$layout->set("content", $postsContents->output());
echo $layout->output();
But this is throwing off a error set() is not an function. Could someone help me out to figure out this? I’m still in the process of learning php classes.
The reason why $layout->set() does not work is because $layout is not a Template Object. In your code you use $post = new Template("all_posts.tpl ");. This makes $post a Template object that can use the set() function in your Template class.
So you should do: $layout = new Template(....) and than you can call $layout->set().
In the tutorial they do the same:
include("template.class.php");
$profile = new Template("user_profile.tpl");
$profile->set("username", "monk3y");
$profile->set("photoURL", "photo.jpg");
$profile->set("name", "Monkey man");
$profile->set("age", "23");
$profile->set("location", "Portugal");
$layout = new Template("layout.tpl");
$layout->set("title", "User profile");
$layout->set("content", $profile->output());
echo $layout->output();
Hope this helps.

How to set or upload only CSV type in Codeigniter

I am trying to import a CSV into MYSQL database. I followed a tutorial here
Now i am getting an error that
The filetype you are attempting to upload is not allowed.
Below is my code for Controller :
function importcsv() {
$data['addressbook'] = $this->csv_model->get_addressbook();
$data['error'] = ''; //initialize image upload error array to empty
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'text/x-comma-separated-values'|'text/comma-separated-values'|'application/octet-stream'| 'application/vnd.ms-excel'|'application/x-csv'|'text/x-csv'|'text/csv'|'application/csv'|'application/excel'|'application/vnd.msexcel'|'text/plain';
$config['max_size'] = '1000';
$this->load->library('upload', $config);
// If upload failed, display error
if (!$this->upload->do_upload()) {
$data['error'] = $this->upload->display_errors();
$this->load->view('csvindex', $data);
} else {
$file_data = $this->upload->data();
$file_path = './uploads/'.$file_data['file_name'];
if ($this->csvimport->get_array($file_path)) {
$csv_array = $this->csvimport->get_array($file_path);
foreach ($csv_array as $row) {
$insert_data = array(
'firstname'=>$row['firstname'],
'lastname'=>$row['lastname'],
'phone'=>$row['phone'],
'email'=>$row['email'],
);
$this->csv_model->insert_csv($insert_data);
}
$this->session->set_flashdata('success', 'Csv Data Imported Succesfully');
redirect(base_url().'csv');
//echo "<pre>"; print_r($insert_data);
} else
$data['error'] = "Error occured";
$this->load->view('csvindex', $data);
}
}
public function chk_attachment() // callback validation for check the attachment extension
{
$file_type = array('.csv');
if(!empty($_FILES['uploaded_file']['name']))
{
$ext = strtolower(strrchr($_FILES['uploaded_file']['name'],"."));
if(in_array($ext,$ext_array))
{
return true;
}
else
{
$this->form_validation->set_message('chk_attachment','Attachment allowed only csv');
return false;
}
}
{
$this->form_validation->set_message('chk_attachment','image field is required');
return false;
}
}
The call back function was suggested by a user on stackoverflow who had same issue solved, but somehow didn't work for me. I have a users table and i just want to insert users using the uploaded CSV but somehow its not allowing me to upload a CSV.
This is the Library
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter CSV Import Class
*
* This library will help import a CSV file into
* an associative array.
*
* This library treats the first row of a CSV file
* as a column header row.
*
*
* #package CodeIgniter
* #subpackage Libraries
* #category Libraries
* #author Brad Stinson
*/
class Csvimport {
private $filepath = "";
private $handle = "";
private $column_headers = "";
/**
* Function that parses a CSV file and returns results
* as an array.
*
* #access public
* #param filepath string Location of the CSV file
* #param column_headers array Alternate values that will be used for array keys instead of first line of CSV
* #param detect_line_endings boolean When true sets the php INI settings to allow script to detect line endings. Needed for CSV files created on Macs.
* #return array
*/
public function get_array($filepath='', $column_headers='', $detect_line_endings=FALSE)
{
// If true, auto detect row endings
if($detect_line_endings){
ini_set("auto_detect_line_endings", TRUE);
}
// If file exists, set filepath
if(file_exists($filepath))
{
$this->_set_filepath($filepath);
}
else
{
return FALSE;
}
// If column headers provided, set them
$this->_set_column_headers($column_headers);
// Open the CSV for reading
$this->_get_handle();
$row = 0;
while (($data = fgetcsv($this->handle, 0, ",")) !== FALSE)
{
// If first row, parse for column_headers
if($row == 0)
{
// If column_headers already provided, use them
if($this->column_headers)
{
foreach ($this->column_headers as $key => $value)
{
$column_headers[$key] = trim($value);
}
}
else // Parse first row for column_headers to use
{
foreach ($data as $key => $value)
{
$column_headers[$key] = trim($value);
}
}
}
else
{
$new_row = $row - 1; // needed so that the returned array starts at 0 instead of 1
foreach($column_headers as $key => $value) // assumes there are as many columns as their are title columns
{
$result[$new_row][$value] = trim($data[$key]);
}
}
$row++;
}
$this->_close_csv();
return $result;
}
/**
* Sets the filepath of a given CSV file
*
* #access private
* #param filepath string Location of the CSV file
* #return void
*/
private function _set_filepath($filepath)
{
$this->filepath = $filepath;
}
/**
* Sets the alternate column headers that will be used when creating the array
*
* #access private
* #param column_headers array Alternate column_headers that will be used instead of first line of CSV
* #return void
*/
private function _set_column_headers($column_headers='')
{
if(is_array($column_headers) && !empty($column_headers))
{
$this->column_headers = $column_headers;
}
}
/**
* Opens the CSV file for parsing
*
* #access private
* #return void
*/
private function _get_handle()
{
$this->handle = fopen($this->filepath, "r");
}
/**
* Closes the CSV file when complete
*
* #access private
* #return array
*/
private function _close_csv()
{
fclose($this->handle);
}
}
Add $config['allowed_types'] = 'csv'; to your $config array.
for more help check https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html
Also go through the file on path application\config\mimes.php for more clear understanding about csv array support on file upload type
You can update this array if you really want...
'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel')

SimpleBrowser returns empty html

I am using SimpleBrowser that is a part of SimpleTest PHP framework.
The idea is to imitate user interactions with the website and record returned HTML code into a file for further comparison. But something goes wrong here as empty HTML is sometimes returned.
getTransportError() returns Nothing fetched
It happens in completely random places and I can't use back() function because most pages are submitted forms.
require_once('simpletest/browser.php');
class TesterBrowser extends SimpleBrowser
{
/**
* Test the page against the reference. If reference is missing, is it created
* Uses md5 checksum to check if files are identical
*
* #param string $forcename Optional. Substitude autogenerated filename.
* #param boolean $forceRef Optional. Force file to be saved as the reference
*
* #access public
*
* #return void
*/
public function testPage($forcename = "")
{
//who called me?
//$callers=debug_backtrace();
//$whocalledme = $callers[1]['function'];
//get the current source
$html = $this->getContent();
//generate filename
$filename = empty($forcename) ? preg_replace('/[^\w\-'. ''. ']+/u', '-', $this->getUrl()) : $forcename;
$filename .= ".html";
//is there a gauge?
if(file_exists("ref/".$filename) && filesize(dirname(__FILE__)."/ref/".$filename) > 0)
{
//is there a difference
file_put_contents(dirname(__FILE__)."/actual/".$filename, $html);
if(filesize(dirname(__FILE__)."/actual/".$filename) == 0)
{
return false;
}
if(md5_file(dirname(__FILE__)."/actual/".$filename) != md5_file(dirname(__FILE__)."/ref/".$filename))
{
echo $this->getUrl() . " (" . $filename . ") has changed \r\n";
}
}
else
{
file_put_contents(dirname(__FILE__)."/ref/".$filename, $html);
if(filesize(dirname(__FILE__)."/ref/".$filename) == 0)
{
return false;
}
}
return true;
}
/**
* Output the string to the terminal
*
* #param mixed $string String to output
*
* #access public
*
* #return void
*/
public function output($string)
{
echo date("d-m-Y H:i:s") . " - $string... \r\n";
//update date so that it will be the same on every page
exec('date -s "24 JUN 2013 10:00:00"');
}
/**
* Restore the server date using external NTP server
*
* #access public
*
* #return void
*/
public function restoreDate(){
$this->output("Restoring the date&time from NTP server");
exec("ntpdate 0.uk.pool.ntp.org");
exec("hwclock -systohc");
}
}
And the way tests are performed:
class Tester
{
public $browser = null;
const BASEURL = "http://ticketing/";
function __construct(){
$this->browser = new TesterBrowser();
$this->browser->setConnectionTimeout(180);
//get the list of class method to be run
$methods = array();
foreach(get_class_methods($this) as $var)
{
if(0 === strpos($var, 'test')) //they all start with test
{
$methods[] = $var;
}
}
$methods[] = "cleanUp";
//now we need to run these methods
foreach($methods as $m){
while($this->$m() == false){
$this->browser->output("Empty page, trying again");
sleep(5);
}
}
}
//index page
function testGetIndexPage()
{
$this->browser->output("Getting index page");
$this->browser->get(self::BASEURL);
return $this->browser->testPage();
}
//try to enter wrong password
function testWrongPassword()
{
$this->browser->output("Entering wrong credentials");
$this->browser->setField("username", "wrong");
$this->browser->setField("password", "wrong");
$this->browser->clickSubmitByName("submit");
return $this->browser->testPage("wrong-credentials");
}
//Delete ticket though admin
function testDeleteTicketThroughAdmin()
{
$this->browser->output("Deleting the ticket through admin page");
$this->browser->setField("bulk[]", "375341");
$this->browser->setField("bulkaction", "delete");
$this->browser->clickSubmit("Do Action");
return $this->browser->testPage("deleted-ticket-admin");
}
//Restore the date
function cleanUp()
{
$this->browser->restoreDate();
return true;
}
}
$tester = new Tester();
There are of course much more test performed and this is a stripped version.
I have googled a lot about this problem, there seems to be no adequate documentation whatsoever.
Solved. It was a timeout issue although for some reason no adequate error message is implemented.
$this->browser->setConnectionTimeout(180);

How to remember 10 last read articles with timestamp in session for a user in Codeigniter?

I would like to make a PHP if condition code that will check if the last 10 articles or 10 minutes from the article reading by the user have already elapsed.
E.g.
A user open a page with id = 235 (this id value is in the url localhost/article/235 )
and this id value will be saved in session with a current timestamp and maybe his IP address
Then he read another article and the same will happen.
I need to remember the clicked stuff for another ten clicks and then reset that only for the first row. E.g. after the 10th click the id and timestamp will not became 11th row but will replace the 1st row in the list.
The php condition in CodeIgniter will then check these values and will update the article hit counter value in the articles table and column counter like this:
$this->db->where('id', $id);
$this->db->set('counter', 'counter+1', FALSE);
$this->db->update('articles');
But before calling this code I need to make this check from the session?
How to do that?
I think storing e.g. 10 entries in the session with timestamps per user will be enough.
Just don't save the same page in the session twice.
And the condition will check the current timestamp with the saved one and if it is more than e.g. 10 minutes or the user have read/clicked another 10 articles it will allow the update counter php code.
I don't need to have this bulletproof. Just to disable the increment using browser's refresh button.
So, if he wants to increment the counter he will need to wait ten minutes or read another 10 articles ;)
You should definitely go for Sessions. It saves you bandwidth consumption and is much easier to handle. Unless, of course, you need the data on the client-side, which, by your explanation, I assume you don't. Assuming you went for sessions, all you gotta do is store an array with the data you have. The following code should do it:
$aClicks = $this->session
->userdata('article_clicks');
// Initialize the array, if it's not already initialized
if ($aClicks == false) {
$aClicks = array();
}
// Now, we clean our array for the articles that have been clicked longer than
// 10 minutes ago.
$aClicks = array_filter(
$aClicks,
function($click) {
return (time() - $click['time']) < 600; // Less than 10 minutes elapsed
}
);
// We check if the article clicked is already in the list
$found = false;
foreach ($aClicks as $click) {
if ($click['article'] === $id) { // Assuming $id holds the article id
$found = true;
break;
}
}
// If it's not, we add it
if (!$found) {
$aClicks[] = array(
'article' => $id, // Assuming $id holds the article id
'time' => time()
);
}
// Store the clicks back to the session
$this->session
->set_userdata('article_clicks', $aClicks);
// If we meet all conditions
if (count($aClicks) < 10) {
// Do something
}
I assumne that $clicks is an array with up to ten visited articles. The id is used as key and the timestamp as value. $id is the id of the new article.
$clicks = $this->session->userdata('article_clicks');
//default value
$clicks = ($clicks)? $clicks : array();
//could be loaded from config
$maxItemCount = 10;
$timwToLive= 600;
//helpers
$time = time();
$deadline = $time - $timeToLive;
//add if not in list
if(! isset($clicks[$id]) ){
$clicks[$id] = $time;
}
//remove old values
$clicks = array_filter($clicks, function($value){ $value >= $deadline;});
//sort newest to oldest
arsort($clicks);
//limit items, oldest will be removed first because we sorted the array
$clicks = array_slice($clicks, 0, $maxItemCount);
//save to session
$this->session->>set_userdata('article_clicks',$clicks)
Usage:
//print how mch time has passed since the last visit
if(isset($clicks[$id]){
echo "visited ".($time-$clicks[$id]). "seconds ago." ;
} else {
echo "first visit";
}
EDIT: you have to use arsort not rsort or the keys will be lost, sorry
Based on Raphael_ code and your question you can try this:
<?php
$aClicks = $this->session
->userdata('article_clicks');
$nextId = $this->session->userdata('nextId');
// Initialize the array, if it's not already initialized
if ($aClicks == false) {
$aClicks = array();
$nextId = 0;
}
// Now, we clean our array for the articles that have been clicked longer than
// 10 minutes ago.
$aClicks = array_filter($aClicks, function($click) {
return (time() - $click['time']) < 600; // Less than 10 minutes elapsed
}
);
// We check if the article clicked is already in the list
$found = false;
foreach ($aClicks as $click) {
if ($click['article'] === $id) { // Assuming $id holds the article id
$found = true;
break;
}
}
// If it's not, we add it
if (!$found) {
$aClicks[$nextId] = array(
'article' => $id, // Assuming $id holds the article id
'time' => time()
);
$nextId++;
$this->session->set_userdata('nextId', $nextId);
}
$this->session->set_userdata('article_clicks', $aClicks);
if (count($aClicks) > 10 && $nextId > 9) {
$this->session->set_userdata('nextId', 0);
echo "OK!";
}
?>
I hope I understood correctly what you need.
Usage:
$this->load->library('click');
$this->click->add($id, time());
The class API is very simple and the code is commented. You can also check if an item expired(), if exists() and you can get() item saved time.
Remember that:
Each item will expire after 10 minutes (see $ttl)
Only 10 items are saved in session (see $max_entries)
class Click
{
/**
* CI instance
* #var object
*/
private $CI;
/**
* Click data holder
* #var array
*/
protected $clicks = array();
/**
* Time until an entry will expire
* #var int
*/
protected $ttl = 600;
/**
* How much entries do we store ?
* #var int
*/
protected $max_entries = 10;
// -------------------------------------------------------------------------
public function __construct()
{
$this->CI =& get_instance();
if (!class_exists('CI_Session')) {
$this->CI->load->library('session');
}
// load existing data from user's session
$this->fetch();
}
// -------------------------------------------------------------------------
/**
* Add a new page
*
* #access public
* #param int $id Page ID
* #param int $time Added time (optional)
* #return bool
*/
public function add($id, $time = null)
{
// If page ID does not exist and limit has been reached, stop here
if (!$this->exist($id) AND (count($this->clicks) == $this->max_entries)) {
return false;
}
$time = !is_null($time) ? $time : time();
if ($this->expired($id)) {
$this->clicks[$id] = $time;
return true;
}
return false;
}
/**
* Get specified page ID data
*
* #access public
* #param int $id Page ID
* #return int|bool Added time or `false` on error
*/
public function get($id)
{
return ($this->exist($id)) ? $this->clicks[$id] : false;
}
/**
* Check if specified page ID exists
*
* #access public
* #param int $id Page ID
* #return bool
*/
public function exist($id)
{
return isset($this->clicks[$id]);
}
/**
* Check if specified page ID expired
*
* #access public
* #param int $id Page ID
* #return bool
*/
public function expired($id)
{
// id does not exist, return `true` so it can added
if (!$this->exist($id)) {
return true;
}
return ((time() - $this->clicks[$id]) >= $this->ttl) ? true : false;
}
/**
* Store current clicks data in session
*
* #access public
* #return object Click
*/
public function save()
{
$this->CI->session->set_userdata('article_clicks', serialize($this->clicks));
return $this;
}
/**
* Load data from user's session
*
* #access public
* #return object Click
*/
public function fetch()
{
if ($data = $this->CI->session->userdata('article_clicks')) {
$this->clicks = unserialize($data);
}
return $this;
}
public function __destruct()
{
$this->save();
}
}
You could easily wrap that into a class of it's own that serializes the information into a string and that is able to manipulate the data, e.g. to add another value while taking care to cap at the maximum of ten elements.
A potential usage could look like, let's assume the cookie last would contain 256 at start:
echo $_COOKIE['last'] = (new StringQueue($_COOKIE['last']))->add(10), "\n";
echo $_COOKIE['last'] = (new StringQueue($_COOKIE['last']))->add(20), "\n";
echo $_COOKIE['last'] = (new StringQueue($_COOKIE['last']))->add(30), "\n";
echo $_COOKIE['last'] = (new StringQueue($_COOKIE['last']))->add(40), "\n";
echo $_COOKIE['last'] = (new StringQueue($_COOKIE['last']))->add(50), "\n";
echo $_COOKIE['last'] = (new StringQueue($_COOKIE['last']))->add(60), "\n";
echo $_COOKIE['last'] = (new StringQueue($_COOKIE['last']))->add(70), "\n";
echo $_COOKIE['last'] = (new StringQueue($_COOKIE['last']))->add(80), "\n";
echo $_COOKIE['last'] = (new StringQueue($_COOKIE['last']))->add(90), "\n";
echo $_COOKIE['last'] = (new StringQueue($_COOKIE['last']))->add(100), "\n";
And the output (Demo):
10,256
20,10,256
30,20,10,256
40,30,20,10,256
50,40,30,20,10,256
60,50,40,30,20,10,256
70,60,50,40,30,20,10,256
80,70,60,50,40,30,20,10,256
90,80,70,60,50,40,30,20,10,256
100,90,80,70,60,50,40,30,20,10
A rough implementation of that:
class StringQueue implements Countable
{
private $size = 10;
private $separator = ',';
private $values;
public function __construct($string) {
$this->values = $this->parseString($string);
}
private function parseString($string) {
$values = explode($this->separator, $string, $this->size + 1);
if (isset($values[$this->size])) {
unset($values[$this->size]);
}
return $values;
}
public function add($value) {
$this->values = $this->parseString($value . $this->separator . $this);
return $this;
}
public function __toString() {
return implode(',', $this->values);
}
public function count() {
return count($this->values);
}
}
It's just some basic string operations, here with implode and explode.

cakephp tcpd error: Some data has already been output to browser, can't send PDF file on remote server

I'm getting the following warning when try to view list_of_holidays.pdf from the remote server:
Warning (2): Cannot modify header information - headers already sent by (output started
at /home/aquinto1/app/views/helpers/flash.php:155) [APP/vendors/tcpdf/tcpdf.php, line 8541]
TCPDF ERROR: Some data has already been output to browser, can't send PDF file
line 155 is the last line in flash.php ie the closing tag for php (?>). Before that it is the code to embedSWF. I don't see anything wrong with that.
However, it is displaying fine on the local server.
I've checked for whitespaces and yet the error is still there.
i'm already using ob_clean before the output.
can someone tell me on what i'm doing wrong. FYI i'm using cakephp with tcpdf.
The following is flash.php
class FlashHelper extends AppHelper {
var $helpers = array('Javascript');
/**
* Used for remembering options from init() to each renderSwf
*
* #var array
*/
var $options = array(
'width' => 100,
'height' => 100
);
/**
* Used by renderSwf to set a flash version requirement
*
* #var string
*/
var $defaultVersionRequirement = '9.0.0';
/**
* Used by renderSwf to only call init if it hasnt been done, either
* manually or automatically by a former renderSwf()
*
* #var boolean
*/
var $initialized = false;
/**
* Optional initializing for setting default parameters and also includes the
* swf library. Should be called once, but if using several groups of flashes,
* MAY be called several times, once before each group.
*
* #example echo $flash->init();
* #example $flash->init(array('width'=>200,'height'=>100);
* #return mixed String if it was not able to add the script to the view, true if it was
*/
function init($options = array()) {
if (!empty($options)) {
$this->options = am($this->options, $options);
}
$this->initialized = true;
$view =& ClassRegistry::getObject('view');
if (is_object($view)) {
$view->addScript($this->Javascript->link('swfobject'));
return true;
} else {
return $this->Javascript->link('swfobject');
}
}
/**
* Wrapper for the SwfObject::embedSWF method in the vendor. This method will write a javascript code
* block that calls that javascript method. If given a dom id as fourth parameter the flash will
* replace that dom object. If false is given, a div will be placed at the point in the
* page that this method is echo'ed. The last parameter is mainly used for sending in extra settings to
* the embedding code, like parameters and attributes. It may also send in flashvars to the flash.
*
* For doucumentation on what options can be sent, look here:
* http://code.google.com/p/swfobject/wiki/documentation
*
* #example echo $flash->renderSwf('counter.swf'); // size set with init();
* #example echo $flash->renderSwf('flash/ad.swf',100,20);
* #example echo $flash->renderSwf('swf/banner.swf',800,200,'banner_ad',array('params'=>array('wmode'=>'opaque')));
* #param string $swfFile Filename (with paths relative to webroot)
* #param int $width if null, will use width set by FlashHelper::init()
* #param int $height if null, will use height set by FlashHelper::init()
* #param mixed $divDomId false or string : dom id
* #param array $options array('flashvars'=>array(),'params'=>array('wmode'=>'opaque'),'attributes'=>array());
* See SwfObject documentation for valid options
* #return string
*/
function renderSwf($swfFile, $width = null, $height = null, $divDomId = false, $options = array()) {
$options = am ($this->options, $options);
if (is_null($width)) {
$width = $options['width'];
}
if (is_null($height)) {
$height = $options['height'];
}
$ret = '';
if (!$this->initialized) {
$init = $this->init($options);
if (is_string($init)) {
$ret = $init;
}
$this->initialized = TRUE;
}
$flashvars = '{}';
$params = '{wmode : "opaque"}';
$attributes = '{}';
if (isset($options['flashvars'])) {
$flashvars = $this->Javascript->object($options['flashvars']);
}
if (isset($options['params'])) {
$params = $this->Javascript->object($options['params']);
}
if (isset($options['attributes'])) {
$attributes = $this->Javascript->object($options['attributes']);
}
if ($divDomId === false) {
$divDomId = uniqid('c_');
$ret .= '<div id="'.$divDomId.'"></div>';
}
if (isset($options['version'])) {
$version = $options['version'];
} else {
$version = $this->defaultVersionRequirement;
}
if (isset($options['install'])) {
$install = $options['install'];
} else {
$install = '';
}
$swfLocation = $this->webroot.$swfFile;
$ret .= $this->Javascript->codeBlock(
'swfobject.embedSWF
("'.$swfLocation.'", "'.$divDomId.'", "'.$width.'", "'.$height.'", "'.$version.'",
"'.$install.'", '.$flashvars.', '.$params.', '.$attributes.');');
return $ret;
}
}
?>
Simply do what the error tells you: Check app/views/helpers/flash.php line 155 and see what it is outputting there and fix it. There must be some code that outputs something.
Could it be one of the return statements?
if (is_object($view)) {
$view->addScript($this->Javascript->link('swfobject'));
return true;
} else {
return $this->Javascript->link('swfobject');
}
$ret .= $this->Javascript->codeBlock(
'swfobject.embedSWF
("'.$swfLocation.'", "'.$divDomId.'", "'.$width.'", "'.$height.'", "'.$version.'",
"'.$install.'", '.$flashvars.', '.$params.', '.$attributes.');');
return $ret;
}
What other code is on the page calling flash?

Categories