简体   繁体   中英

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. 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. (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. 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.

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];' return the result of the second row of the $seedings array? I got a blank.

And why wouldn't 'echo $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']).

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...

$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. 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...

$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.

In the end I was hoping to get 10 variables ($seed1, $seed2,...$seedx) which would each be an array that includes (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
........
......

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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