PHP:将数组输出到表

时间:2017-11-20 18:42:38

标签: php arrays sorting html-table explode

对于我的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循环中爆炸的特定分解将其输出到表格中。但是,这样做不会将它们分解到表的空间中,每行只有一个盒子,而另外两个盒子是空的。它将数据打印到Firstname列中的所有表中,每行包含数据。将数据格式化为特定行会导致它不会在列中打印任何内容,只会在空表中打印。

在将爆炸函数添加到代码底部附近的foreach循环之前,以下是我的代码输出到Web浏览器的方式。

       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

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

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

我对你的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>';
}  
?>

有关原始代码的一些评论:

  1. 在您的代码中: $array=explode("\n", $fileInputInData);$arrayExplode = explode("\n", $array);
  2. 两次做同样的事情然后抛出错误。

    1. 正如@PatrickSJ所提到的,你的HTML输出是在3行而不是在一行上写firstName,lastName和email。

      <tr><td>$firstName</td></tr>
      <tr><td>$lastName</td></tr>
      <tr><td>$email</td></tr>
      
    2. VS

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

      希望这有帮助!

答案 1 :(得分:0)

与@ 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>