Jump to content

Trying to limit data that comes back from mysql query into array


galvin

Recommended Posts

I have these two tables...

  • schedule (gameid, homeid, awayid, weekno, seasonno)
  • teams (teamid, location, nickname)

This mysql query below gets me schedule info for ALL 32 teams in an array...

$sql = "SELECT
h.nickname AS home,
a.nickname AS away,
h.teamid AS homeid,
a.teamid AS awayid,
s.weekno
FROM schedule s
INNER JOIN teams h ON s.homeid = h.teamid
LEFT JOIN teams a ON s.awayid = a.teamid
WHERE s.seasonno =2014";
$schedule= mysqli_query($connection, $sql);
if (!$schedule) {
die("Database query failed: " . mysqli_error($connection));
} else { 
// Placeholder for data
$data = array();
while($row = mysqli_fetch_assoc($schedule)) {
if ($row['away'] == "") {$row['away']="BYE";}
$data[$row['homeid']][$row['weekno']] = $row['away'];
$data[$row['awayid']][$row['weekno']] = '@ '.$row['home'];
}
}

However, I only want to get info for one specific team, which is stored in the $teamid variable. This should be very easy, right? I have tried multiple things, including this one below (where I added an AND statement of "AND (h.teamid=$teamid OR a.teamid=$teamid)"), but this one still outputs too much...

$sql = "SELECT
h.nickname AS home,
a.nickname AS away,
h.teamid AS homeid,
a.teamid AS awayid,
s.weekno
FROM schedule s
INNER JOIN teams h ON s.homeid = h.teamid
LEFT JOIN teams a ON s.awayid = a.teamid
WHERE s.seasonno =2014
AND (h.teamid=$teamid OR a.teamid=$teamid)";
$schedule= mysqli_query($connection, $sql);
if (!$schedule) {
die("Database query failed: " . mysqli_error($connection));
} else { 
// Placeholder for data
$data = array();
while($row = mysqli_fetch_assoc($schedule)) {
if ($row['away'] == "") {$row['away']="BYE";}
$data[$row['homeid']][$row['weekno']] = $row['away'];
$data[$row['awayid']][$row['weekno']] = '@ '.$row['home'];
}
}

Below is the array that the above outputs. In a nutshell, all I want is that 1st array ([1]) which has, in this example, the Eagles full schedule. It's not giving me too much else and I guess I could live with it and just ignore the other stuff, but I'd rather be as efficient as possible and only get what I need...

Array
(
[1] => Array
(
[1] => Jaguars
[2] => @ Colts
[3] => Redskins
[4] => @ 49ers
[5] => Rams
[6] => Giants
[7] => BYE
[8] => @ Cardinals
[9] => @ Texans
[10] => Panthers
[11] => @ Packers
[12] => Titans
[13] => @ Cowboys
[14] => Seahawks
[15] => Cowboys
[16] => @ Redskins
[17] => @ Giants
)

[27] => Array
(
[1] => @ Eagles
)

[28] => Array
(
[2] => Eagles
)

[4] => Array
(
[3] => @ Eagles
[16] => Eagles
)

[14] => Array
(
[4] => Eagles
)

[15] => Array
(
[5] => @ Eagles
)

[3] => Array
(
[6] => @ Eagles
[17] => Eagles
)

[] => Array
(
[7] => @ Eagles
)

[16] => Array
(
[8] => Eagles
)

[25] => Array
(
[9] => Eagles
)

[11] => Array
(
[10] => @ Eagles
)

[7] => Array
(
[11] => Eagles
)

[26] => Array
(
[12] => @ Eagles
)

[2] => Array
(
[13] => Eagles
[15] => @ Eagles
)

[13] => Array
(
[14] => @ Eagles
)

)
Link to comment
Share on other sites

One suggestion that I would make is to change the WHERE condition. You are doing a left join on table a so there may not be a matching record so use the schedule table ids in the where clause.

I.E. change

AND (h.teamid=$teamid OR a.teamid=$teamid)

to

AND (s.homeid=$teamid OR s.awayid=$teamid)

or you could use this alternative

AND $teamid IN (s.homeid, s.awayid)
Link to comment
Share on other sites

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.