Page 1 of 1

Printing Out Records

Posted: Thu Feb 16, 2006 4:00 pm
by icesolid
I have a table that has the following fields:

tablename = cases

file1
caption1
file2
caption2
file3
caption3
file4
caption4
file5
caption5
file6
caption6

I want to print out these fields 3 per line in a table, however sometimes there may not be a file 5 or 6, so I want the program to print out only the fields that have information in them. I want the final table print out if there was all six files to look something like this:

Code: Select all

<table>
  <tr>
    <td><?php echo $row["file1"]; ?> - <?php echo $row["caption1"]; ?></td>
    <td><?php echo $row["file2"]; ?> - <?php echo $row["caption2"]; ?></td>
    <td><?php echo $row["file3"]; ?> - <?php echo $row["caption3"]; ?></td>
  </tr>
  <tr>
    <td><?php echo $row["file4"]; ?> - <?php echo $row["caption4"]; ?></td>
    <td><?php echo $row["file5"]; ?> - <?php echo $row["caption5"]; ?></td>
    <td><?php echo $row["file6"]; ?> - <?php echo $row["caption6"]; ?></td>
  </tr>
</table>
I want the final table print out if there was only 4 files to look something like this:

Code: Select all

<table>
  <tr>
    <td><?php echo $row["file1"]; ?> - <?php echo $row["caption1"]; ?></td>
    <td><?php echo $row["file2"]; ?> - <?php echo $row["caption2"]; ?></td>
    <td><?php echo $row["file3"]; ?> - <?php echo $row["caption3"]; ?></td>
  </tr>
  <tr>
    <td><?php echo $row["file4"]; ?> - <?php echo $row["caption4"]; ?></td>
  </tr>
</table>
All of these print outs of course are all done by PHP, not manually.

Posted: Thu Feb 16, 2006 4:13 pm
by RobertGonzalez
You are going to have use an array, the count() function and some logic. You are also going to have to add in a colspan="X" attribute to the <td> tag in case it is not a full table.

The basic principle is loop through the data set array. When the loop counter is 0, 3, 6, etc, throw an opening row and table data tag. If the loop counter is 1, 4, 7, etc, just throw the table data tag. If the loop counter is 2, 5, 8, etc, throw a table data tag and a closing row tag. The logic comes into play as you get to the end of your data set array and have to tell the program how to close the table and what to look for depending upon the number of items in the array.

Posted: Thu Feb 16, 2006 4:15 pm
by John Cartwright
First listing in the Useful Posts thread :wink:

Posted: Thu Feb 16, 2006 4:29 pm
by RobertGonzalez
That is why you are the extreme guru moderator... :wink:

Hmhmhm...

Posted: Thu Feb 16, 2006 4:45 pm
by icesolid
I don't understand, those examples show you how to print out rows, I know how to do that.

I want to print out columns that are in the row, in rows and columns, not the rows into rows and columns.

Posted: Thu Feb 16, 2006 5:29 pm
by John Cartwright