简体   繁体   English

在PHP中输出Mysql数组

[英]Outputting Mysql array in PHP

I am creating a quoting system for our company and I have come into a small problem. 我正在为我们的公司创建一个报价系统,但我遇到了一个小问题。 Here's the rundown; 这是败笔; I have two tables, one named data, the other named zips. 我有两个表,一个名为数据,另一个名为zips。 The client enters their gender, age, if they use tobacco, zipcode, and state. 如果客户使用烟草,邮政编码和州,则输入其性别,年龄。

Here's the data table: 这是数据表:

图片

Here's the zips table: 这是zips表:

图片

I've queried the two tables to join based on zip lookup code. 我已经查询了两个基于zip查找代码联接的表。 Here's the query I'm using: 这是我正在使用的查询:

SELECT data.Monthly_Rate, zips.ZIP_LOOKUP_CODE AS Expr1, zips.State, zips.County, zips.City, zips.Zipcode
FROM data INNER JOIN
zips ON data.ZIP_LOOKUP_CODE = zips.ZIP_LOOKUP_CODE
WHERE (zips.Zipcode = '$zipcode') AND 
(data.Company_Old LIKE '%Blue Cross%') AND
(data.Plan IN ('A','F','F (High)','G','N')) AND 
(data.Gender = '$gender') AND 
(data.Age = '$age') AND 
(data.Tobacco = '$tobacco') AND 
(data.State = '$state');

Now what I want to do is output this in php in a table. 现在,我想做的就是在一个表的php中输出此内容。 The problem I'm coming across is there are multiple Plan letters under the "Plan" column. 我遇到的问题是“计划”列下有多个计划字母。 I only want to return 5 different ones, but the issue is some companies don't offer all 5 of those plans, so in this instance when I'm outputting the array, the rate for a certain plan will line up in the wrong column in the table. 我只想返回5个不同的计划,但是问题是有些公司没有提供所有这5个计划,因此在这种情况下,当我输出阵列时,某个计划的费率将在错误的列中排列在桌子上。

Basically, I don't know how to align the data correctly in the specific column it should be in. In a scenario where a plan A, F, and N are only available for the company, it will fill the first three fields, and the rate for plan N will not line up in the plan N column. 基本上,我不知道如何正确对齐数据应在的特定列中。在计划A,F和N仅适用于公司的情况下,它将填充前三个字段,并且计划N的费率不会在计划N列中排列。

I need to figure out how to associate a given rate with the Plan letter, and output it correctly in the table. 我需要弄清楚如何将给定的费率与计划字母关联起来,并在表格中正确输出。 In the instance below Plan A rate is $111.40, Plan F is 135.37 and Plan N rate is $96.52. 在下面的示例中,计划A的费率为$ 111.40,计划F的费率为135.37,计划N的费率为$ 96.52。 As you see, the Plan N rate is not aligning correctly. 如您所见,N计划费率未正确对齐。 How do I go about fixing this? 我该如何解决这个问题?

Table output: 表输出:

图片

This is what I'm using to output the array in PHP: 这就是我用来在PHP中输出数组的内容:

while($row = mysql_fetch_array($PlanRates))
{   
$Plan = "$" . number_format($row['Monthly_Rate'], 2, '.', '');
echo "<td align='center'>$Plan</td>";           
}
echo "</tr>";
mysql_free_result($PlanRates);

Retrieve plan as well in your SQL query 在SQL查询中也检索计划

SELECT data.Monthly_Rate, data.Plan ...

Then use the below code 然后使用下面的代码

while($row = mysql_fetch_array($PlanRates)) {   
  $Plans[$row['Plan']] = "$" . number_format($row['Monthly_Rate'], 2, '.', '');
}
echo "<td align='center'>".(isset($Plans['A']) ? $Plans['A'] : '')."</td>"; 
echo "<td align='center'>".(isset($Plans['F']) ? $Plans['F'] : '')."</td>"; 
echo "<td align='center'>".(isset($Plans['F (High)']) ? $Plans['F (High)'] : '')."</td>"; 
echo "<td align='center'>".(isset($Plans['G']) ? $Plans['G'] : '')."</td>"; 
echo "<td align='center'>".(isset($Plans['N']) ? $Plans['N'] : '')."</td>"; 

You shouldn't be mixing your data processing logic and your presentation this way though. 但是,您不应该这样混合数据处理逻辑和演示文稿。 You should use a function from which to run the query and return the data as an array. 您应该使用一个函数来运行查询并以数组形式返回数据。 Then iterate over the array in the html markup. 然后遍历html标记中的数组。

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

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