简体   繁体   English

HTML表格中的PHP数组

[英]PHP arrays in HTML table

I got 3 PHP arrays which I've put inside a HTML table. 我有3个PHP数组,我把它放在HTML表格中。 The table headers are correctly displayed but the data not. 表头正确显示但数据没有。 That are the <td> 's. 那是<td>的。 The table rows and <td> 's are displayed under each other and not in the columns. 表行和<td>显示在彼此之下而不是列中。 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. 我用<tr> <td>没什么用。

EDIT This is the $result variable. 编辑这是$ result变量。 (Using the ADLDAP php class). (使用ADLDAP php类)。

$result = $adldap->group()->members($groepbekijken, $sorted = true);

You are constructing incorrect html, after every <td></td> you also have a </tr> closing tag. 你正在构建不正确的html,在每个<td></td>你还有一个</tr>结束标记。

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

Here you only output 1 in every 在这里你只输出1

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> 在这里输出<td></td>或输出<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> 在这里输出<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. 你有三次相同$结果的foreach。 why? 为什么?

Put it into one foreach, makes it more readable: 把它放在一个foreach中,使它更具可读性:

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'. 不要混淆演示文稿'html'和逻辑'那$ adlap-> thing'。

And please, dont echo stuff out in a function. 请不要在函数中回显东西。 return it as a string, and then echo it. 将其作为字符串返回,然后回显它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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