简体   繁体   English

PHP的SQL,分配变量到数组的每一行

[英]php sql, assign variable to each row of an array

I have an mysqli_query that pulls 3 columns and 10 rows from my table. 我有一个mysqli_query从我的表中提取3列和10行。 I would to assign a variable to each row of the query. 我将为查询的每一行分配一个变量。 I could do 10 seperate querys and assign each to a variable, but i assumed that would be frowned upon. 我可以做10个单独的查询,并将每个查询分配给一个变量,但是我认为那会被皱眉。 (note: connection info in seperate file and not shown here). (注意:连接信息位于单独的文件中,此处未显示)。

<?php

$playoffseedings = mysqli_query($con, "SELECT playoffseed, ttName, ttsUID
                                FROM standings
                                WHERE ttsUID = $season_view
                                AND playoffseed > 0
                                ORDER BY playoffseed
                                LIMIT 10");

$seedings = array($playoffseedings);

    FOREACH($seedings as $ps){ 
        $num_rows = 1;
        while($row = mysqli_fetch_array($ps)){
            $num_rows++;
            echo $row['playoffseed'].$row['ttName'];
            echo "<br>";
    }}
?>

The FOREACH used above works fine and returns 10 rows of the data I queried. 上面使用的FOREACH可以正常工作,并返回我查询的10行数据。 So that's cool. 太酷了。 But I need to be able to display different rows in different places on my page. 但是我需要能够在页面的不同位置显示不同的行。 So I thought assigning each row to a different variable would work well. 因此,我认为将每一行分配给不同的变量会很好。

I tried adding something such as this to assign a variable to each row so that I'd have 10 different variables $seed1, $seed2, $seed3..., each $seedx would be an array of playoffseed, ttName, and ttsUID. 我尝试添加诸如此类的东西来为每行分配一个变量,这样我将拥有10个不同的变量$ seed1,$ seed2,$ seed3 ...,每个$ seedx都是playoffseed,ttName和ttsUID的数组。

FOREACH($seedings as $ps){ 
        $num_rows = 1;
        while($row = mysqli_fetch_array($ps)){
            $num_rows++;
            $seed$num_rows = $row[$num_rows];
}}

I'd like to know how to do the above, but also, I was confused on my journey as to why I couldn't echo a row from the an array using something like 我想知道如何执行上述操作,但是我在旅途中却迷茫不解,为什么我无法使用类似以下内容从数组中回显一行

echo $seedings[1];
echo $seedings[7]['playoffseed'];

Why wouldn't 'echo $seedings[1];' 为什么不回显$ seedings [1] ;? return the result of the second row of the $seedings array? 返回$ seedings数组第二行的结果? I got a blank. 我一片空白。

And why wouldn't 'echo $seedings[7]['playoffseed'];' 为什么不回显$ seedings [7] ['playoffseed'];' return the playoffseed of the 8th row? 返回第八排的季后赛种子? Again, blank. 再次空白。

I felt the 2 questions were tied together because I thought I could either refer to the data I wanted to later with either a variable (ie. $seed3), or I could possibly refer to the data I need using the index (keys?) of the array (ie $seedings[7]['playoffseed']). 我觉得这两个问题联系在一起,因为我想我可以使用变量(即$ seed3)来引用我想稍后使用的数据,也可以使用索引(键?)来引用我需要的数据。数组的值(即$ seedings [7] ['playoffseed'])。

First post! 第一篇文章! Thanks for the help. 谢谢您的帮助。 Usually always find what I need after a few hours of searching. 通常,经过几个小时的搜索,总能找到我需要的东西。 Stuck on this one after a few days of looking. 经过几天的观察,坚持了这一点。

Edit (In an attempt to clarify): My query returns a table (multidimensional array?) like this... 编辑(尝试澄清):我的查询返回了一个表(多维数组?),如下所示...

ttsUID     ttName     playoffseed
1993       Buffalo    1
1993       Miami      2
1993       Detroit    3
1993       Denver     4
1993       Chicago    5
...and so on.  10 rows total

After calling that query I'd like to set variables ($seedx) such as this... 调用该查询后,我想设置诸如此类的变量($ seedx)...

$seed1 = (1993, Buffalo, 1)
$seed2 = (1993, Miami, 2)
$seed3 = (1993, Detroit, 3)
...and so on until $seed10

So each $seedx variable is an array which is pulled from the multidimensional array. 因此,每个$ seedx变量都是一个从多维数组中拉出的数组。 That's pretty much it. 就是这样。

Alternatively I could make 10 separate querys to the database and increment 'AND playoffseed = 1' each time such as this... 或者,我可以对数据库进行10个单独的查询,并每次这样都增加“ AND playoffseed = 1” ...

$seed1 = mysqli_query($con, "SELECT playoffseed, ttName, ttsUID
                            FROM standings
                            WHERE ttsUID = $season_view
                            AND playoffseed = 1");

$seed2 = mysqli_query($con, "SELECT playoffseed, ttName, ttsUID
                            FROM standings
                            WHERE ttsUID = $season_view
                            AND playoffseed = 2");

but I didn't think querying the database 10 times was the way to go. 但是我不认为要查询数据库10次才行。

In the end I was hoping to get 10 variables ($seed1, $seed2,...$seedx) which would each be an array that includes (ttsUID, ttName, playoffseed). 最后,我希望获得10个变量($ seed1,$ seed2,... $ seedx),每个变量都是一个包含(ttsUID,ttName,playoffseed)的数组。

still i am not sure how you want to store the data,but i hope this helps you 我仍然不确定您要如何存储数据,但是我希望这对您有帮助

$playoffseedings = mysqli_query($con, "SELECT playoffseed, ttName, ttsUID
                                FROM standings
                                WHERE ttsUID = $season_view
                                AND playoffseed > 0
                                ORDER BY playoffseed
                                LIMIT 10");

while($row = mysqli_fetch_assoc($playoffseedings))
{
 $seed[$row['ttName']] = $row['ttsUID'];
 $seed[$row['playoffseed']] = $row['ttsUID'];
}

this will have data like below 这将具有如下数据

(for example) (例如)

$seed['ttname123']= ttsuid123
$seed['playoffseed123']= ttsuid123
$seed['ttname456']= ttsuid456
$seed['playoffseed456']= ttsuid456
........
......

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

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