[英]how to display mysql results into different columns based off of values
ok I am really struggling for 3 days on learning how to get values from a query to show in different columns based off of the values from the query. 好的,我在学习如何从查询中获取值以根据查询中的值显示在不同列中的过程上确实花费了3天的时间。 Here is what I want it to look like...
这是我想要的样子...
Name 0-30 30-60 60-90 90+
john $50
june $0.00
joe $100
fred $500
As of right now, I can get a list of the names and the money values above, but I only know how to display them as follows... 到目前为止,我可以获得上面的名称和货币值的列表,但是我只知道如何如下显示它们...
Name 0-30 30-60 60-90 90+
john $50 $50 $50 $50
june $0.00 $0.00 $0.00 $0.00
joe $100 $100 $100 $100
fred $500 $500 $500 $500
I am simple printing the variables in each column. 我很简单地在每列中打印变量。 Any help is greatly appreciated.
任何帮助是极大的赞赏。
echo "<TABLE WIDTH='100%' BORDER='0' CELLSPACING='0' CELLPADDING='5'>
<TR>
<TD WIDTH='5%'><B>Job</B><BR></TD>
<TD WIDTH='30%'><B>Name</B><BR></TD>
<TD WIDTH='12%'><B>0-30 Days</B><BR></TD>
<TD WIDTH='12%'><B>30-60 Days</B><BR></TD>
<TD WIDTH='12%'><B>60-90 Days</B><BR></TD>
<TD WIDTH='12%'><B>90+ Days</B><BR></TD>
<TD WIDTH='15%'><B>Total Amount</B><BR></TD>
</TR><TR>";
SQL query here...the sqlfiddle. 此处的SQL查询... sqlfiddle。 http://sqlfiddle.com/#!2/e5d9c/1/0
http://sqlfiddle.com/#!2/e5d9c/1/0
echo "<TD WIDTH='5%'>$primary_key</TD>
<TD WIDTH='30%'>$a_name</TD>
<TD WIDTH='12%'>$total / $timeperiod</TD>
<TD WIDTH='12%'>$total / $timeperiod</TD>
<TD WIDTH='12%'>$total / $timeperiod</TD>
<TD WIDTH='12%'>$total / $timeperiod</TD>
<TD WIDTH='15%'>$total / $timeperiod</TD>
</TR>";
I know the above is wrong but I am not sure what to put above to display my example of what I want? 我知道以上内容是错误的,但是我不确定上面要显示的内容是我想要的例子吗?
just try to add a condition before the echo in your column. 只需在列中的回显之前添加一个条件即可。 that should looks like :
应该看起来像:
`if ($res['name']>=0 and $res['name']<30) echo $res['data']; `if($ res ['name']> = 0且$ res ['name'] <30)回显$ res ['data'];
if ($res['name']>=30 and $res['name']<60) echo $res['data']; if($ res ['name']> = 30并且$ res ['name'] <60)回显$ res ['data'];
if ($res['name']>=60 and $res['name']<90) echo $res['data'];` if($ res ['name']> = 60并且$ res ['name'] <90)echo $ res ['data'];`
.... ....
//$res['name'] --> john / june ... //$res['data'] --> $50 / $500 ... // $ res ['name']-> john / june ... // $ res ['data']-> $ 50 / $ 500 ...
I think this is the easiest way to do it .. 我认为这是最简单的方法。
Below code snippet will do the needful. 下面的代码片段将满足您的需要。
<?php
$query = "SELECT amount FROM mytable";
$res = mysql_query($query);
while($data = mysql_fetch_row($res))
{
echo "<tr>";
if($data[0] < 30)
{
echo "<td colspan='4' align='left'>".$data[0]."</td>";
}
else
{
echo "<td> </td>";
if($data[0] > 30 && $data[0] < 60)
echo "<td colspan='3' align='left'>".$data[0]."</td>";
else
{
echo "<td> </td>";
if($data[0] > 60 && $data[0] < 90)
echo "<td colspan='2' align='left'>".$data[0]."</td>";
else
{
echo "<td> </td>";
if($data[0] > 90)
echo "<td align='left'>".$data[0]."</td>";
}
}
}
echo "</tr>";
}
?>
SELECT
Name,
c0_30, c30_60, c60_90, c90
FROM (
SELECT
Name,
SUM(IF(val BETWEEN 0 AND 30, val, NULL)) As 'c0_30',
SUM(IF(val BETWEEN 31 AND 60, val, NULL)) As 'c30_60',
SUM(IF(val BETWEEN 61 AND 90, val, NULL)) As 'c60_90',
SUM(IF(val > 90, val, NULL)) As 'c90'
FROM talName
GROUP BY Name WITH ROLLUP
) AS sums;
This query will output the total value of val
col of a person and categorize them to the suitable column based on its total value. 该查询将输出一个人的
val
col的总价值,并根据其总价值将其分类到合适的列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.