Jump to content
MakeWebGames

[Competition Questions] Designing Dynamic Tables


Guest Anonymous

Recommended Posts

Guest Anonymous

This is a question that arises from time to time from a variety of sources, but is still a little tricky to answer.

The idea is, we have a set of data in a table - text and url and want to display these links in a table.

So what's so hard about this?

Well lets collect the data first (I won't bother with a database for brevity, but the principal is the same:

 

<?php
$links = array
(
array('text' => "one",      'href' => "one.php"),
array('text' => "two",      'href' => "two.php"),
array('text' => "three",    'href' => "three.php"),
array('text' => "four",     'href' => "four.php"),
array('text' => "five",     'href' => "five.php"),
array('text' => "six",      'href' => "six.php"),
array('text' => "seven",    'href' => "seven.php"),
array('text' => "eight",    'href' => "eight.php"),
array('text' => "nine",     'href' => "nine.php"),
array('text' => "ten",      'href' => "ten.php"),
array('text' => "eleven",   'href' => "eleven.php"),
array('text' => "twelve",   'href' => "twelve.php"),
array('text' => "thirteen", 'href' => "thirteen.php"),
);
?>

 

Now given $columns - being the number of columns we want to render these links in, can you come up with a simple loop to display them cleanly, with the same number of cells (<td>...</td>) per row.

The final solution should be (assuming $columns = 4)

 

<table>
<tr>
	<td>[url="one.php"]one[/url]</td>
	<td>[url="two.php"]two[/url]</td>
	<td>[url="three.php"]three[/url]</td>
	<td>[url="four.php"]four[/url]</td>
</tr>
<tr>
	<td>[url="five.php"]five[/url]</td>
	<td>[url="six.php"]six[/url]</td>
	<td>[url="seven.php"]seven[/url]</td>
	<td>[url="eight.php"]eight[/url]</td>
</tr>
<tr>
	<td>[url="nine.php"]nine[/url]</td>
	<td>[url="ten.php"]ten[/url]</td>
	<td>[url="eleven.php"]eleven[/url]</td>
	<td>[url="twelve.php"]twelve[/url]</td>
</tr>
<tr>
	<td>[url="thirteen.php"]thirteen[/url]</td>
	<td> </td>
	<td> </td>
	<td> </td>
</tr>
</table>

 

Hint: Look at the % operator.

For the more experience programmers, can you modify your original source to accept a flag $across that if true, renders as above, and if false renders as below:

 

<table>
<tr>
	<td>[url="one.php"]one[/url]</td>
	<td>[url="five.php"]five[/url]</td>
	<td>[url="nine.php"]nine[/url]</td>
	<td>[url="thirteen.php"]thirteen[/url]</td>
</tr>
<tr>
	<td>[url="two.php"]two[/url]</td>
	<td>[url="six.php"]six[/url]</td>
	<td>[url="ten.php"]ten[/url]</td>
	<td> </td>
</tr>
<tr>
	<td>[url="three.php"]three[/url]</td>
	<td>[url="seven.php"]seven[/url]</td>
	<td>[url="eleven.php"]eleven[/url]</td>
	<td> </td>
</tr>
<tr>
	<td>[url="four.php"]four[/url]</td>
	<td>[url="eight.php"]eight[/url]</td>
	<td>[url="twelve.php"]twelve[/url]</td>
	<td> </td>
</tr>
</table>

 

Remember, the input array is formatted as an associated array as supplied, and you have one controlling variable: $columns which controls the number of columns to display. This will be from 1 to (say) 20.

Brownie points and scooby snacks will be awarded to clean, efficient (and obviously working) code.

Link to comment
Share on other sites

Re: [Competition Questions] Designing Dynamic Tables

I had a go at it:

It can be viewed at: www.thecrimelife.net/tablecomp.php

By adding a 'col' variable into the URL you can change the amount of columns(leaving it will make it 4); eg:

http://www.thecrimelife.net/tablecomp.php?col=6

The source; (Just the PHP part)

<?php
$links = array
(
array('text' => "one",      'href' => "one.php"),
array('text' => "two",      'href' => "two.php"),
array('text' => "three",    'href' => "three.php"),
array('text' => "four",     'href' => "four.php"),
array('text' => "five",     'href' => "five.php"),
array('text' => "six",      'href' => "six.php"),
array('text' => "seven",    'href' => "seven.php"),
array('text' => "eight",    'href' => "eight.php"),
array('text' => "nine",     'href' => "nine.php"),
array('text' => "ten",      'href' => "ten.php"),
array('text' => "eleven",   'href' => "eleven.php"),
array('text' => "twelve",   'href' => "twelve.php"),
array('text' => "thirteen", 'href' => "thirteen.php"),
);
if ($_GET['col']==0) {
$columns=4;
} else {
$columns=$_GET['col'];
}
$size=count($links);
$rows=ceil($size/$columns);
echo "<table border='1'>";
$num=0;
for ($r=0;$r<$rows;$r++)
{
echo "<tr>";
for ($c=0;$c<$columns;$c++)
{
echo "<td>";
if (isset($links[$num])) {
echo '[url="'.$links[$num]['href'].'"]'.$links[$num]['text'].'[/url]'; 
} else { echo " "; }
echo "</td>";
$num++;
}
echo "</tr>";
}
echo "</table>";
?>

It could probably me more efficient but it works; and the syntax is valid: http://validator.w3.org/check?uri=http://www.thecrimelife.net/tablecomp.php

Might as well try the other one now.

Link to comment
Share on other sites

Guest Anonymous

Re: [Competition Questions] Designing Dynamic Tables

Not bad at all... Not quite the expected solution, but functional (which is always a bonus)... Well done.

I guess I'd better root out my copy now ;)

Link to comment
Share on other sites

Re: [Competition Questions] Designing Dynamic Tables

Here's my go at this:

 

<?php
$links = array
(
array('text' => "one",      'href' => "one.php"),
array('text' => "two",      'href' => "two.php"),
array('text' => "three",    'href' => "three.php"),
array('text' => "four",     'href' => "four.php"),
array('text' => "five",     'href' => "five.php"),
array('text' => "six",      'href' => "six.php"),
array('text' => "seven",    'href' => "seven.php"),
array('text' => "eight",    'href' => "eight.php"),
array('text' => "nine",     'href' => "nine.php"),
array('text' => "ten",      'href' => "ten.php"),
array('text' => "eleven",   'href' => "eleven.php"),
array('text' => "twelve",   'href' => "twelve.php"),
array('text' => "thirteen", 'href' => "thirteen.php"),
);

$columns = (isset($_GET['columns']))?intval($_GET['columns']):4;
$num_links = count($links);
$left_over = (ceil($num_links / $columns) * $columns) - $num_links;
$i = 0;

echo "<table>\n";
foreach($links as $l)
{
echo (($i % $columns) == 0)?"<tr>\n":"";
echo "<td><a href=\"" , $l['href'] , "\">" , $l['text'] , "</a></td>\n";
echo (($i % $columns) == ($columns-1))?"</tr>\n":"";
if ($i == ($num_links-1))
{
	for($i2 = 0; $i2 < $left_over; $i2++)
	{
		echo "<td> </td>\n";
	}
	echo "</tr>\n";
}
$i++;
}
echo "</table>\n";
?>

 

I'll give the harder one a try, but I'll need to rewrite my code since I used foreach.

btw, you should do more of these competitions. Gives me something to do, and it's interesting to see what other people come up with.

Link to comment
Share on other sites

Re: [Competition Questions] Designing Dynamic Tables

Revised code:

 

<?php
$links = array
(
   array('text' => "one",      'href' => "one.php"),
   array('text' => "two",      'href' => "two.php"),
   array('text' => "three",    'href' => "three.php"),
   array('text' => "four",     'href' => "four.php"),
   array('text' => "five",     'href' => "five.php"),
   array('text' => "six",      'href' => "six.php"),
   array('text' => "seven",    'href' => "seven.php"),
   array('text' => "eight",    'href' => "eight.php"),
   array('text' => "nine",     'href' => "nine.php"),
   array('text' => "ten",      'href' => "ten.php"),
   array('text' => "eleven",   'href' => "eleven.php"),
   array('text' => "twelve",   'href' => "twelve.php"),
   array('text' => "thirteen", 'href' => "thirteen.php"),
);

$columns = (isset($_GET['columns']))?intval($_GET['columns']):4;
$num_links = count($links);
$full_size = (ceil($num_links / $columns) * $columns);
$links = array_pad($links, (ceil($num_links / $columns) * $columns), "0");
$i = 0;

echo "<table>\n";
foreach($links as $l)
{
   echo (($i % $columns) == 0)?"<tr>\n":"";
   echo ($l=="0")?"<td> </td>\n":"<td><a href=\"" . $l['href'] . "\">" . $l['text'] . "</a></td>\n";
   echo (($i % $columns) == ($columns-1))?"</tr>\n":"";
   $i++;
}
echo "</table>\n";
?>

 

Thanks for the push in the right direction, now the code is shorter :)

Link to comment
Share on other sites

  • 3 weeks later...

Re: [Competition Questions] Designing Dynamic Tables

Even though its late, thought I'd chime in with a different take on it:

 

ob_start();
$links = array
(
array('text' => "one",      'href' => "one.php"),
array('text' => "two",      'href' => "two.php"),
array('text' => "three",    'href' => "three.php"),
array('text' => "four",     'href' => "four.php"),
array('text' => "five",     'href' => "five.php"),
array('text' => "six",      'href' => "six.php"),
array('text' => "seven",    'href' => "seven.php"),
array('text' => "eight",    'href' => "eight.php"),
array('text' => "nine",     'href' => "nine.php"),
array('text' => "ten",      'href' => "ten.php"),
array('text' => "eleven",   'href' => "eleven.php"),
array('text' => "twelve",   'href' => "twelve.php"),
array('text' => "thirteen", 'href' => "thirteen.php"),
);

echo '<table>';
for ($count = 0, $columns = 4; $count < (ceil(count($links) / $columns) * $columns); $count++) {
echo $count % $columns == 0 ? "\n\t<tr>" : '',

"\n\t\t<td>" . (array_key_exists($count, $links) ? '[url="' . $links[$count]['href'] . '"]' . $links[$count]['text'] . '[/url]' : ' ') . '</td>',

$count % $columns == ($columns - 1) ? "\n\t</tr>" : '';
}
echo "\n</table>";

echo '<pre>' . htmlentities(ob_get_clean()) . '</pre>';

 

Includes easy way to see the result for no reason

Link to comment
Share on other sites

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.   Paste as plain text instead

  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...