aborted
aborted

Reputation: 4541

Producing a table from a loop with one array

I'm attempting to create a table using a loop. The number of columns is what matters, it should be 6-7, while the number of rows is irrelevant.

The problem here is that I need to create this from one array only, which has a set of image names which I need to display through the table.

This is the PHP:

if ($mode == 'skins')
{
    $player_gender  = ($player_data['playerGender'] == true) ? 'male' : 'female';
    $skins_array    = $samp->skin('small', false, $player_gender);
    $index_counter  = 0;

    foreach ($skins_array as $skin_img)
    {
        $template->assign_block_vars('skinrow', array(
            'IMAGE_PATH'    => $root_path . $config['skins_path'] . '/Skin_' . $skin_img . '.png',
        ));
    }
}

And this is the HTML:

<div class="container">
    <table>
        <!-- BEGIN skinrow -->
        <tr>
            <td><a href="{skinrow.IMAGE_PATH}"><img src="{skinrow.IMAGE_PATH}" /></a></td>
        </tr>
        <!-- END skinrow -->
    </table>
</div>

The template engine used in this case is from phpBB.

If I include the <tr> in the loop in the HTML, I get my results all going down (vertical) in one column and when I exclude the <tr> from the loop, the results all go aside in one row (horizontal).

So, I basically care for the number of columns only, I want those to be limited to 6-7.

I'm failing to see the logic on achieving this. Any help would be appreicated.

Here is an example array with the data I'm using: http://pastebin.com/uDMeBJw6

If the template engine is causing you trouble to understand the code, please let me know and I'll try to convert it to a pure PHP example.

Upvotes: 0

Views: 77

Answers (1)

frnhr
frnhr

Reputation: 12903

MY phpBB skills are non-existant, but maybe this will do the trick:

Template:

<div class="container">
    <table>
        <tr>
            <!-- BEGIN skinrow -->
                {skinrow.NEW_TR} <!-- **EDITED** -->
                <td><a href="{skinrow.IMAGE_PATH}"><img src="{skinrow.IMAGE_PATH}" />                   </a></td>
            <!-- END skinrow -->
        </tr>
    </table>
</div>

PHP (loop only):

$counter = 0;
foreach ($skins_array as $skin_img)
{
    $new_tr = ($counter && ($counter % 7 === 0)) ? '</tr><tr>' : ''; // **EDITED**
    $template->assign_block_vars('skinrow', array(
        'NEW_TR' => $new_tr, 
        'IMAGE_PATH'    => $root_path . $config['skins_path'] . '/Skin_' . $skin_img . '.png',
    ));
}

Code is untested, this is just an idea.
... and it's probably cleaner to do for instead of dancing with foreach and $counter :)

Upvotes: 1

Related Questions