PHP Excel Reader - Writing Tables - php

Hey guys i am new to PHP and is trying to self learn this language. Right now i am trying to implement something using pure PHP to display Excel documents onto browsers. I am using PHP Excel Reader to read the documents but somehow they are not showing.
Although i can do my own debugging and research if there were error messages which tells me which line was causing the error, but there were none and the problem i am facing now is that
The tables are not showing up
No error messages of any kind from apache
What i am trying to achieve:
Display Excel spreadsheets in table format on browsers using PHP Excel Reader
Check how many sheets are in the .xls document, get the sheet name and display the amount of buttons accordingly
The buttons must be able to allow the user to traverse between different sheets of one document
Right now i am able to get the codes to count the number of sheets and display its name and buttons correctly, but still not being able to display the corresponding tables.
I have tried many ways to tackle this issue based on the documentation but to no avail.
Here are my current codes:
example.php
<?php
error_reporting(E_ALL ^ E_NOTICE);
require_once 'reader.php';
$data = new Spreadsheet_Excel_Reader("test.xls");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="scripts/excel.js"></script>
<style>
table.excel {
border-style:ridge;
border-width:1;
border-collapse:collapse;
font-family:sans-serif;
font-size:12px;
}
table.excel thead th, table.excel tbody th {
background:#CCCCCC;
border-style:ridge;
border-width:1;
text-align: center;
vertical-align:bottom;
}
table.excel tbody th {
text-align:center;
width:20px;
}
table.excel tbody td {
vertical-align:bottom;
}
.clearfloat {
clear: both;
}
table.excel tbody td {
padding: 0 3px;
border: 1px solid #EEEEEE;
}
table.excel tname {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
font-weight: bold;
color: #000000;
padding-right: 10px;
padding-left: 10px;
}
.sheetnames {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
font-weight: bold;
text-transform: uppercase;
color: #000000;
padding-right: 20px;
float: left;
padding-bottom: 20px;
}
</style>
</head>
<body onLoad="changeSheet(0)">
<form action="<?php echo $editFormAction; ?>" method="post" name="worksheet">
<?php
$i = -1;
for($s=0; $s<count($data->sheets)-1; $s++) {
$i++;
?>
<div class="sheetnames">
<input name="sheet" type="button" value="<?php echo $data->boundsheets[$i]['name'];?>" onClick="changeSheet(<?php echo $i ?>)">
</div>
<?php
}
?>
<div class="clearfloat"></div>
<div id="test"></div>
</form>
</body>
</html>
excel.js
// JavaScript Document
var xmlHttp
function changeSheet(n)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="script pages/changeSheet.php"
url=url+"?q="+n
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("test").innerHTML=xmlHttp.responseText
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
changeSheet.php
<?php
error_reporting(E_ALL ^ E_NOTICE);
require_once 'reader.php';
$data = new Spreadsheet_Excel_Reader("test.xls");
$q = $_GET['q'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
echo $data->dump(true,false, $sheet=$q);
?>
</body>
</html>
And attached along with the codes is the library file for PHP Excel Reader which i do not believe is causing the problem.
Once again, i thank you guys for taking time to read my question. So what went wrong? What was causing the tables to not show up?

Looks like your version of Excel_Reader does not support "::dump()".
Looks like you have to iterate through the sheets, rows and columns yourself.
Try var_dump($data) to see whats in there.

Related

Render a partial view with layout in Zend Framework 1.12

Similar questions have been asked before but unfortunately I am still not able to find a solution of my problem.
I want to render a view with its layout and store its response into a variable.
I have an action called pdf as shown below.
public function pdfAction(){
$ids = $this->getParam('id');
$entity = $this->getParam('entity');
$referer = $this->getRequest()->getServer('HTTP_REFERER');
if(empty($ids) || empty($entity)){
$this->_helper->redirector->gotoUrlAndExit($referer);
}
$grid = $this->_exportModel->getExportGrid($ids, $entity);
if(empty($grid->data[0])){
$this->_helper->messenger->error('No user was found for the given id. Please pass the correct parameters and try again.');
$this->_helper->redirector->gotoUrlAndExit($referer);
}
/* I would like to render the view here and get its response below. */
$html = '';
$pdf = new Om_PDF();
$pdf->writeHtmlToPDF($html);
}
This is the pdf view for the above action.
<?php echo $this->render('templates/grid/print-grid.phtml'); ?>
And here's the pdf layout
<?php echo $this->doctype(); ?>
<html moznomarginboxes mozdisallowselectionprint>
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" media="all"/>
<style type="text/css">
.container {
margin: 0 auto;
max-width: 1000px;
text-align: center;
margin-top: 30px;
}
#media print {
#page {
size: auto;
margin: 15mm 10mm 15mm 10mm;
}
body {
margin: 0px;
}
.page-break {
display: block;
page-break-before: always;
}
.print-view-table {
float: left;
width: 45% !important;
margin-right: 5%;
}
.print-view-table tr th, .print-view-table tr td {
width: 30%;
}
.print-view-table tr th {
background-color: #e0e0e0 !important;
border: 1px solid #ddd;
-webkit-print-color-adjust: exact;
}
}
</style>
<head>
<?php ?>
</head>
<body>
<div class="container">
<div class="row">
<?php echo $this->layout()->content; ?>
</div>
</div>
</body>
</html>
So far I have tried using render() and partial() methods but none of them worked.
Is there a way using which I can get the out put of the pdf view along with its layout inside the pdfAction() method?
You need to use :
$this->_helper->layout->setLayout('/path/to/your/pdf_layout_script');
in your pdfAction() and after that you can use render() or partial() methods, the corrects layout should be out put

How to convert html page to pdf and breaking the content with header in each pdf page?

I am using css to break the content as required to be printed in fresh pdf page, I want to include the header/footer on each pdf page, I tried with fixed css/ along with margin but that works for first page not the successive page. The footer is getting overlapped with page content..
mypage.php
<?php //call_db
//call_func
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Print Page</title>
<style>
body {
background-color: white;
color: black;
font-size: small;
font-family: sans-serif, helvetica, tahoma, arial;
margin: 20%;
}
.page-break {
display:block;
page-break-before:always;
}
#media screen {
div.divFooter {
display: none;
}
div.divHeader {
display: none;
}
}
#media print {
div.divFooter {
position: fixed;
bottom: 0;
}
div.divHeader {
position: fixed;
top: 0;
}
}
</style>
<script type="text/javascript" src="jquery.js" > </script>
<script type="text/javascript">
$(document).ready(function(){
window.print();
});
</script>
</head>
<body>
//call sql query for content // A collection of 200 lists
<div class="divHeader"> <h1 align="center" style="font-size:36px;"> THE GAME OF THORNES</h1> </div>
<h1>Sean's CSS Print vs. Screen Example</h1>
<h2>Lesson 1</h2>
<p>This is my first Page in the document.</p>
<div class="page-break"></div>
<h2>Lesson 2</h2>
<p>Examples and theory of lesson2</p>
<div class="page-break"></div>
<h2>Lesson 3</h2>
<p>Examples and theory of lesson2</p>
<div class="divFooter">UNCLASSIFIED </div>
</body>
</html>
** I am adding PHP tag as if you have any other better way to print the content from db, please suggest.
I think this is duplicate of this Convert HTML + CSS to PDF with PHP? ,
you must use your pdf convertor library to add header and footer to pdf.
I'm used http://www.tcpdf.org/ in my project. you can use it too.

Uncaught SyntaxError: Unexpected token } doctype

I am writing again on a little import script which I have to split because of the data amount.
So I have a start.php with the fancy ajax action and the dbactions.php doing all the hard work.
The script first initializes the data, checks for doubles and creates a session with ids.
Then it should go through the ids in batches.
On first call the start.php has no errors, and the first part of the script runs well, in creates the user session with an array over 12k ids.
But when I output the result of the second initialize, it gives the the error message of
Uncaught SyntaxError: Unexpected token }
See screenshot attached:
The dbactions.php itself gives no error despite the missing $do variable, and the start.php at first load also not.
The start.php looks like this:
<?php
header('Content-Type: text/html; charset=utf-8');
session_start();
$_SESSION['start_counter'] = 0;
$_SESSION['batches'] = 1;
$_SESSION['array_counter'] = 0;
$_SESSION['batchcount'] = 0;
$_SESSION['newsletter'] = 0;
?>
<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>DB-actions</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="main.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
function DoSomething(what){
var request = $.ajax({
url: "dbactions.php?do=" + what,
type: "GET",
dataType: "html"
});
$('#godosomething').attr('disabled','true');
$('#doing').show();
$('#callmsg').empty();
$('body').addClass('grey');
request.done(function(msg) {
$('#doing').hide();
$("body").removeClass("grey");
$("#callmsg").html(msg);
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}
</script>
<style type="text/css">
padding, margin: 0;
body { padding:20px 0;}
p { padding-bottom:3px;}
#callmsg { margin:50px 0; }
#insertmsg { margin:50px 0; }
#doing { display:none; position:absolute; bottom:48%; right:48%; border: 1px solid green; padding: 20px; background-color:green; color: white;}
.grey { background-color: grey; opacity: 0.5;}
.green { color:green; }
</style>
</head>
<body>
<h1>Start the import</h1>
<button type="button" onclick="DoSomething('initialize')" id="godosomething">Go baby</button>
<div id="callmsg"></div>
<div id="doing">I am working, please wait...</div>
</body>
</html>
I know there are several threads about it, the suggestions where, that the charset is wrong (not as excepted from ajax), but I have this on each file:
header('Content-Type: text/html; charset=utf-8');
And there was a suggestion about the ' and the " marks, I tried to change them in the output, but it did not help.
I thought also of the long output of the print_r, but disabling it didn't help.
This is the second output:
echo '<h2>Batch successfull</h2>';
$_SESSION['start_counter'] = $_SESSION['start_counter'] + $numberofqueries;
$_SESSION['batches']++;
echo "<p>Next? <button type='button' onclick" . "='DoSomething('makearray')" ."id='godosomething'>Go baby</button></p>";
echo '<p>Batch'.$_SESSION['batches'].' from '.$_SESSION['batchcount'].'</p>';
echo '<pre>';
print_r($_SESSION['users']);
echo '</pre>';
ADDITIONAL EDIT
I uncommented the whole second output until the h2 echo, and it still give me the same error.
How can this be? So the error comes not from the second output?
you are not qouting this correctly.
'DoSomething('makearray')'
should be:
"DoSomething('makearray')"
And in php:
echo "<p>Start first? <button type='button' onclick" . "='DoSomething('makearray')" ."id='godosomething'>Go baby</button></p>";
Your CSS is invalid. Try this.
<style type="text/css">
padding { margin: 0;}
body { padding:20px 0;}
p { padding-bottom:3px;}
#callmsg { margin:50px 0; }
#insertmsg { margin:50px 0; }
#doing { display:none; position:absolute; bottom:48%; right:48%; border: 1px solid green; padding: 20px; background-color:green; color: white;}
.grey { background-color: grey; opacity: 0.5;}
.green { color:green; }
</style>
You have an error on your second button.
Change this
<button type="button" onclick="DoSomething(" makearray")"="" id="godosomething">Go baby</button>
To this
<button type="button" onclick="DoSomething('makearray')" id="godosomething">Go baby</button>
Change This -
echo "<p>Next? <button type='button' onclick" . "='DoSomething('makearray')" ."id='godosomething'>Go baby</button></p>";
To-
echo "<p>Next? <button type='button' onclick=DoSomething('makearray') id='godosomething'>Go baby</button></p>";
And let me know if it clicks or not.
EDIT--
Remove any dummy spaces/content from your file.
Change <!DOCTYPE> to <!DOCTYPE html>

CSS issues with dynamic php page

I am no beginner with HTML and CSS... but I am with PHP. I have a very tiny dynamically generated PHP page that creates a table and fills it with numbers. The CSS is working on random items, very weirdly and I'm not sure what is going on. The <body> tag CSS is not working at all. Not one property is being affected by it. The table.temps css is not working either, but if I remove it, then the <th> tag looses its styling. This is for a homework assignment and I know it's an easy fix, I've just been having the worst time with CSS and PHP lately.
It's all one page, and its relatively small, so here is the document:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Lab 1</title>
<style type="text/css">
<!-- Declare document <body> styles -->
body{
background-color: #9FDBFF;
color: #333;
font-family: Helvetica, sans-serif;
font-size: .8em;
height: 100%;
width: 100%;
}
<!-- Declare document table styles -->
table.temps {
width: 50%;
height: auto;
min-height: 400px;
border: 2px solid black;
color: #333;
}
th {
background: blue;
color: #FFF;
height: 20px;
width: 100px;
}
.colorIt {
background-color: #CCC;
}
</style>
</head>
<body>
<?php
$intTableTotalWidth = 8;
$intTableTotalHeight = 8;
$intCount = 1;
print("<table class='temps'>");
print("<th>Farenheight</th>");
print("<th>Celcius</th>");
for ($intHeight = 0; $intHeight < $intTableTotalHeight; $intHeight++) {
print("<tr>");
for ($intWidth = 0; $intWidth < $intTableTotalWidth; $intWidth++) {
if ($intCount % 2 == 0){
print("<td class='colorIt'>" . $intCount ."</td>");
}
else {
print ("<td>" . $intCount . "</td>");
}
$intCount++;
}
print("</tr>");
}
print("</table>");
?>
</body>
</html>
EDIT: My fault people, I didn't even realize I was using HTML comments in the CSS. It's internal CSS so the CSS comments don't change color in the HTML document which was throwing me off. I fixed the issue. I appreciate your input!
You are using html comments in the style tag. This is not allowed. You should never use html comments in style and script tags.It's not allowed. Remove them and it will all work.
remove these lines
<!-- Declare document <body> styles -->
<!-- Declare document table styles -->
Your <th> tags should be embeded in <tr> tags: <tr><th>col1</th><th>col2</th></tr>.
You are outputing more cells / row than columns defined with <th>. If you want a <th> to contain two columns, you should use <th colspan="2">col1</th>.
much easier sollution: have session_start() at begining of .php file, put all your theme in a array and later, include stylesheet
session_start()
$theme=array("links"=>"#ff0000", "bkg"=>"#00ff00");
$_SESSION["Theme"]=$theme;
...
<link rel="stylesheet" type="text/css" href="default.php">
and your stylesheet look like this:
default.php
<?php session_start();
if (isset($_SESSION["Theme"])) {
extract($_SESSION["Theme"]);
header("Content-type: text/css");
} else {
// this part is for proper coloring the file, editor will see <style>
// but when loaded from main file <style> will be discarded
?>
<style>
<?php } ?>
a {
color:<?=$links?>;
}
body {
background-color: <?=$bkg?>;
}

Mixing PHP and CSS Across Pages

I have a few pages which I'd like to have the same background across, so I figured an external style sheet would be the way to go. However, the background changes depending on the time of day, so I had to mix some PHP into the CSS. So now I have a file, background.php:
<html>
<style type="text/css">
body
{
background-image: url('<?php echo (day() == 1 ? 'images/day_sheep.jpg'
: 'images/night_sheep.jpg'); ?>');
background-position: 50% 50%;
background-repeat: no-repeat;
background-color: silver
}
a {text-decoration:none;}
a:link {color:#ff0000;}
a:visited {color:#0000FF;}
a:hover {text-decoration:underline;}
</style>
</html>
Which is being called from two different pages. One page works perfectly fine, but the other page completely broke when I added the line require_once 'background.php', by which I mean nothing displays at all anymore. The offending page is as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Penelope's Conquests</title>
<?php require_once 'background.php'; ?>
<style type="text/css">
table {
margin: 10px;
margin-left: 50%;
margin-right: 50%;
padding: 12px;
border: 10px;
text-align: center;
background-color: #fffb40;
border-style: ridge;
border-collapse: separate;
border-color: #9c6ad6;
outline-style: inset;
}
td.cap {
text-transform: capitalize;
font-variant: small-caps;
}
td.str {
font-weight: bolder;
font-size: 1.4em;
}
</style>
</head>
<body>
<h2>To date, Queen Penelope has destroyed:<br /><br /></h2>
<?php
require_once 'victims.php';
require_once 'mysqlSheep.php';
echo '<table border="3"
frame="box"
cellpadding="5">
<caption>Penelope\'s Victims.</caption>
<tr><th>Victim</th><th>Times Zapped</th>';
$hits = mysql_query("
SELECT *
FROM victims
ORDER BY amount
");
if( $hits )
{
while( $row = mysql_fetch_array($hits) )
{
echo '<tr><td class="cap">'.$row['victim'].'</td>
<td>'.$row['amount'].'</td></tr>';
}
}
else
{
echo '<p>' . mysql_error() . '</p>';
}
echo '</tr></table><br /><br />';
echo '<table border="3"
frame="box"
cellpadding="5">
<caption>Button Clicks.</caption>
<tr><th>Hour of day</th><th>Times Clicked</th>';
$time = mysql_query("
SELECT *
FROM time
ORDER BY hits
");
while( $row = mysql_fetch_array($time) )
{
print "<tr><td class='str'>".$row['hour']."</td>";
print "<td>".$row['hits']."</td></tr>";
}
?>
</body>
</html>
Why doesn't the page want to behave with the style sheet?
Another option is to attach your css file with the <link> attribute.
So in your background.php place
<?php
header("Content-type: text/css");
?>
body
{
background-image: url('<?php echo (day() == 1 ? 'images/day_sheep.jpg'
: 'images/night_sheep.jpg'); ?>');
background-position: 50% 50%;
background-repeat: no-repeat;
background-color: silver
}
a {text-decoration:none;}
a:link {color:#ff0000;}
a:visited {color:#0000FF;}
a:hover {text-decoration:underline;}
And then call it using
<link rel="stylesheet" type="text/css" href="background.php" />
Placing this in 1 answer would get messy which is why I've added another one.
You've got an <html> tag inside your PHP stylesheet, which means you will get duplicate <html> tags.. not nice
Remove that and just use the <style> tags..
background.php
<style type="text/css">
body
{
background-image: url('<?php echo (day() == 1 ? 'images/day_sheep.jpg'
: 'images/night_sheep.jpg'); ?>');
background-position: 50% 50%;
background-repeat: no-repeat;
background-color: silver
}
a {text-decoration:none;}
a:link {color:#ff0000;}
a:visited {color:#0000FF;}
a:hover {text-decoration:underline;}
</style>
Ohhh, the issue is that the function day() is defined in a separate file, I forgot to move it over when I was reorganizing the site.
Come on Marko, it was super fantastic script!!! I used your way in order to set custom width inside an external css file and it works great.
I do all php stuff if needed any in here:
<?php
header("Content-type: text/css");
$width= $_COOKIE['scr_width']-10;
?>
then I use the variables below inside the css file like this:
width:<?PHP echo $width."px;";?>
and I have done the include like this:
<head>
<link rel="stylesheet" href="<?PHP echo myLocVars::$mySpacePath."style.php";?>">
</head>
Including CSS can be very tricky because unfortunately it doesn't throw an error (at least I don't know how to..), and it can be a trouble. That's why I give the full path because it is a local CSS included by a local file included itself from index.php.
If you are testing your site on localhost, then try
require_once(dirname(background.php) . "/background.php");
instead of require_once(background.php);

Categories