【问题标题】:PHP : outputting array into tablePHP:将数组输出到表中
【发布时间】:2018-05-04 01:58:19
【问题描述】:

对于我的 PHP 项目,我需要将排序后的数组打印到表格中。我们不能使用预制的排序函数来做到这一点,并被指示制作我们自己的排序函数。排序工作,但它不会按照我们需要的方式进入 html 表格格式。

    <?php
    $fileInput = "guestBook.txt";
    $fileInputInData = file_get_contents($fileInput);
    $array=explode("\n", $fileInputInData);

    for($j = 0; $j < count($array); $j ++) 
    {
      for($i = 0; $i < count($array)-1; $i ++)
      {

        if($array[$i] > $array[$i+1])
        {
        $temp = $array[$i+1];
        $array[$i+1]=$array[$i];
        $array[$i]=$temp;

        }       
      }
     }
     echo "<th>Firstname</th><th>Lastname</th><th>Email Address</th>";
     $arrayExplode = explode("\n", $array);
     $k = 0;
     foreach($array as $arrayOut)
     {
     $arrayExplodeTwo = explode(" ", $arrayExplode[$k]);
     $firstName =  $arrayExplodeTwo[0];
     $lastName = $arrayExplodeTwo[1];
     $email = $arrayExplodeTwo[2];
     echo '<tr><td>'; echo $firstName; echo '</td></tr>';echo '<tr><td>'; 
     echo $lastName; echo '</td></tr>';
     echo '<tr><td>'; echo $email; echo '</td></tr>';
     $k++;  
     }


     ?>

代码输出如下:

           Firstname Lastname Email

代码不想将数据正确地打印到表中。在foreach循环中取出explode的具体分解输出到表格中。但是,这样做不会将它们分成表格的空间,每行只有一个框,其他 2 个框是空的。它将数据打印到表中的 Firstname 列中,每行都有数据。将数据格式化为特定的行会导致它在列中不打印任何内容,只是一个空表。

这是我的代码在将explode函数添加到代码底部附近的foreach循环之前如何输出到网络浏览器的。

       Firstname                            Lastname    Email Address
       Anon emous anon@email.com
       Matthew rando rando@gmail.com has signed in.
       Matthew rando rando@gmail.com has signed in.
       Matthew rando rando@gmail.com has signed in.
       Person Anon Anon@emailaddr.com has signed in.
       Person AnonyMouse AnonyMouse@emailaddr.com has signed in.
       Person Name randomaddr@example.com has signed in.
       Test Person thispersonisatest@fake.com has signed in.
       Test PersonTwo thispersonisatestTwo@fake.com has signed in.
       random name randomname@example.com
       test personand testPersonAnd@testemail.com has signed in.

在foreach循环中添加explode函数后,打印结果如下:

       Firstname    Lastname    Email Address

表格周期内没有数据,输出为空白表格。

提前感谢您的帮助。

【问题讨论】:

  • 首先,注意到你有'&lt;tr&gt;&lt;td&gt;'; echo $firstName; echo '&lt;/td&gt;&lt;/tr&gt;'。这是每行 1 个单元格,而不是每行 3 个单元格。应该是&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;。二、为什么是foreach($array as $arrayOut)而不是foreach($arrayExplode as $arrayOut) 三、var_dump($arrayExplodeTwo)的结果是什么?

标签: php arrays sorting html-table explode


【解决方案1】:

与@dchao5 建议的解决方案几乎相同。他提交时正在处理它。主要区别在于使用 php 的内置模板语法,因此您不会在业务逻辑中混合 html 代码。首先进行处理总是更干净,生成页面需要呈现的元素,然后使用模板语法在页面中呈现它们。使维护更加清晰。

<?php

    // all the same did not touch
    $fileInput = "guestBook.txt";
    $fileInputInData = file_get_contents($fileInput);
    $array=explode("\n", $fileInputInData);

    for($j = 0; $j < count($array); $j ++) 
    {
      for($i = 0; $i < count($array)-1; $i ++)
      {

        if($array[$i] > $array[$i+1])
        {
        $temp = $array[$i+1];
        $array[$i+1]=$array[$i];
        $array[$i]=$temp;

        }       
      }
     }
    //--------------------------------------


     $formatted = array();
     foreach($array as $row)
     {

        if($row != ''){

            $columns = explode(" ", $row);

            $formatted[] = array(
                'first'     => $columns[0],
                'last'      => $columns[1],
                'email'     => $columns[2]
            );

        }

     }
?>

<table>
    <thead>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Email</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($formatted as $row): ?>
        <tr>
            <td><?php echo $row['first']; ?></td>
            <td><?php echo $row['last']; ?></td>
            <td><?php echo $row['email']; ?>td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

【讨论】:

    【解决方案2】:

    我对您的 guestBook.txt 文件做了一些假设,并得到了以下代码。


    <?php
    
    # Read and parse the file
    $fileInput = "guestBook.txt";
    $fileInputInData = file_get_contents($fileInput);
    $array=explode("\n", $fileInputInData);
    
    # Sort the data
    for($j = 0; $j < count($array); $j ++)
    {
       for($i = 0; $i < count($array)-1; $i ++)
       {
          if($array[$i] > $array[$i+1])
          {
             $temp = $array[$i+1];
             $array[$i+1]=$array[$i];
             $array[$i]=$temp;
          }
       }
    }
    
    # Check the sort results
    #var_dump($array);
    
    # Echo the table header
    echo "<tr><th>Firstname</th><th>Lastname</th><th>Email Address</th></tr>";
    
    # Loop through each element of the sorted array
    foreach($array as $arrayOut)
    {
       # Explode the current array element by spaces
       $arrayExplode = explode(" ", $arrayOut);
    
       # Assign the data
       $firstName =  $arrayExplode[0];
       $lastName = $arrayExplode[1];
       $email = $arrayExplode[2];
    
       # Echo out the results
       echo '<tr>';
       echo ' <td>'; echo $firstName; echo '</td>';
       echo ' <td>'; echo $lastName; echo '</td>';
       echo ' <td>'; echo $email; echo '</td>';
       echo '</tr>';
    }  
    ?>
    

    关于您的原始代码的一些 cmets:

    1. 在您的代码中: $array=explode("\n", $fileInputInData);$arrayExplode = explode("\n", $array);

    做了两次同样的事情并抛出了一个错误。

    1. 正如@PatrickSJ 提到的,您的 HTML 输出在 3 个单独的行上而不是在一行上写入名字、姓氏和电子邮件。

      <tr><td>$firstName</td></tr>
      <tr><td>$lastName</td></tr>
      <tr><td>$email</td></tr>
      

        <tr>
         <td>$firstName</td>
         <td>$lastName</td>
         <td>$email</td>
        <tr>
    

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 2011-01-28
      • 2017-11-12
      • 1970-01-01
      • 2015-02-16
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 2015-01-05
      • 2018-01-10
      相关资源
      最近更新 更多