Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   [php] Arrays, Table Rows, etc (https://thetfp.com/tfp/tilted-technology/44675-php-arrays-table-rows-etc.html)

seretogis 02-06-2004 08:53 PM

[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 + 1 ) % $per_row == 0 ) {
            $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";

?>


n0nsensical 02-06-2004 08:56 PM

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.

DonnChadh 02-07-2004 05:31 AM

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.


All times are GMT -8. The time now is 07:56 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73