php-generated table question

Joined: 08/03/2009
User offline. Last seen 2 years 19 weeks ago.

i'm using drupal views to create a grid view of photos (a gallery).

the max number of columns is 4, the max rows is 2. so essentially, i have 8 photos total, 4 on each row. i've got it styled to fit the confined space in the div layer.

but, for example, when there are less than 4 photos, the php only generates 1 row, with that number of columns. i'm thinking if i can set up a "if the number of rows is less than 2, still set up that second row" and "if the number of columns is less than 4, still set up those extra columns" argument, it will display as if place-holder images are there, right?

here is the drupal code:

foreach ($rows as $row_number => $columns): ?>
php
$row_class = 'row-' . ($row_number + 1);
if ($row_number == 0) {
$row_class .= ' row-first';
}
elseif (count($rows) == ($row_number + 1)) {
$row_class .= ' row-last';
}

< tr class >="php print $row_class; ?>">
php foreach ($columns as $column_number => $item): ?>
< td class >="php print 'col-'. ($column_number + 1); ?>">
php print $item; ?>
< /td >
php endforeach; ?>
< /tr >
php endforeach; ?>

can anyone help me out on this?

Why not just emit a string of

Why not just emit a string of img elements, with a class, and in the CSS make them all the same size float them all left? It will fill across the first row and start the second row with the 5th img.

Joined: 10/18/2008
User offline. Last seen 1 year 27 weeks ago.
Why do you need it to show 8

Why do you need it to show 8 every time? Are you trying to make sure the pages all look the same or do you have some other purpose for requiring 8 cells each time?

You can use array_pad to make sure your $rows and $columns array are always the same length or you could also hardcode the numbers into for loops

for($row_count=0;$row_count<4;$row_count++){
//do table stuff
  if (!isset($row[$row_count])) {
    // print an empty row
  }
}

Then do the same thing with your columns.

But unless you have a purpose for needing those extra cells, I'd go with Arlen's css approach. It would make life a LOT easier in this case.

<?php
<!-- Create an image gallery wrapper -->
<
div class="img_gallery">
foreach (
$rows as $row_number => $columns):
 
$row_class = 'row row-' . ($row_number + 1);
  if (
$row_number == 0) {
   
$row_class .= 'row row-first';
  }
  elseif (
count($rows) == ($row_number + 1)) {
 
$row_class .= 'row row-last';
  }
?>

< div class ="<? php print $row_class; ?>">
<?php foreach ($columns as $column_number => $item): ?>
< div class ="<?php print 'column column-'. ($column_number + 1); ?>">
<?php print $item; ?>
</div >
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</div>

Then in a css file you'd just set your row to whatever width you'd want

div.img_gallery{
  width:800px;
}

div.column{
  width:200px; // or whatever the img_gallery is divided by 4
  float:left;
  padding:10px;
}

This is a lot more better than tables.

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 08/03/2009
User offline. Last seen 2 years 19 weeks ago.
so i realized, only now, that

so i realized, only now, that this is exactly the same problem i had when i was working on the links.

also, like i said before, i'm a newb, so by the time i got to the links, i figured out defining the cells' size in the css.

so, in all reality, i can take the code you spit out for the links, and change it to fit THIS problem. though i am interested in playing around with the solution you mentioned above.

Joined: 10/18/2008
User offline. Last seen 1 year 27 weeks ago.
Sure if you have a class for

Sure if you have a class for rows and a class for columns then you could use float for the columns and then clear:both for the rows. Oh, and set a width so everything looks good.

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 08/03/2009
User offline. Last seen 2 years 19 weeks ago.
yikes. after playing with the

yikes. after playing with the tables for WAY too long, i realized Arlen is TOTALLY right here. it's so darned simple, that it was TOO simple.