Hi all,
I have this print version below. Can anyone explain me how to add a page break for every 5 records? Thanks and regards.

<?php require_once('db.php'); ?>
<?php
mysql_select_db($database_db, $db);
$query_result = "SELECT * FROM books";
$result = mysql_query($query_result, $db) or die(mysql_error());
$row_result = mysql_fetch_assoc($result);
$totalRows_result = mysql_num_rows($result);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<table border="0">
  <tr>
    <td>ID</td>
    <td>author_first</td>
    <td>author_last</td>
    <td>title</td>
    <td>publisher</td>
    <td>year</td>
    <td>ISBN</td>
    <td>subject</td>
  </tr>
  <?php do { ?>
  <tr>
    <td><?php echo $row_result['ID']; ?></td>
    <td><?php echo $row_result['author_first']; ?></td>
    <td><?php echo $row_result['author_last']; ?></td>
    <td><?php echo $row_result['title']; ?></td>
    <td><?php echo $row_result['publisher']; ?></td>
    <td><?php echo $row_result['year']; ?></td>
    <td><?php echo $row_result['ISBN']; ?></td>
    <td><?php echo $row_result['subject']; ?></td>
  </tr>
  <?php } while ($row_result = mysql_fetch_assoc($result)); ?>
</table>
</body>
</html>
<?php
mysql_free_result($result);
?>

    First off, I have absolutely no idea what browsers actually support this CSS attribute, and implements it properly. I believe FF 3.5 does, but if this is supposed to be used in any other browser, you should definitely test it.

    Assuming you want your first table row to be a table header and not a table row, you will need to adjust the markup accordingly. HTML markup is about semantics, not presentation.

    <style type="text/css" media="print">
    .breakAfter {
    	page-break-after: always
    }
    </style>
    </head>
    <body>
    <table>
    	<thead>
    		<tr>
    			<th>ID</th><th>author_first</th>
    		</tr>
    	</thead>
    	<tbody>
    		<tr>
    			<td>1</td><td>John</td>
    		</tr>
    		<tr>
    			<td>20</td><td>Jane</td>
    		</tr>
    		<tr class="breakAfter">
    			<td>8</td><td>Anthony</td>
    		</tr>
    		<tr>
    			<td>14</td><td>Lucy</td>
    		</tr>
    	</tbody>
    </table>
    

      johanafm > Thanks. This part I will implement, but first I have to get php to loop through the recordset, and for each 5 records add some element like a <div> or <hr> or <tr> to style with the break-after-rule. Its with the php-loop part I need a helping hand 🙂

        First off, I'd remove the initial fetch_assoc() (in the first few lines of code) and use a while loop rather than a do while. Otherwise you can end up with an empty result set that generate one error per td.

        
        $i = 0;
        echo '<tbody><tr>';
        while($row = mysql_fetch_assoc()) {
        	if ($i && $i % 5 == 0)
        		echo '</tr><tr class="breakAfter">';
        	else if ($i)
        		echo '</tr><tr>';
        	++$i;
        	// echo the tds and their data.
        }
        echo '</tr></tbody></table>';
        

          johanafm > Thanks again.
          This is what I end up with - the php is working smoothly and the page break is showing nicely in Firefox :rolleyes:

          <?php require_once('db.php'); ?>
          <?php 
          mysql_select_db($database_db, $db); 
          $query_result = "SELECT * FROM books"; 
          $result = mysql_query($query_result, $db) or die(mysql_error()); 
          $row_result = mysql_fetch_assoc($result); 
          $totalRows_result = mysql_num_rows($result); 
          ?> 
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
          <html> 
          <head> 
          <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
          <title>PrintTest</title>
          <style type="text/css" media="print">
          .breakAfter{
          page-break-after: always;
          }
          </style>
          </head> 
          
          <body> 
          <table border="0"> 
            <thead>
            <tr> 
              <td>ID</td> 
              <td>author_first</td> 
              <td>author_last</td> 
              <td>title</td> 
              <td>publisher</td> 
              <td>year</td> 
              <td>ISBN</td> 
              <td>subject</td> 
            </tr>
            </thead> 
          <?php 
            	$i = 0; 
          	echo '<tbody><tr>'; 
          	while($row_result = mysql_fetch_assoc($result)) { 
              if ($i && $i % 5 == 0) 
                  echo '</tr><tr class="breakAfter">'; 
              else if ($i) 
                  echo '</tr><tr>'; 
              ++$i;
              // echo the tds and their data. 
              echo "<td>"; echo $row_result['materialID']; echo "</td>";
              echo "<td>"; echo $row_result['author_first']; echo "</td>";
              echo "<td>"; echo $row_result['author_last']; echo "</td>";
              echo "<td>"; echo $row_result['title']; echo "</td>";
              echo "<td>"; echo $row_result['publisher']; echo "</td>";
              echo "<td>"; echo $row_result['year']; echo "</td>";
              echo "<td>"; echo $row_result['ISBN']; echo "</td>";
              echo "<td>"; echo $row_result['subject']; echo "</td>";
          } 
          echo '</tr></tbody>'; 
          ?>
          </table>
          </body>
          </html>
          <?php
          mysql_free_result($result);
          ?>
          

            One little issue: The first page to print has 6 records 😕 - all pages after this has 5 records - the last page has between 1 and 5 records. Can anybody tell me how to fix the first page?

            And FYI: According to w3school "the page-break-after property is supported in all major browsers". I have tested on a MAC. In Camino (1.5) its working, in Firefox (3.5) its working, but in Safari (4.0) its not working.

              Either change the css attribute to page-break-before, or change the if check to $i&#37;5-1 == 0.

                One little issue: The first page to print has 6 records - all pages after this has 5 records - the last page has between 1 and 5 records. Can anybody tell me how to fix the first page?

                Change the counter $i to start at '1'

                <?php
                      $i = 1; // initial count value = 1
                    echo '<tbody><tr>';
                    while($row_result = mysql_fetch_assoc($result)) { 

                  If you go with halojoy's solution, you'd also need to change

                  if ($i && 

                  to

                  if ($i > 1 &&
                  

                    johanafm > Thank you for your help all the way - it is very appreciated. I set the page-break-before and everything is working now - thanks.
                    halojoy > Thanks a lot for the input.

                      Just found out that first record in the recordset is being skipped 😕 - can anybody tell me how to fix that?

                        johanafm;10941058 wrote:

                        First off, I'd remove the initial fetch_assoc() (in the first few lines of code) and use a while loop rather than a do while. Otherwise you can end up with an empty result set that generate one error per td.

                        I'm guessing you still have mysql_fetch_assoc sometime before the while loop.

                          johanafm > Thanks once again. You are right of course. After removing this part:

                          $row_result = mysql_fetch_assoc($result);

                          ...from the first few lines of code, everything is playing nicely. Thanks a ton 🙂

                            Write a Reply...