2017-03-31 143 views
0

我有多个奎雷斯从MySQL数据库检索数据(多表)不同的PHP变量,包括创建表if语句

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,则忽略计数

这里是适度的尝试,工作,因为它有定义变量,如果陈述和逻辑不正确?

$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

试试这个方法。它应该工作,你不担心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>"; 
 
     ?>

+0

虽然看起来有效,但不和我一起工作,如上面提到的,我想将其显示为$变量以后将pupulated在一个领域... – MikeDerik

+0

任何其他的想法吗? – MikeDerik

+0

看一看就是你的第二个片段。我假设信息存在,并且您的SQL数据被检索并正确存储。 –