如何根据条件在while循环中填充两个不同的html表?

时间:2013-11-22 21:01:31

标签: php html mysql html-table

我想在php循环中填充两个不同的html表。当name不为空时,我当前的代码填充表1( $ row [name] )。但是,我怎么能声明table2所以它只在name为空时填充table2?任何人都可以告诉我如何声明2个不同的表并根据db中的名称值是否为空来填充它们?

//table declration
echo "<table border='1'>
<tr>
<th>ID</th>
<th>name</th>
<th>page URL</th>
<th>img URL</th>
</tr>";


foreach ($lines as $line) {

//print_r( $line );

$line = rtrim($line);

$result = mysqli_query($con,"SELECT ID,name,imgUrl,imgPURL FROM testdb WHERE imgUrl like '%$line'");            


 if (!$result) {
             die('Invalid query: ' . mysql_error());
            }
//echo $result;


if(mysqli_num_rows($result)>0)
{
  if(mysqli_num_rows($result)>1)
  {
    echo "<br>Duplicate in DB:".$line."<br>";
  };

while($row = mysqli_fetch_array($result))
  {
  $totalRows++;



    if (!empty($row[name])) 
    {


      echo "<tr>";
      echo "<td>" . $row['ID'] ."(".$totalRows. ")</td>";
      echo "<td>" . $row['name'] . "</td>";
      echo "<td>" . $row['imgPURL'] . "</td>";
      echo "<td>" . $row['imgUrl'] . "</td>";  echo "</tr>";


    } 
    else 
    { 
       $emptyNameCounter++;


        //fill table2 if name is empty



    };



}; // end of while loop


}
else
{
   echo "<br>Not Found:".$line."<br>";

}; 

};// end of for each line loop

echo "</table>";

3 个答案:

答案 0 :(得分:1)

最简单的方法是首先在数组中收集数据,然后在该数组上循环两次。

该主题还有其他变体,但基本上它们都是将读数从数据库和渲染分成两个不同的步骤。

基本上类似于:

while($row = mysqli_fetch_array($result)) $rows[] = $result;

foreach( $rows as $row ) {
  // Render table 1
}

foreach( $rows as $row ) {
  // Render table 2
}

答案 1 :(得分:1)

另一种方法是将数据附加到表1和表2的变量中,并在循环之后将数据放入表中

这是一个如何做到这一点的演示

$table1='';
$table2='';
while($row = mysqli_fetch_array($result))
  {
  $totalRows++;
    if (!empty($row[name])) 
    {
      $table1.="<tr>";
      $table1.="<td>" . $row['ID'] ."(".$totalRows. ")</td>";
      $table1.="<td>" . $row['name'] . "</td>";
      $table1.="<td>" . $row['imgPURL'] . "</td>";
      $table1.="<td>" . $row['imgUrl'] . "</td>";  echo "</tr>";

    } 
    else 
    { 
       $emptyNameCounter++;
     $table2.="data to show in table 2";
     //fill table2 if name is empty
    }
} // end of while loop

if(!empty($table1)){
echo "<table border='1'>
<tr>
<th>ID</th>
<th>name</th>
<th>page URL</th>
<th>img URL</th>
</tr>";
echo $table1;
echo "</table>";
}

if(!empty($table2)){
echo "<table border='1'>
<tr>
<th>ID</th>
<th>name</th>
<th>page URL</th>
<th>img URL</th>
</tr>";
echo $table2;
echo "</table>";
}

答案 2 :(得分:1)

不是将表格回显,而是将每个表存储到变量中。循环结束后,回显变量。实施例

$tableOne = "<table border='1'>
    <tr>
        <th>ID</th>
        <th>name</th>
        <th>page URL</th>
        <th>img URL</th>
    </tr>";
$tableTwo = "<table border='1'>
    <tr>
        <th>ID</th>
        <th>name</th>
        <th>page URL</th>
        <th>img URL</th>
    </tr>";

//Code and Query here

while($row = mysqli_fetch_array($result)) {
  $totalRows++;

  if (!empty($row[name])) {
    $tableOne .= "<tr>";
    $tableOne .=  "<td>" . $row['ID'] ."(".$totalRows. ")</td>";
    $tableOne .= "<td>" . $row['name'] . "</td>";
    $tableOne .= "<td>" . $row['imgPURL'] . "</td>";
    $tableOne .= "<td>" . $row['imgUrl'] . "</td>";  echo "</tr>";
  } 
  else { 
    $emptyNameCounter++; 
    //store in $tableTwo if name is empty
  }
}

//close tables, echo out variables