Jump to content

[SOLVED] Is it possible to loop through two mysql_fetch_array funtions?


Recommended Posts

Hey

Is it possible to loop through two mysql_fetch_array funtions?

In my example i want the while loop to loop through both $fet1 and $fet2, is this somehow possible?

I tried with [i]while ($fet2 = mysql_fetch_array($que2) && $fet2 = mysql_fetch_array($que2)) [/i] but this only result in that it looped through the last variable $fet2.

There most be anohter way?

[code]
<?php
include('config.php');
$sql1 = "SELECT team_id, name, home_id FROM team, game WHERE team_id = home_id";
$sql2 = "SELECT team_id, name, visitor_id FROM team, game WHERE team_id = visitor_id";

$que1 = mysql_query($sql1);
$que2 = mysql_query($sql2);

$fet1 = mysql_fetch_array($que1);

while ($fet2 = mysql_fetch_array($que2)){
echo $fet1['name']." - ".$fet2['name']."<br>";
}

?>
[/code]
[quote author=JJohnsenDK link=topic=120386.msg493681#msg493681 date=1167492963]
I tried with [i]while ($fet2 = mysql_fetch_array($que2) && $fet2 = mysql_fetch_array($que2)) [/i] but this only result in that it looped through the last variable $fet2.[/quote]
Well those two statemetns are EXACTLY the same. Didn't you mean to include [b]$fet1 = mysql_fetch_array($que1)[/b] as one of the operands? The way you have it written I would expect that you would only get every other value from the second result set.

I see no reason that wouldn't work if it was properly written for both result sets. However, what exactly are you trying to do, I think a better approach would be to create a single query to return all the data you need.[/quote]
Try the following query out:
[code]$sql = "SELECT t.team_id, t. name, g.home_id,  g.visitor_id FROM team AS t, game AS g
WHERE t.team_id = g.home_id AND t.team_id = g.visitor_id";[/code]
That should do it. You might want to have a read about SQL Joins. That is what I turned your query in to.
I actually tried that before, but first how do i show the team name(the colum name from table team)? and second how do i show the visitor name and home name seperatly?

[code]
<?php
include('config.php');
$sql = "SELECT t.team_id, t. name, g.home_id,  g.visitor_id FROM team AS t, game AS g
WHERE t.team_id = g.home_id AND t.team_id = g.visitor_id";

$que = mysql_query($sql);

while ($fet = mysql_fetch_array($que)){
echo $fet['t.name']." - ".$fet['t.name']."<br>";
}

?>
[/code]

This doesnt work.
Your while loop is wrong.

What columns hold the team name and visitor name in your teams and games table

name is the team name and you. so you use $fet['name'] to get the team name. (note the t. and g. in the query are aliases - you don't reference them in your code. Just the actual column name).

So what coloumn holds the visitor name?
The database is design in this way:

Game holds only numbers. game_id = the number of the match, home_id = the number of the home team in the team table and visitor = the number of the visitor team in the team table.

For example the first game of the season looks like this:
Game table:
game_id = 1, home_id=3 and visitor_id=1

Team table:
team_id = 1 - name = Silkeborg
team_id = 2 - name = Brøndby
team_id = 3 - name = Fc København

Then the first game of the season should look like this when it is printed to the website:
Match nr. 1  -  Fc København - Silkeborg

I hope you understand my example, else plz do write so.
I think this will get you what you want:
[code]<?php
include('config.php');
$sql = "SELECT h.name as home, v.name AS visitor
          FROM game AS g
          LEFT JOIN team AS h ON g.home_id = h.team_id
          LEFT JOIN team AS v ON g.visitor_id = v.team_id";

$result = mysql_query($sql);

if (mysql_num_rows($result)==0) {
    echo "There were no results";
} else {
    $matchno = 0;
    while ($row = mysql_fetch_array($result)){
             $matchno++;
             echo "Match nr. {$matchno} - {$row['home']} - {$row['visitor']}<br>";
    }
}

>?[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.