使用不同的PHP变量创建表,包括if语句

时间:2017-03-31 22:27:05

标签: php html mysql html-table

我有多个quires从mysql数据库(多个表)中检索数据

SELECT description FROM table1 As descr WHERE type='MYTYPE'; //this shows description of each product type
SELECT count(id) FROM table2 As tp1 WHERE ACTIVE='Y' AND type=1"); //this is to count records that has this product type =1
SELECT count(id) FROM table2 As tp2 WHERE ACTIVE='Y' AND type=2");
SELECT count(id) FROM table2 As tp3 WHERE ACTIVE='Y' AND type=3");
SELECT count(id) FROM table2 As tp4 WHERE ACTIVE='Y' AND type=4");

我想要的是每种类型描述的表格,以及活动产品的数量都有这种类型:如果计数为0则忽略计数

这是谦虚的尝试,我知道它并不完美,因此在这里寻求帮助,也许不同的方法也会有所帮助,但这不起作用,因为它有定义变量,它有if语句和逻辑不正确?

$tbl_header = '<tr>
<th width=220px align=left>Type description</th>
<th width=10px align=left>Active products of this type</th>
</tr>';

$tbl_data =     

if (tp1>0) {
'<tr>
<td width=220px align=left><font color=#000000>'. {descr[0][0]}.'</td></font>
<td width=10px align=left><font color=#FF0000>.'$tp1.'</td></font>
</tr>';}

if (tp2>0) {
'<tr>
<td width=220px align=left><font color=#000000>'. {descr[1][0]}.'</td></font>
<td width=10px align=left><font color=#FF0000>.'$tp2.'</td></font>
</tr>';}

;

echo "<table> $tbl_header $tbl_data </table>";

1 个答案:

答案 0 :(得分:1)

试试这个方法。它应该工作,您不必担心if语句位于变量

的中间

<table>
  <tr>
    <th width=220px align=left>Type description</th>
    <th width=10px align=left>Active products of this type</th>
  </tr>
  
  <?php
    if ($tp1 > 0) {
  ?>
    <tr>
      <td width=2 20 px align=l eft>
        < font color=# 000000>
          <?php echo descr[0][0]; ?>
          </font>
      </td>
      <td width=10px align=left>
        <font color=#FF0000>
          <?php echo $tp1; ?>
        </font>
      </td>
    </tr>
    <?php
      }
    ?>

      <?php
        if ($tp2 > 0) {
      ?>
        <tr>
          <td width=2 20 px align=l eft>
            < font color=# 000000>
              <?php echo descr[1][0]; ?>
              </font>
          </td>
          <td width=10px align=left>
            <font color=#FF0000>
              <?php echo $tp2; ?>
            </font>
          </td>
        </tr>
       <?php
        }
       ?>
</table>

这个怎么样?

<?php
  $table_head = "<tr>
    <th width=220px align=left>Type description</th>
    <th width=10px align=left>Active products of this type</th>
  </tr>";
        if ($tp1 > 0) {
        $table_data_1 = "<tr>
          <td width=2 20 px align=l eft>
            < font color=# 000000>". descr[0][0] . "</font>
          </td>
          <td width=10px align=left>
            <font color=#FF0000>". $tp1. "</font>
          </td>
        </tr>";
       }
       
       if ($tp2 > 0) {
        $table_data_2 = "<tr>
          <td width=2 20 px align=l eft>
            < font color=# 000000>". descr[1][0] . "</font>
          </td>
          <td width=10px align=left>
            <font color=#FF0000>". $tp2. "</font>
          </td>
        </tr>";
       }
       
   echo "<table> $table_ head $table_data_1 $table_data_2 </table>";
   ?>

<?php
$table_data = "";
      $table_head = "<tr>
        <th width=220px align=left>Type description</th>
        <th width=10px align=left>Active products of this type</th>
      </tr>";
            if ($tp1 > 0) {
            $table_data = "<tr>
              <td width=2 20 px align=l eft>
                < font color=# 000000>". descr[0][0] . "</font>
              </td>
              <td width=10px align=left>
                <font color=#FF0000>". $tp1. "</font>
              </td>
            </tr>";
           }
           
           if ($tp2 > 0) {
            $table_data .= "<tr>
              <td width=2 20 px align=l eft>
                < font color=# 000000>". descr[1][0] . "</font>
              </td>
              <td width=10px align=left>
                <font color=#FF0000>". $tp2. "</font>
              </td>
            </tr>";
           }
           
       echo "<table> $table_ head $table_data </table>";
       ?>