Forum Moderators: coopster

Message Too Old, No Replies

if $variable is there more than one time then.

         

skoff

7:54 pm on Aug 26, 2010 (gmt 0)

10+ Year Member



Hi!
I'll try to be the most clear as i can!
I have two tables call : drivers and teams
In my drivers table i have those fields :
iddrivers,fname,lname,team,drivernum

in my teams table i have those fields:
idteams,team

You should know that my drivers table contains 22 drivers
and my teams table contains 11 teams.

so want i want to do here is to output on a page the name of each teams with the two names of drivers. The problem i'm having is that because I have two drivers for each teams it outputs 22 times instead of 11 times.

I hope you understand and can help me! :)

Demaestro

8:06 pm on Aug 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



skoff,

It should out 22 times, because there are 22 drivers assigned to 11 teams.

What you need to do is group your results on team.

There are a few ways of doing this. The most simple to explain is to have a nested loops instead of just 1.

1) Get all the teams
$teams = "select team from teams"
2) Loop through $teams and get players for each.
..for team in teams
....$players = "select * from players where team = " + team['team']
3) Loop through players and display them.

skoff

8:14 pm on Aug 26, 2010 (gmt 0)

10+ Year Member



could you give me a little bit more code on this i'm not use to looping and all that stuff! thanks a lot for fast answering! :)

Demaestro

8:25 pm on Aug 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can you post what you have? It will be easier to alter yours then write my own.

skoff

8:31 pm on Aug 26, 2010 (gmt 0)

10+ Year Member



There you go!

<?PHP
$db_host = '#*$!';
$db_user = '#*$!';
$db_pwd = '#*$!';

if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
mysql_select_db("f1qr");

$result = mysql_query("SELECT drivers.name,teams.team FROM drivers,teams where driver.team = teams.team") or die(mysql_error());

while ($row = mysql_fetch_array($result))
{
echo "<table width='100%' border='0' cellspacing='0' cellpadding='0'>
<tr>
<td colspan='2'>" . $row["team"] . "</td>
</tr>
<tr>
<td>" . $row["name"] . "</td>
<td>Here would be the second driver name</td>
</tr>
</table>";
}
mysql_close();
?>

Demaestro

9:17 pm on Aug 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You do have a loop, it is a while loop so we will create another loop within that loop.

My PHP is rusty, so you may have to touch up my syntax but basically this is what you want.

$result = mysql_query("SELECT team FROM teams") or die(mysql_error());

while ($row = mysql_fetch_array($result))
{
echo "<table width='100%' border='0' cellspacing='0' cellpadding='0'>
<tr>
<td colspan='2'>" . $row["team"] . "</td></tr><tr>"

$drivers = mysql_query("SELECT * FROM drivers where team = " + $row["team"]) or die(mysql_error());
while ($driver_row = mysql_fetch_array($drivers))
{

echo "<td>" . $driver_row["name"] . "</td>"

}
echo "</tr></table>";
}
mysql_close();

skoff

9:38 pm on Aug 26, 2010 (gmt 0)

10+ Year Member



this is what I get when i tried what you told me to do! Mercedes is one of the team. The code is right until it reach the $drivers=....
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0' at line 1
Mercedes

skoff

9:49 pm on Aug 26, 2010 (gmt 0)

10+ Year Member



I got it working! Just needed to add -> ' before and after " . $row["team"] . " in the WHERE clause of the second query! Thanks a lot! So the code if you are wondering would look like this! Just need to work how the table output because its all mixed up but the essential is now working! Thanks again! :)

<?PHP
$db_host = '#*$!';
$db_user = '#*$!';
$db_pwd = '#*$!';

if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
mysql_select_db("f1qr");

$result = mysql_query("SELECT team FROM teams") or die(mysql_error());

while ($row = mysql_fetch_array($result))
{
echo "<table width='100%' border='0' cellspacing='0' cellpadding='0'>
<tr>
<td colspan='2'>" . $row["team"] . "</td>
</tr><tr>";

$drivers = mysql_query("SELECT * FROM drivers WHERE team = '" . $row["team"] . "'") or die(mysql_error());

while ($driver_row = mysql_fetch_array($drivers))
{
echo "<td>" . $driver_row["name"] . "</td>";
}
echo "</tr></table>";
}
mysql_close();
?>

Demaestro

3:19 pm on Aug 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Nice work, glad it worked out.

There are other ways to work this, but this is one of the easier ways to maintain as your data changes.