简体   繁体   English

循环:PhP数组循环遍历数据库查询以输出结果

[英]Looping : PhP Array Loops through Database query to output result

I have an array called $helloArray which looks like 我有一个名为$ helloArray的数组,看起来像

[peach] => 1
[banana] => 1
[apple] => 1
[pineapple] => 1
[grapefruit] => 2
[tomatoe] => 2
[giger] => 1
[watermelon] => 1

Database columns look like 数据库列看起来像

City    peach   banana  apple   pineapple   grapfruit   tomatoe ginger  watermelon
Tokyo   0       0       0       500         0            0      0         0
DC      50      55      0       0           0            0      0         0
NY      0       0       0       0           0            500    0         0
Rome    0       0       0       0           90           0      0         0

SQL Statment that I used 我使用的SQL语句

$sql = "SELECT peach,banana,apple,pineapple,grapfruit,tomatoe,ginger,watermelon";
$sql .= " FROM TestTable2";
$sql .= " WHERE city ='NY'";  

Question: How do I loop through the array and then the variable names (aka column names) names so that so that we get NY and Rome values of 2 and 1 for the rest. 问题:如何依次遍历数组,然后遍历变量名(即列名),以便剩下的纽约和罗马的值分别为2和1。 Below is my code that I have tried, not to mention it not working 下面是我尝试过的代码,更不用说它不起作用了

foreach ($helloArray as $key =>$value){
         for($i=0;$i<=odbc_num_fields($connection);$i++)
         { if (odbc_result($connection,$i) > 0) {
                echo $value; }
         }


    }

OKay, not sure if I completely understand your question without an output sample, but I'm guessing you want: Tokyo => 1 DC => 1 NY => 2 Rome => 2 好的,不确定是否没有输出样本就能完全理解您的问题,但是我猜您想要:东京=> 1 DC => 1 NY => 2罗马=> 2

If so, your approach is wrong. 如果是这样,您的方法是错误的。 Don't pull data and then massage it, construct your SQL to return the data you want. 不要提取数据然后再处理它,构造您的SQL以返回所需的数据。

SELECT City, CASE WHEN grapefruit > 0 THEN 2 WHEN tomatoe > 0 THEN 2 WHEN peach > 0 THEN 1 WHEN banana > 0 THEN 1 WHEN ... 

If it has to be data driven then sort your hello array by descending values and then use it to generate the SQL case conditions and return vals 如果必须是数据驱动的,则按递减值对hello数组排序,然后使用它生成SQL大小写条件并返回val

-- edit -- -编辑-

Okay, but the code sample above, you are looping through your array as key=>value pairs. 好的,但是上面的代码示例中,您以key => value对的形式遍历数组。 So first pass, you're on peach=>1, then you loop through every column in the return result (peach, watermelon, etc) and check it's value is greater than 0 and if it is then you output value from your array. 因此,首先通过,您使用的是peach => 1,然后循环遍历返回结果中的每一列(桃子,西瓜等),并检查其值是否大于0,如果是,则从数组中输出值。 (peach = 1) (桃子= 1)

Because there is no comparison between the fieldname and the key of the array, you are simply going to output 1 for each column while on peach, 1 for each column while on banana, etc. While I am assuming you are wanting to check if the field NAMED peach is greater than zero and if so, then output 1, and if the field named watermelon is greater than zero then output 2, you need to modify your loop. 因为字段名和数组的键之间没有比较,所以您只需要在桃子上每列输出1,在香蕉树上每列输出1,等等。我假设您要检查是否名为桃的字段大于零,如果是,则输出1;如果名为西瓜的字段大于零,则输出2,则需要修改循环。 Drop the odbc_num_field and use the odbc_result syntax to specify the column name you want to retrieve. 删除odbc_num_field并使用odbc_result语法指定要检索的列名。 odbc_result($connection, $key) > 0. odbc_result($ connection,$ key)> 0。

But going by your original question it sounds like that isn't really what you are looking for, but instead want a city assigned a certain value based on the largest number returned by those field comparisons described above (NY = 2) in which case my original answer stands and you just need to use SQL to return that value for each city. 但是按照您的原始问题听起来似乎并不是您真正要寻找的东西,而是希望一个城市根据上述字段比较返回的最大数字(NY = 2)分配一个特定的值,在这种情况下原始答案就可以了,您只需要使用SQL即可为每个城市返回该值。

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

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