【问题标题】:how to populate two diffrent html tables inside php while loop depending on condition?如何根据条件在 php while 循环中填充两个不同的 html 表?
【发布时间】:2013-11-23 05:09:41
【问题描述】:

我想在 php while 循环中填充两个不同的 html 表。我当前的代码在名称不为空时填充表 1($row[name])。但是,我如何声明 table2 以便仅在名称为空时填充 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>";

【问题讨论】:

    标签: php html mysql html-table


    【解决方案1】:

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

    这里有一个演示如何做到这一点

    $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>";
    }
    

    【讨论】:

    • 谢谢大家。 dianuj 谢谢你的解决方案非常好。我不知道我可以在变量中附加表数据!
    【解决方案2】:

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

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

    基本上是这样的:

    while($row = mysqli_fetch_array($result)) $rows[] = $result;
    
    foreach( $rows as $row ) {
      // Render table 1
    }
    
    foreach( $rows as $row ) {
      // Render table 2
    }
    

    【讨论】:

      【解决方案3】:

      与其将表回显,不如将每个表存储到一个变量中。循环完成后,回显变量。示例

      $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
      

      【讨论】:

        猜你喜欢
        • 2014-02-13
        • 1970-01-01
        • 1970-01-01
        • 2013-11-03
        • 2011-10-08
        • 1970-01-01
        • 2012-10-30
        • 2019-03-29
        • 2015-06-18
        相关资源
        最近更新 更多