fredb Posted March 15, 2007 Share Posted March 15, 2007 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 Link to comment https://forums.phpfreaks.com/topic/42923-solved-simple-function-not-working-printing/ Share on other sites More sharing options...
Psycho Posted March 15, 2007 Share Posted March 15, 2007 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. Link to comment https://forums.phpfreaks.com/topic/42923-solved-simple-function-not-working-printing/#findComment-208493 Share on other sites More sharing options...
HoTDaWg Posted March 15, 2007 Share Posted March 15, 2007 it would be easier if u made the schedule itself in php, then when it comes to printing; you can create a page called print.php which gets the variables you want to print, echos it, and then prints it. HoTDaWg Link to comment https://forums.phpfreaks.com/topic/42923-solved-simple-function-not-working-printing/#findComment-208495 Share on other sites More sharing options...
fredb Posted March 16, 2007 Author Share Posted March 16, 2007 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 Link to comment https://forums.phpfreaks.com/topic/42923-solved-simple-function-not-working-printing/#findComment-208806 Share on other sites More sharing options...
Psycho Posted March 16, 2007 Share Posted March 16, 2007 Then at the very least in the "printable" page you should consider including the stylesheet as well. Otherwise, you need to put all your style in-line which defeats the purpose of CSS. Link to comment https://forums.phpfreaks.com/topic/42923-solved-simple-function-not-working-printing/#findComment-208821 Share on other sites More sharing options...
fredb Posted March 16, 2007 Author Share Posted March 16, 2007 Yes, I attached the CSS sheet to the printable page. I didn't want to include any unnecessary tags before getting the actual php to work first. It looks a lot cleaner now if you take a look. Thanks again. Link to comment https://forums.phpfreaks.com/topic/42923-solved-simple-function-not-working-printing/#findComment-208891 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.