gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:55:02 25 4
gpt4 key购买 nike

我有多个查询从 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>";
?>

关于php - 使用不同的 PHP 变量(包括 if 语句)创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43150560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com