如何显示表中的所有值

时间:2013-03-19 21:39:11

标签: php mysql arrays fetch

我在下面的代码中遇到的就业表遇到了问题。我希望表显示已插入表中的所有作业。相反,它只显示最新的一个。显示这些的值是$ ee(开始日期和结束日期的$ start和$ end)。再次问题是它没有显示最后一个所有的工作。有谁知道如何解决这个问题? Isit与数组有关吗?提前致谢

  $employment_table = "no table";
  $sql = "SELECT * FROM history WHERE userID='$profile_id' AND type='job'";
  $query = mysqli_query($db_conx, $sql) or die(mysqli_error($db_conx));

   while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
     $h_id = $row["id"];
     $ee = $row["ee"];
     $htype = $row["type"];
     $unixstart = $row["start"];
     $unixend = $row["end"];
       $start = date("d/m/Y",$unixstart);
       $end =  date("d/m/Y",$unixend);
  $employment_table = "<table>";
  $employment_table .= "<tr>";
  $employment_table .= "<th>Company Name</td>";
  $employment_table .= " <th>Start Date</td>";
  $employment_table .= "<th>End Date</td>";
  $employment_table .= "</tr>";
  $employment_table .= "<tr>";
  $employment_table .= "<td>".$ee."</td>";
  $employment_table .= "<td>".$start."</td>";
  $employment_table .= "<td>".$end."</td>";
  $employment_table .= "</tr>";
  $employment_table .= "</table>";      
 }

2 个答案:

答案 0 :(得分:0)

每次循环时都会覆盖$employment_table变量。将<table></table>标记移出循环 - 例如

    $employment_table = "<table>";
    while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
         $h_id = $row["id"];
         $ee = $row["ee"];
         $htype = $row["type"];
         $unixstart = $row["start"];
         $unixend = $row["end"];
           $start = date("d/m/Y",$unixstart);
           $end =  date("d/m/Y",$unixend);

      $employment_table .= "<tr>";
      $employment_table .= "<th>Company Name</td>";
      $employment_table .= " <th>Start Date</td>";
      $employment_table .= "<th>End Date</td>";
      $employment_table .= "</tr>";
      $employment_table .= "<tr>";
      $employment_table .= "<td>".$ee."</td>";
      $employment_table .= "<td>".$start."</td>";
      $employment_table .= "<td>".$end."</td>";
      $employment_table .= "</tr>";

     }
$employment_table .= "</table>";

答案 1 :(得分:0)

每次迭代都会覆盖$employment_table。 您还要为每一行创建一个新表。

以下是:

$employment_table = "<table>";
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
    $h_id = $row["id"];
    $ee = $row["ee"];
    $htype = $row["type"];
    $unixstart = $row["start"];
    $unixend = $row["end"];
    $start = date("d/m/Y",$unixstart);
    $end =  date("d/m/Y",$unixend);
    $employment_table .= "<tr>";
    $employment_table .= "<th>Company Name</td>";
    $employment_table .= " <th>Start Date</td>";
    $employment_table .= "<th>End Date</td>";
    $employment_table .= "</tr>";
    $employment_table .= "<tr>";
    $employment_table .= "<td>".$ee."</td>";
    $employment_table .= "<td>".$start."</td>";
    $employment_table .= "<td>".$end."</td>";
    $employment_table .= "</tr>";
}
$employment_table .= "</table>";