Reputation: 3
Been hammering away at this for weeks. I think it's actually simple...but I just can't get it.
I want to pull info from the database and use it in divs later on in my html. I've been successful in other cases looping through and displaying the full results of a query in the past. In this case I just want to echo a specific team name or team ID in my html. All the data gets used...so I'd like to just have one query, but the data gets used in different divs in different places...so being able to echo specific parts of the query is important.
The query I have returns 10 rows and 4 columns.
<?php
$playoffs = mysqli_query($con, "SELECT playoffseed, ttName, teamID, ttsUID
FROM standings
WHERE ttsUID = $season_view
AND playoffseed > 0
ORDER BY playoffseed
LIMIT 10");
?>
Now, I think I've learned that this query returns a result set and not an array. It needs to be processed using some php to turn it into an array. Correct? And given the fact it's got multiple columns and rows once it is processed it would be a multidimensionsal array. Ok I've scoured this site and google...tried many ideas using mysqli_fetch_array, mysqlifetch_assoc, and any other mysqli_fetch that I'd come across.
In my mind anyway, after processing the query with some php I'm thinking the array would look like this...
playoffseed ttName teamID ttsUID
1 Buffalo 13 1993
2 Miami 19 1993
3 Detroit 8 1993
4 Denver 3 1993
5 Chicago 26 1993
...and so on. 10 rows total
After that I'd like to be able to call on (echo) various items from the above in my html. Let's say in one div I'd like to be able to echo the ttName "Detroit" and then to call and image associated with the teamID I'd like to be able to call that too (it'd be something like this in my html...
IMG SRC="../images/<?php echo "teamID" ?>.jpeg
...where in this case Detroits image is "8.jpg").
How can that be done? How do I correctly fetch the array and then echo a specific item of data from it?
I'm thinking if the above is an associated array I'd be able to echo something like this to return "Detroit"...
echo $playoffs[3]['ttName'];
[3] = 3rd row and ['ttName'] = column. no? I realize that [3] would actually refer to the 4th row if it's actually pointing to rows that start with zero and not one, but I'd like to be able to call on the item by using the "playoffseed" identifier, but I'm just too confused at this point.
I already feel I've made this question too confusing in itself. Thanks a ton for your help.
Upvotes: 0
Views: 963
Reputation: 46900
kevinabelita:
this could shed some light to you us2.php.net//manual/en/mysqli-result.fetch-assoc.php
OP:
been there a 100 times. i'm just not getting something. long story short in case the above is too much....take query and process it with php to create an array...then echo....say..."detroit" from that array
And still didn't notice this simple way?
while ($row = mysqli_fetch_assoc($playoffs)) {
$values[]=$row;
echo $row["ttName"];
//print_r($row); // if you want to see all the data this row contains
}
mysqli_free_result($result);
}
Now you can use $values with an index like you wish. i.e.
echo $values[0]["ttName"];
Edit
I mistakenly was trying to execute the query again. Fixed in the code above. Now for a little explanation of what you are confused with. Here's how the flow goes roughly
mysqli_query
in this case)$playoffs
in this case)mysqli_fetch_assoc
in a loop is fetching the rows one by one till all of the result set has been fetched.$row
variable.Upvotes: 2