page breaks

Discussion in 'PHP' started by ataloss, Apr 3, 2014.

  1. #1
    need suggestions pertaining to the above title. I want to print my php report w/heading at the top
    of each page with page numbering. Anyone?
     
    Solved! View solution.
    ataloss, Apr 3, 2014 IP
  2. AbstractChaos

    AbstractChaos Member

    Messages:
    58
    Likes Received:
    4
    Best Answers:
    4
    Trophy Points:
    25
    #2
    take a look at these
    http://davidwalsh.name/css-page-breaks
    http://stackoverflow.com/questions/20050939/print-page-numbers-on-pages-when-printing-html

    basically create a css style sheet for your html page thats only used for printing (media="print")
     
    AbstractChaos, Apr 3, 2014 IP
  3. ataloss

    ataloss Active Member

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    63
    #3
    I get the following error:

    Parse error: syntax error, unexpected 'endforeach' (T_ENDFOREACH) in



    <html>
    <head></head><body>
    <?php
    //Print version
    //Set at what record to break at 25 records per page
    $BreakAT=25;
    $num=0;
    $pagenum = 1;
    $totdue=0;
    ?>
    <div class="display print">
    <?php
    foreach($rows as $row):
    if($num % $BreakAT == 0)
    {
    if($num>0){ echo '</table class="print">';
    echo '<div class="breakhere"></div>';
    $pagenum++; }
    echo date('m/d/y');
    echo " Page " . $pagenum;
    ?>
    <div class="title">Accounts Payable Report</div><br />
    <table align="center" cellspacing=0 cellpadding=0 border=1>
    <thead>
    <tr>
    <thead> <th colspan=4></th>
    <th bgcolor="#ccffff">date</th>
    <th bgcolor="#ccffff">days</th>
    <th bgcolor="#ccffff">amt</th>
    <tr>
    <th bgcolor="#ccffff">recur?</th>
    <th bgcolor="#ccffff">acct#</th>
    <th bgcolor="#ccffff">creditor</th>
    <th bgcolor="#ccffff">purpose</th>
    <th bgcolor="#ccffff">due</th>
    <th bgcolor="#ccffff">late</th>
    <th bgcolor="#ccffff">due</th>
    </tr>
    </thead>
    <?php
    $totdue += $row['amtdue'];
    echo '
    <tr>
    <td>' . $row['status'] . '</td>
    <td>' . $row['acctno'] . '</td>
    <td>' . $row['bname'] . '</td>
    <td>' . $row['purpose'] . '</td>
    <td>' . $row['duedate'] . '</td>
    <td align=right class="currency">' . ($late > 120 ? 'pastdue' : $row['dayslate']) . '</td>
    <td align=right class="currency">' . number_format($row['amtdue'], 2, '.', '') . '</td>
    </tr>';
    $num++;
    endforeach;
    echo '
    <tr>
    <th scope="row" colspan="6">Grand Total:</th>
    <td bgcolor="#FFD4D4" class="currency">' . number_format($totdue, 2, '.', '') . '</td>
    </tr>
    </table>';
    }
    ?>
    </div></body></html>
     
    ataloss, May 3, 2014 IP
  4. #4
    1) use the CODE tags on the forum, it might help us make sense of your code logic.

    2) CSS exists for a reason, use it. This is 2014 not 1997, even in testing you shouldn't be trying to use BGCOLOR or ALIGN. Likewise targeting via CSS means you should have one markup for ALL your media targets, so that 'print' class and DIV for nothing around the table doesn't make much sense.

    3) Semantics exist for a reason, that DIV at the top should be either a H1 or H2 before the table -- or even more semantically a CAPTION inside the table.

    4) the lack of sensible formatting (which I was able to get from the forum's quote feature), string additions for no reason, and opening/closing of PHP for no good reason is most likely a hefty part of what's biting you on that code... Especially even using that garbage "endforeach" nonsense.

    5) you can't add classes on the CLOSING tag of an element. </table class="print"> is gibberish. Classes go when you OPEN it, not when you close it.

    6) You are opening THEAD more than once...

    7) you aren't closing your TR

    8) your THEAD content seems to have nothing to do with the values being displayed...

    9) You're declaring THEAD, where's your matching TBODY?

    10) Most of what you are trying to do is THEAD and TFOOT's job, with CSS 2.1's assistance.

    11) Even in testing, get a PROPER head and doctype on it, as browsers (even so called compliant ones) can behave differently without them! It shouldn't be a headache to copy/paste a standard document start on there.

    12) Beware that even though we theoretically have print.css -- both Firefuxxors and Interdebt Exploder apply their own styling over it and seem to for some jacktarded reason think your screen.css should still be applied. You can optionally turn that behavior off as a user, but not as the developer. :(

    So first, let's clean up the markup:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html
    	xmlns="http://www.w3.org/1999/xhtml"
    	lang="en"
    	xml:lang="en"
    ><head>
    
    <meta
    	http-equiv="Content-Type"
    	content="text/html; charset=utf-8"
    />
    
    <meta
    	http-equiv="Content-Language"
    	content="en"
    />
    
    <meta
    	name="viewport"
    	content="width=device-width; height=device-height; initial-scale=1.0"
    />
    
    <link
    	type="text/css"
    	rel="stylesheet"
    	href="print.css"
    	media="print"
    />
    
    <title>
    	Accounts Payable Demo
    </title>
    
    </head><body>
    
    <table class="accountsPayable">
    	<caption>
    		Accounts Payable Report
    	</caption><thead>
    		<tr>
    			<th>recur?</th>
    			<th>acct#</th>
    			<th>creditor</th>
    			<th>purpose</th>
    			<th>due</th>
    			<th>late</th>
    			<th>due</th>
    		</tr>
    	</thead><tfoot>
    		<tr>
    			<td colspan="3">', date('m/d/y'), '</td>
    			<td colspan="4" class="page"></td>
    		</tr>
    	</tfoot><tbody>
    	
    <?php
    	
    $totalDue = 0;
    	
    foreach ($rows as $row) {
    	$totalDue += $row['amtdue']; 
    	echo '
    		<tr>
    			<td>', $row['status'], '</td>
    			<td>', $row['acctno'], '</td>
    			<td>', $row['bname'], '</td>
    			<td>', $row['purpose'], '</td>
    			<td>', $row['duedate'], '</td>  
    			<td class="currency">', ($row['dayslate'] > 120 ? 'pastdue' : $row['dayslate']), '</td>
    			<td class="currency">', number_format($row['amtdue'], 2, '.', ''), '</td>
    		</tr>';
    }
    
    echo '
    		<tr class="grandTotal">
    			<th scope="row" colspan="6">Grand Total:</th>
    			<td class="currency">', number_format($totalDue, 2, '.', ''), '</td>
    		</tr>';
    ?>
    
    	</tbody>
    </table>
    
    </body></html>
    Code (markup):
    Then the print.css should be something like:
    .accountsPayable {
    	border-collapse:collapse;
    	border:1px solid #000;
    	counter-reset:page;
    }
    
    .accountsPayable thead th {
    	background:#CFF;
    }
    
    .accountsPayable .currency {
    	text-align:right;
    }
    
    .accountsPayable .grandTotal td {
    	background:#FFD4D4;
    }
    
    .accountsPayable tfoot .page:before {
    	counter-increment:page;
    	content:"Page: " counter(page);
    }
    Code (markup):
    Should work in IE8/newer.

    THEAD and TFOOT both go before TBODY, (a LOT of people screw that up with TFOOT) and are automatically printed at the top and bottom of each page when a table paginates. CSS 2.1 counters then handle showing which page is which.
    http://www.w3.org/TR/CSS21/generate.html#counters

    That pretty much drags it kicking and screaming out of the 1990's, and simplifies the PHP many-fold.

    Pagination of lots of data automatically fitting the output area is something TABLE does REALLY well, IF you use semantic markup and the complete set of tags a TABLE is supposed to have. It's why we have THEAD, TFOOT and TBODY -- and even better, it gracefully degrades to a single page on devices that don't need to be broken into multiple pages -- like SCREEN.

    It's a shame M&M (Mozilla and Microsoft) have their heads wedged up their backsides about the default behavior of PRINT media targets.

    Good rule of thumb, the whole foreach: endforeach nonsense -- Just don't do it. Confusing pointless sloppy mess... Another of those things I think should be stricken from PHP. Along with the php <?php ?> tags...
     
    Last edited: May 3, 2014
    deathshadow, May 3, 2014 IP