简体   繁体   中英

PHP arrays in HTML table

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM