Mysql array output in PHP

I am creating a citation system for our company, and I ran into a small problem. Here is a brief summary; I have two tables, one of which is called data, the other is zip. The client enters their gender, age, if they use tobacco, zip code and condition.

Here is the data table:

image

Here's the zips table:

image

I requested two tables to join based on the zip search code. Here is 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 print this in php in a table. The problem I am facing is that there are several plan letters in the Plan column. I just want to return 5 different ones, but the problem is that some companies do not offer all 5 of these plans, so in this case, when I output the array, the rate for a certain plan is lined up in the wrong column in the table.

Basically, I don’t know how to correctly align the data in the specific column in which it should be. In a scenario where plan A, F, and N are available only to the company, it will fill in the first three fields, and the rate for plan N will not line up in the column of plan N.

I need to figure out how to associate a given bid with the letter Plan and correctly display it in the table. In the example below, the Plan A tariff is 111.40 US dollars, the F plan is 135.37, and the N tariff plan is 96.52 US dollars. As you can see, the Plan N speed does not align correctly. How can i fix this?

Table result:

image

This is what I use to output the array in 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); 
+6
source share
1 answer

Get also a plan in your SQL query

 SELECT data.Monthly_Rate, data.Plan ... 

Then use the code below

 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>"; 

However, you should not mix data processing logic and your presentation. You must use the function from which to run the query and return the data as an array. Then iterate over the array in html markup.

+2
source

Source: https://habr.com/ru/post/927036/


All Articles