Personally, if I were to do this, I would create 1 table and put all the information in it. I would structure it as so:
CREATE TABLE `nascar_standings` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`race_week` INT NOT NULL,
`rank` INT NOT NULL,
`rank_delta` VARCHAR(5),
`driver` TEXT NOT NULL,
`points` INT NOT NULL,
`behind` VARCHAR(20),
`start` INT,
`poles` INT,
`wins` INT,
`top5` INT,
`top10` INT,
`dnf` INT,
`winnings` VARCHAR(15),
`in_contention` INT DEFAULT '0',
UNIQUE (`race_week`)
) ENGINE=InnoDB COMMENT="Nascar Standings from Yahoo!";
Now, for the race_week column, I would do an extra scrape to get the exact race week we were getting the standings for. Then when I go to update, I'd first check if race_week is already in the database, and if so, then skip it (since the standings shouldn't change without a race); otherwise, insert it.
You're correct in that it would have to be done in a foreach loop; however, given my array of users, I might modify the code just a bit and create an array more along the lines of:
$drivers = array(
'contention' => array(),
'field' => array()
);
// Add the drivers in contention
foreach($matches as $match)
{
$drivers[$match[1]] = array(
'rank' => $match[1],
'rank_delta' => $match[2],
'driver' => $match[3],
'points' => $match[4],
'behind' => $match[5],
'start' => $match[6],
'poles' => $match[7],
'wins' => $match[8],
'top5' => $match[9],
'top10' => $match[10],
'dnf' => $match[11],
'winnings' => $match[12],
'in_contention' => 1
);
}
foreach($matches2 as $match)
{
$drivers[$match[1]] = array(
'rank' => $match[1],
'rank_delta' => $match[2],
'driver' => $match[3],
'points' => $match[4],
'behind' => $match[5],
'start' => $match[6],
'poles' => $match[7],
'wins' => $match[8],
'top5' => $match[9],
'top10' => $match[10],
'dnf' => $match[11],
'winnings' => $match[12],
'in_contention' => 0
);
}
Giving me an array like:
Array(
[1] => Array
(
[rank] => 1
[rank_delta] => -
[driver] => Tony Stewart
[points] => 3383
[behind] => Leader
[start] => 22
[poles] => 4
[wins] => 3
[top5] => 13
[top10] => 18
[dnf] => 0
[winnings] => $5,084,829
[in_contention] => 1
)
[2] => Array
(
[rank] => 2
[rank_delta] => -
[driver] => Jimmie Johnson
[points] => 3123
[behind] => 260
[start] => 22
[poles] => 1
[wins] => 3
[top5] => 9
[top10] => 14
[dnf] => 1
[winnings] => $4,607,420
[in_contention] => 1
)
....
[13] => Array
(
[rank] => 13
[rank_delta] => -
[driver] => Kyle Busch
[points] => 2627
[behind] => 756
[start] => 22
[poles] => 1
[wins] => 3
[top5] => 5
[top10] => 7
[dnf] => 2
[winnings] => $4,044,855
[in_contention] => 0
)
[14] => Array
(
[rank] => 14
[rank_delta] => -
[driver] => Brian Vickers
[points] => 2589
[behind] => 794
[start] => 22
[poles] => 5
[wins] => 0
[top5] => 3
[top10] => 10
[dnf] => 3
[winnings] => $3,070,173
[in_contention] => 0
)
)
To which I would then do:
foreach($drivers as $driver)
{
$query = sprintf("INSERT INTO `nascar_standings`
(`race_week`, `rank`, `rank_delta`, `driver`, `points`, `behind`, `start`, `poles`, `wins`, `top5`, `top10`, `dnf`, `winnings`, `in_contention`)
VALUES (%d, %d, '%s', %d, '%s', %d, %d, %d, %d, %d, '%s', %d)",
intval($race_week),
intval($driver['rank']),
mysql_real_escape_string($driver['driver']),
intval($driver['points']),
mysql_real_escape_string($driver['behind']),
intval($driver['start']),
intval($driver['poles']),
intval($driver['wins']),
intval($driver['top5']),
intval($driver['top10']),
intval($driver['dnf']),
mysql_real_escape_string($driver['winnings']),
intval($driver['in_contention'])
);
$result = mysql_query($query);
if(!$result)
{
die('Unable to make the full update!<br />'.mysql_error());
}
}
echo 'Completed update.';
Hope that helps.
FYI: $race_week would be scraped out of the info we have already received from Yahoo! Sports.