dongyinglan8707 2017-03-31 22:27
浏览 19
已采纳

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

I have multiple quires retrieving data from mysql database (multiple tables)

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");

What I'm trying to have is a table of each type description, along with count of active products has this type: ignoring the count if the count is 0

Here is modest try, I know it isn't perfect, therefore seeking help here, perhaps different approach will be also helpful, but this doesn't work as it have defining variable which has if statement and also logic is not correct?

$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条回答 默认 最新

  • drv16821 2017-03-31 22:44
    关注

    Try this method. It should work and you don't have to worry about if statements being in the middle of variable

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

    How about this?

    <?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>";
           ?>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路