Jump to content

[SOLVED] help with printing new table header on every page


rchandran0023

Recommended Posts

I query a database and print the info in a table.

I need to print a new table header on each new page.

Do I need to embed another loop somewhere so that like every 20 rows print a new table header, if so how, and where? Thank you so much for any help. Here is my code now:

 

...

$result = mysql_query("SELECT u.file_number, u.unit_address1, u.bldg_num, u.apt, u.mutual, r.tel_num, r.res1_f_name, r.res1_l_name from unit u, resident_info r where u.file_number = r.file_number AND u.mutual = $mutual");

 

print "<table align='center'>";

 

// print headers

 

for ($c=0; $c<mysql_num_fields($result); $c++) {

print "<th>".mysql_field_name($result, $c)."</th>";

}

// print table rows

while( $record = mysql_fetch_array( $result )){

//$colors = array("#DCE1E7", "#E9EBEF");

 

$i = 0;

if($i%2)

{

$color="#DCE1E7";

}

else

{

$color="#E9EBEF";

}

 

$colors = array($color, $i);

print "<tr bgcolor=$color>";

for( $c=0; $c<mysql_num_fields( $result ); $c++ ) {

print "<td align='center'>".$record[$c]."</td>";

}

print "</tr>";

}}

print "</table>";

 

?>

If I understand what you want to do correctly, then just nest the for loop within the while loop and increment a counter variable.

 

  //Need to keep track of how many times you have looped in the while.
  $count = 0;

  for ($c=0; $c<mysql_num_fields($result); $c++) {
      print "<th>".mysql_field_name($result, $c)."</th>";
   }
      // print table rows
      while( $record = mysql_fetch_array( $result )){
         $count++;
         if ( $count == 20){
              for ($c=0; $c<mysql_num_fields($result); $c++)
                   print "<th>".mysql_field_name($result, $c)."</th>";
               //Reset $count to 0 so headers will be printed after another 20 iterations
              $count = 0;
         }

         //$colors = array("#DCE1E7", "#E9EBEF");
         
         $i = 0;
         if($i%2)
            {
               $color="#DCE1E7";
            }
            else
            {
               $color="#E9EBEF";
            }
         
         $colors = array($color, $i);
         print "<tr bgcolor=$color>";
         for( $c=0; $c<mysql_num_fields( $result ); $c++ ) {
            print "<td align='center'>".$record[$c]."</td>";
         }
         print "</tr>";
      }}

hey thefortress u f-ing rock dude, your response answered my question perfectly and I even learned something! I just wasnt sure where to nest that loop and how to set the count to 0 again. Brilliant!

U are a php guru and i love u. I said if my question gets answered I would donate to phpfreaks and now I will and will be a regular member. I love this site. It is a crucial resourse now on my job. :)

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.