0

I got 3 PHP arrays which I've put inside a HTML table. The table headers are correctly displayed but the data not. That are the <td>'s. The table rows and <td>'s are displayed under each other and not in the columns. How can I fix this?

Here is my code:

echo '<table border="1"><tr><th><b>Gebruikersnaam</b></th><th>Functie</th><th>E-mailadres</th></tr>';
sort($result);

foreach($result as $key)
{
  echo '<tr>';
  echo '<td>'.$key.'</td>';
  echo '</tr>'; 
}

foreach ($result as $key=>$function)
  {
    $check = $adldap->user()->info($function, array("title"));
    $title = $check[0]['title'][0];
    if(empty($title)) {
    echo '<td>Geen functie</td>';
    } else {
    echo '<tr>';
    echo '<td>'.$title.'</td>';
    echo '</tr>';
    }
    }
foreach ($result as $key=>$function)
{
    $check = $adldap->user()->info($function, array("mail"));
    $title = $check[0]['mail'][0];
      echo '<tr>';
    echo '<td>'.$title.'</td>';  
      echo '</tr>';
}
echo '</table>';
}

And here is an idea what my problem is:

| Column 1        |  Column 2      |  Column 3        |
----------------------------------------------------
|   value1        |                |                  |
|---------------------------------------------------
|   value2        |                |                  |
|---------------------------------------------------
|   value3        |                |                  |
|---------------------------------------------------
|   value4 etc    |                |                  |
|---------------------------------------------------

All the arrays appear in the first column. I used <tr> <td> nothing works.

EDIT This is the $result variable. (Using the ADLDAP php class).

$result = $adldap->group()->members($groepbekijken, $sorted = true);
3
  • Could you also list what the content of $result is? Commented May 20, 2014 at 9:29
  • I updated my post with the $result variable. Commented May 20, 2014 at 9:32
  • The result of the command var_dump($result), will be welcomed. It will show us what you have stored inside it, because the problem probably lies in there. Commented Jun 7, 2014 at 0:26

1 Answer 1

2

You are constructing incorrect html, after every <td></td> you also have a </tr> closing tag.

foreach($result as $key)
{
  echo '<tr>';
  echo '<td>'.$key.'</td>';
  echo '</tr>'; 
}

Here you only output 1 in every

foreach ($result as $key=>$function)
  {
    $check = $adldap->user()->info($function, array("title"));
    $title = $check[0]['title'][0];
    if(empty($title)) {
    echo '<td>Geen functie</td>';
    } else {
    echo '<tr>';
    echo '<td>'.$title.'</td>';
    echo '</tr>';
    }
    }

Here you or output <td></td> OR you output <tr><td><td></tr>

foreach ($result as $key=>$function)
{
    $check = $adldap->user()->info($function, array("mail"));
    $title = $check[0]['mail'][0];
      echo '<tr>';
    echo '<td>'.$title.'</td>';  
      echo '</tr>';
}

And here you output <tr><td></td></tr>

So, what is the problem then? well, your code is a mess. Sorry, but it really is.

You have a foreach over the same $result three times. why?

Put it into one foreach, makes it more readable:

foreach ( $result as $key => $functions )
{
    echo '<tr>';

        echo '<td>'.$key.'</td>';

        $check = $adldap->user()->info($function, array("title"));
        $title = $check[0]['title'][0];

        if( empty($title) )
        {
            echo '<td>Geen functie</td>';
        } else
        {
            echo '<tr>';
            echo '<td>'.$title.'</td>';
            echo '</tr>';
        }

        $check = $adldap->user()->info($function, array("mail"));
        $title = $check[0]['mail'][0];
        echo '<tr>';
            echo '<td>'.$title.'</td>';  
        echo '</tr>';


    echo '</tr>';
}

But then again, this is still bad code. Don't mix presentation 'html' and logic 'that $adlap-> thing'.

And please, dont echo stuff out in a function. return it as a string, and then echo it.

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

Comments

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.