PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
no, as long as you keep your code clean and understandable, this is a good solution. you can put the inner for() into an extern function and call it, but it's not necessary, due to the fact that you can quickly vary the rows/cols count this way.
/*
* make HTML to display images
*
* @param $images an array with assoc arrays that have keys src and alt
* @param $rows the number of rows
* @param $cols the number of columns
* @return the generated HTML
*/
function makeGallery($images, $rows, $cols)
{
$html = '';
for ($i = 0; $i < $rows; ++$i)
{
$html .= '<tr>';
for ($j = $j < $cols; ++$j)
{
$html .= '<td><img src="{$images[(($i * $cols) + $j)]['src'}" alt="{$images['(($i * $cols) + $j)]['alt']" /></td>';
}
$html .= '</tr>';
}
return $thml;
}