0

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.

1
  • 1
    Use a modulo like <?php if ($rowNumber % 6 == 0) print '<tr>' .... and the same if conditional for the closing tr tag. Commented Oct 27, 2013 at 14:27

1 Answer 1

1

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 :)

Sign up to request clarification or add additional context in comments.

5 Comments

This is producing the same effect as putting the <tr> in the loop again :\
it shouldn't! The idea is to have </tr><tr> only in every 7th (or 6th) block. I'm probably doing something wrong with the template logic (blocks) if it does :(
Sorry, it worked! It was a mistake from my part. I apologize for this.
Sure thing! You deserved it as you helped me out. Anyway, how can I make the first <td> part of the first row? It looks like this momentarily: i.imgur.com/j6esOCU.png
@Dugi Um, yeah, I was concerned to make the ending of the table ok and so I forgot all about the beginning :) See my edited answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.