繁体   English   中英

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

[英]Create table with different PHP variables including if statement

我有多个需要从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>";

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM