1

I'm having a particularly interesting problem. Perhaps my "don't remember the last time I slept" logic is failing...

Anyway, I'm grabbing a list of users from a database table and want to stick it in an HTML table. This works perfect:

$table->construct_header( $lang->username );

while ( $user = $db->fetch_array( $user_query ) )
{
    $link = '<a href="'.$settings[ 'url' ].'/'.get_profile_link( $user[ 'uid' ] ).'" target="_blank">'.$user[ 'username' ].'</a>';
    $table->construct_cell( $link );
    $table->construct_row();
}

However, I want to have three columns. Naturally, I tried this:

$table->construct_header( $lang->username );
$table->construct_header( $lang->username );
$table->construct_header( $lang->username );

while ( $user = $db->fetch_array( $user_query ) )
{
    $static $i = 1;
    if ( $i <= 3 )
    {
        $link = '<a href="'.$settings[ 'url' ].'/'.get_profile_link( $user[ 'uid' ] ).'" target="_blank">'.$user[ 'username' ].'</a>';
        $table->construct_cell( $link );
    }
    else
    {
        $table->construct_row();
        $i = 1;
    }
}

Except nothing gets outputted now. Is there some glaring issue I'm not seeing? I basically need to insert a username into three consecutive columns (straight across) and then generate that row and start again.

7
  • It is hard to tell without a complete script including the table object class. Commented Feb 20, 2011 at 5:40
  • As mlemos said - what is in the $table variable? Is that a class provided by the framework you are using? Have you read the docs for it? Commented Feb 20, 2011 at 5:42
  • 1
    First, use the view source option of the browser to see the generated html. You might have messed up in html when generating the table. Commented Feb 20, 2011 at 5:42
  • @Mark, yes, it's a class provided by the framework I'm using (MyBB to be exact). It's pretty basic and the way I'm using it should work just fine. @Sarwar, nothing but the table header's get outputted, but the class should handle everything the way it is. Commented Feb 20, 2011 at 5:47
  • Do you need to call something to tell $table that you're done defining cells and rows? Commented Feb 20, 2011 at 5:55

2 Answers 2

1

Here's the basic approach I typically use:

$i = 0;
echo '<table><tbody><tr>';
while ( $user = $db->fetch_array( $user_query ) ) {
    if ($i && $i%3 == 0) echo '</tr><tr>';
    $i++;
    echo '<td><a href="'.$settings[ 'url' ].'/'.get_profile_link( $user[ 'uid' ] ).'" target="_blank">'.$user[ 'username' ].'</a></td>';
}
echo '</tr></tbody></table>';

I'm not familiar with MyBB's table class, but this will likely work:

$table->construct_header( $lang->username );
$table->construct_header( $lang->username );
$table->construct_header( $lang->username );

$i = 0;
while ( $user = $db->fetch_array( $user_query ) ) {
    if ($i && $i%3 == 0) $table->construct_row();
    $i++;
    $table->construct_cell( '<a href="'.$settings[ 'url' ].'/'.get_profile_link( $user[ 'uid' ] ).'" target="_blank">'.$user[ 'username' ].'</a>' );
}
$table->construct_row();
Sign up to request clarification or add additional context in comments.

Comments

1
$static $i = 1;

should be

static $i = 1;

2 Comments

I don't know how that got there; it's not in my actual script. Nice catch, though!
$i=1 should be outside the loop and you never increment $i

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.