Printing Out Records

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Printing Out Records

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

First listing in the Useful Posts thread :wink:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

That is why you are the extreme guru moderator... :wink:
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Hmhmhm...

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Post Reply