Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
what is this $seed$num_rows ?? –  Feroz Akbar May 29 at 16:32
    
sorry, more of a theory and the main question I have. How do I get a variable $seed1, $seed2...? I assume it has something to do with combining a variable ($seed) with $num_rows. –  user3574005 May 29 at 16:42
    
how you want the output look like? –  Feroz Akbar May 29 at 16:51
    
my output. well, i'm going to use it later my html. I'd like to be able to 'echo $seed5['ttName']." #".$seed5['playoffseed']." seed";'. Each $seedx will be an array with ttName, playoffseed, and ttsUID in it. –  user3574005 May 29 at 18:47

1 Answer 1

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
........
......
share|improve this answer
    
not quite. I edited bottom of original in hopes of clarying. –  user3574005 May 30 at 11:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.