Tilted Forum Project - TFP - Sexuality, Philosophy and Political Discussion

Go Back   Tilted Forum Project - TFP - Sexuality, Philosophy and Political Discussion > Interests > Tilted Technology

Reply
 
LinkBack Thread Tools
Old 02-06-2004, 09:53 PM   #1 (permalink)
Huggles, sir?
 
seretogis's Avatar
 
Join Date: May 2003
Location: Seattle
[php] Arrays, Table Rows, etc

For a project that I am working on, I am going to need to dynamically create a table of $x amount of elements, and display them in an orderly fashion. For instance, in a table with 4-5 elements per row. The problem with this, is that if you have an array of 10 elements and you decide to split it up into rows of 4, you have two spaces left over which will break the table unless you fill them up with something.

I've devised a way to do this with any array, and it works pretty well, I think. It's a bit late, and I didn't get much sleep last night so I'm kind of fuzzy as to how well I am doing this, or how it could be done better.

Below is the code. Click here to see it in action.

PHP Code:
<?

    $numbers 
= array( "one""two""three""four",
                    
"five""six""seven""eight",
                    
"nine""ten" );

    if ( !
$per_row $per_row 4;

    
$cellwidth 100 $per_row;
    
$cellwidth .= "%";

    for ( 
$x 0$x count$numbers ); $x++ ) {
        
$row[] = $numbers[$x];

        if ( ( 
$x ) % $per_row == ) {
            
$all[] = $row;
            
$row NULL;
        }
        else if ( ! 
$numbers[$x 1] ) {
            
$numbers[$x 1] = "empty";
        }
    }

    echo 
$per_row " items per row: <br><br>\n";

    echo 
"<table border=\"1\" width=\"400\"><tr>";

    for ( 
$x 0$x count$all ); $x++) {
        for ( 
$y 0$y count$all[$x] ); $y++ ) {
            echo 
"<td align=\"center\" width=\"$cellwidth\">" $all[$x][$y] . "</td>";
        }
        echo 
"</tr>\n<tr>";
    }

    echo 
"</tr></table>\n<br>\n";

    echo 
"<form action=\"./numbers.php\" method=\"post\">\n" .
         
"<input type=\"text\" name=\"per_row\" size=\"2\">\n" .
         
"<input type=\"submit\" value=\"items per row\">\n</form>\n";

?>
__________________
seretogis - sieg heil
perfect little dream the kind that hurts the most, forgot how it feels well almost
no one to blame always the same, open my eyes wake up in flames
seretogis is offline   Reply With Quote
Old 02-06-2004, 09:56 PM   #2 (permalink)
It's Just A Game
 
n0nsensical's Avatar
 
Join Date: Sep 2003
Location: San Francisco
Try printing "&amp;nbsp;" instead of "empty"; it should just give you a blank table entry if that's what you really want.

Also, it's putting a blank table row at the bottom of the table, so changing

PHP Code:
echo "<table border=\"1\" width=\"400\"><tr>";

    for ( 
$x 0$x count$all ); $x++) {
        for ( 
$y 0$y count$all[$x] ); $y++ ) {
            echo 
"<td align=\"center\" width=\"$cellwidth\">" $all[$x][$y] . "</td>";
        }
        echo 
"</tr>\n<tr>";
    }

    echo 
"</tr></table>\n<br>\n"
to

PHP Code:
echo "<table border=\"1\" width=\"400\">";

    for ( 
$x 0$x count$all ); $x++) {
        echo 
"<tr>";
        for ( 
$y 0$y count$all[$x] ); $y++ ) {
            echo 
"<td align=\"center\" width=\"$cellwidth\">" $all[$x][$y] . "</td>";
        }
        echo 
"</tr>\n";
    }

    echo 
"</table>\n<br>\n"
should fix that.
__________________
The spice must flow...

"My friends I will have an energy policy that we will be talking about, which will eliminate our dependence on oil from the Middle East that will – that will then prevent us – that will prevent us from having ever to send our young men and women into conflict again in the Middle East."
--John McCain May 2nd 2008

Last edited by n0nsensical; 02-06-2004 at 10:02 PM.
n0nsensical is offline   Reply With Quote
Old 02-07-2004, 06:31 AM   #3 (permalink)
Tilted
 
Join Date: Apr 2003
Location: Tha Boro
I've got a function like that, wrote it a few years back when I was still learning php, so it's probably not as optimised as it could be.

It does allow you to set the number of columns you want and distributes the items accordingly (adding empty cells if the final row isn't long enough)

It was originally designed for displaying a table of links, hence the m_items variable is a hash of url=name&...

PHP Code:
function TableMatrix($m_width$m_border$m_cols$m_items) {

 
$m_done "false";
 
$m_newcol "false";
 echo
"<table align=center width=$m_width border=$m_border>\n";

 
$m_split explode("&"$m_items);
 
$colcount 1;
 
$itemcount 0;
 
$m_totitem count ($m_split);

 while (
$m_done == "false") {

  if (
$itemcount $m_totitem) {
   
$temp $m_split[$itemcount];

   
$m_urlname explode("="$temp);
   if ((
$colcount == 1) && ($colcount != $m_cols)) {
     echo
"<tr>\n<td width=33%><a href='$m_urlname[0]' target='_blank'>$m_urlname[1]</a></td>\n";
     
$colcount++;
     
$m_newcol "false";
   }
   elseif (
$colcount $m_cols) {
     echo
"<td width=33%><a href='$m_urlname[0]' target='_blank'>$m_urlname[1]</a></td>\n";
     
$colcount++;
     
$m_newcol "false";
   }
   elseif (
$colcount == $m_cols) {
     echo
"<td width=33%><a href='$m_urlname[0]' target='_blank'>$m_urlname[1]</a></td>\n</tr>\n";
     
$colcount 1;
     
$m_newcol "true";
   }
   
$itemcount++;

  }
  elseif ((
$colcount <= $m_cols) && ($m_newcol == "false")) {
   while (
$colcount $m_cols) {
    echo
"<td width=33%>&nbsp;</td>\n";
    
$colcount++;
   }
   if (
$colcount $m_cols) {
    echo
"<td width=33%>&nbsp;</td>\n</tr>\n";
    
$m_done "true";
   }
  }
  else { 
$m_done "true"; }
 }

 echo
"</table>";

hmm, just noticed on here, that the td widths should probably calculated from the number of cols, oh well.
__________________
I try to take life one day at a time, but sometimes several days attack me at once.
DonnChadh is offline   Reply With Quote
Reply

Bookmarks

Tags
arrays, php, rows, table

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -7. The time now is 12:57 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
All text (c) 2002-2008 Tilted Forum Project
"Insignia" vBulletin 3.5 - b6gm6n - x7x7x7.com