Jump to content

[SOLVED] Simple function not working... printing


fredb

Recommended Posts

Just want to start by saying I am completely new to php and a client of mine asked me to make a "Printer Friendly" version of their online schedule for an upcoming conference.

 

I searched and found simple enough looking php code and decided to try it out.

 

Here is the html for the schedule:

http://www.kc-a.com/clients/wsslc/schedule.html

 

At the bottom you see "Print This Page" which is linked to:

http://www.kc-a.com/clients/wsslc/php/printer.php

 

Here is the contents of printer.php(I put spaces in the < BR > tags to make them display here):

<?php

$fd= fread(fopen("http://www.kc-a.com/clients/wsslc/schedule.html", "r"), 30000000);

if ($fd)

{

$start = strpos($fd, "<!-- CONTENT -->");

$finish = strpos($fd, "<!-- CONTENTEND -->");

$length = $finish-$start;

$code = Substr($fd, $start, $length);

}

echo '<html>

<head>

<title>Western States Surplus Line Conference 2007</title>

</head>

<body>

<center>This page is in testing.< br >< br >

Start Value: '.$start.'< br >

Finish Value: '.$finish.'< br >

Length Value: '.$length.'< br >

Code Value: '.$code.'< br ></center>

</body>

</html>';

?>

 

You can see the result on the client page. I only put the $start, $finish and $length in the echo so I could see if they hold any value. The only thing I want echoed is $code which should include everything between the <!-- CONTENT --> tag and <!-- CONTENTED --> tag. Which if you view the source of schedule.html, you see it is enclosed around the schedules <table>.

 

So this is basically all I want printer.php to look like:

<?php

$fd= fread(fopen("http://www.kc-a.com/clients/wsslc/schedule.html", "r"), 30000000);

if ($fd)

{

$start = strpos($fd, "<!-- CONTENT -->");

$finish = strpos($fd, "<!-- CONTENTEND -->");

$length = $finish-$start;

$code = Substr($fd, $start, $length);

}

echo '<html>

<head>

<title>Western States Surplus Line Conference 2007</title>

</head>

<body>

'.$code.'

</body>

</html>';

?>

 

Why is it not finding the $finish value? Can someone show me how to fix this? Thank you for your help.

-Fred

It's not reading the entire file, not sure why. But you can verify by echoing $fd to the page. So, the <!-- CONTENTEND --> tag does not exist in the $fd variable. I was able to get it to work by doing the following:

 

<?php

$filename = "http://www.kc-a.com/clients/wsslc/schedule.html";
$handle = fopen($filename, "r");
while ( $contents = fread($handle, 1000) ) {
  $fd.=$contents;
}
fclose($handle);

if ($fd) {
  $start = strpos($fd, "<!-- CONTENT -->");
  $finish = strpos($fd, "<!-- CONTENTEND -->");
  $length = $finish-$start;
  $code = Substr($fd, $start, $length);
}

echo '<html>
<head>
<title>Western States Surplus Line Conference 2007</title>
</head>
<body>
'.$code.'
</body>
</html>';
?>

 

There are some problems with this method, most notably styles. Any styles that are in the head of the document or in an external file will not be carried over tot he "printable" version.

 

However, there is a much easier way to accomplish this without having to use PHP. Use style sheet properties. You can specify different display properties for something based upon the media that it is presented in using the @media function. For example you can specify that something is displayed one way on screen and another way when printed. So in this case give your body element the display property of "none" when printed and then put the content within a div with the print display property of "inline" or "block". Then the user can simply print the original page and only get the content you want them to print.

mjdamato,

 

Thank you. This worked great. Appreciate the great input at the bottom too.

 

The reason why I needed to do this is because the client was complaining that in an older browser (he really should upgrade and come out of the stone age) that it wasn't all printing on one page. Our CSS file already has the print options in it and we were printing it on one page. This at least keeps the client happy and it doesn't take too much of my time away from other work that needs to be done.

 

Thank you again for your help.

 

Fred

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.