Jump to content

List an array in a table with predefined columns?


benphp

Recommended Posts

Say you have an $arrayItem = Item01, Item02, Item03, Item04, Item05, Item06, Item07, Item08, Item09, Item10, Item11, Item12.

 

I want to display it in a table so that they are sorted down and across rather than just across:

 

Item01  Item04  Item07  Item09  Item11

Item02  Item05  Item08  Item10  Item12

Item03  Item06

 

and it varies:

 

Item01  Item05  Item09  Item13  Item16

Item02  Item06  Item10  Item14  Item17

Item03  Item07  Item11  Item15  Item18

Item04  Item08  Item12

 

??

 

Thanks!

Link to comment
Share on other sites

Here's what I've got so far. This is tricky.

 

<?php 
$aLine = array('Item01', 'Item02', 'Item03', 'Item04', 'Item05', 'Item06', 'Item07', 'Item08', 'Item09', 'Item10', 'Item11', 'Item12', 'Item13', 'Item14', 'Item15', 'Item16', 'Item17', 'Item18', 'Item19'); 

////tabe 3
print "
<table>
<tr>\n";
$cols = 5;
$colCount = 1; 
$numElements = count($aLine); 

$rows = round($numElements/$cols);

$i = 0; 
$rcount = 1; 
while($i < $numElements) { 
$increment = $i * $rows;
	if ($increment <= $numElements) {
		$cell = $aLine[$increment];
		if ($colCount < $cols) {
			print "\t<td>$cell </td>\n";
		} else if ($colCount == $cols) {
			print "\t<td>$cell </td>\n</tr> \n";
		}
		$colCount ++;
	}
$i++;
}

print "</table>";
?>

Link to comment
Share on other sites

you have the general principle but I think you're over-complicating it.

 

$aLine = array('Item01', 'Item02', 'Item03', 'Item04', 'Item05', 'Item06', 'Item07', 'Item08', 'Item09', 'Item10', 'Item11', 'Item12', 'Item13', 'Item14', 'Item15', 'Item16', 'Item17', 'Item18', 'Item19');

$numCols = 5;

echo "<table><tr>";
foreach($aLine as $key => $val) {
  echo ($key % $numCols == 0)? "</tr><tr>" : "";
  echo "<td>$val</td>";
}
echo "</tr></table>";

Link to comment
Share on other sites

No, the OP is saying they wish a predefined number of columns, with whatever amount of rows that determines. But they want them to be 'dealt' downwards first. As shown in their examples. I'm not entirely sure why it would really matter, but it is an interesting problem. I think I may have a solution, which I will paste after testing it.

Link to comment
Share on other sites

No, the OP is saying they wish a predefined number of columns, with whatever amount of rows that determines. But they want them to be 'dealt' downwards first. As shown in their examples. I'm not entirely sure why it would really matter, but it is an interesting problem. I think I may have a solution, which I will paste after testing it.

 

ah I missed that.... actually I vaguely recall solving this a long time ago in some previous post.  will have to search.

Link to comment
Share on other sites

Well here's my offering for what it's worth, there might very well be a simpler way but...

 

// predefine variables
$items = range(1, 12); // your actual array of items here
$col_count = 5;

// calculated variables
$item_count = count($items);
$row_count = ceil($item_count/$col_count);
$total_squares = $row_count * $col_count;
$full_cols = $col_count - ($total_squares % $item_count);

echo '<table>';
for($row = 0; $row < $row_count; $row++) {
    echo '<tr>';
    for($col = 0; $col < $col_count; $col++) {
        if($col < $full_cols) {
            $index = ($row + 1) + ($row_count * $col);
        } else {
            if($row == ($row_count - 1)) {
                $index = -1;
            } else {
                $index = ($row + 1) + ($full_cols * $row_count) + (($col - $full_cols) * ($row_count - 1));
            }
        }
        
        if(isset($items[$index - 1])) {
            echo '<td>' . $items[$index - 1] . '</td>';
        } else {
            echo '<td> </td>';
        }
    }
    echo '</tr>';
}
echo '</table>';

Link to comment
Share on other sites

This is my take. I'm pretty sure I can merge a lot of it together; ran out of time.

 

$aLine = array('Item01', 'Item02', 'Item03', 'Item04', 'Item05', 'Item06', 'Item07', 'Item08', 'Item09', 'Item10', 'Item11', 'Item12', 'Item13', 'Item14', 'Item15', 'Item16', 'Item17', 'Item18', 'Item19');

$numCols = 4;
$numRows = ceil(count($aLine) / $numCols);
$aLine = array_chunk($aLine,$numRows);
foreach ($aLine as $key => $val) {
  foreach ($val as $k => $v) {
    $taLine[$k][] = $v;
  }
}
$aLine = array();
foreach ($taLine as $key => $val) {
  $aLine = array_merge($aLine, $val);
}

echo "<table><tr>";
foreach($aLine as $key => $val) {
  echo ($key % $numCols == 0)? "</tr><tr>" : "";
  echo "<td>$val</td>";
}
echo "</tr></table>";
?>

Link to comment
Share on other sites

cags - that's the ticket - quite a bit of magic going on there.

It was a simple concept, bit of a bitch to wrap my head around getting the maths right though. The basic theory being you calculate based on a grid position how many 'items' have been used already, then insert the next item. It looks like the approach taken by CV is another one I considered, I discounted it as an option because I couldn't work out how to solve a few of the issues with it. Unfortunately I don't think CV fully did either. If you remove item19 from the example it - if you'll excuse the phrase - goes to cock. Of course I'm not saying my solutions perfect, there may be situations where it will fail, I didn't test it that thoroughly.

 

 

Link to comment
Share on other sites

so I was randomly thinking about this, and I came up with a different approach:

 

$list = range(1,20);

$numCols = 7;
$numRows = ceil(count($list) / $numCols);

echo "<div style='float:left'>";
foreach ($list as $c => $item) {
  echo ($c % $numRows == 0)? "</div><div style='float:left'>" : "";
  echo "<div style='border:solid 1px green;padding:2px'>$item</div>";
}
echo "</div>";

 

Basically the idea here is to minimize the scripting (logic) involved in displaying the values, and let css do the formatting work, instead.  I mean after all, isn't that what it's there for?  There is one tiny "bug" in this that that ceil() kinda makes it want to not be forced to certain column amounts.  For instance, trying to set the columns at 6 will make it format as 5 columns.  The numbers are still ordered right, though.  This may or may not be an issue to you, as you may not need it to be 6 columns.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.