I have multiple arrays that I need to insert/update database and I am not sure how to start, I think I need to use the "foreach" statement and also not sure whether or not to serialize and unserialize?
this info is then going to be used to populate a table.
This is what I have so far - <?php
-
$url = 'http://sports.yahoo.com/nascar/standings';
-
$html = file_get_contents($url);
-
-
// First, get that one single table.
-
preg_match('~(<table width="100%" border="0" cellspacing="0" cellpadding="0">.*<tr.*class="ysptblthbody1".*>.*</table>)~iUs', $html, $matches);
-
-
// $matches[0] is now the full match (e.g. the whole $html content)
-
// $matches[1] is just the table we need
-
-
// We'll split it up between those in the contention and those not
-
preg_match('~(<tr class="ysprow(?:1|2)">.*)<tr><td.*><img.*></td></tr>(<tr class="ysprow(?:1|2)".*>.*</tr>.*)</table>~iUs', $matches[1], $parts);
-
-
// $parts[1] are those in contention
-
// $parts[2] are the other drivers in the field
-
-
// Look for all the information for those in contention, save to $matches
-
preg_match_all('~<tr class="ysprow.*">.*<td.*>.*([\d]*)</td>.*<td.*>([+|-][\d]*)</td><td class=".*padded2px.*".*>.*<a.*>(.*)</a></td>.*<td class=".*ysptblclbg6.*".*>(.*)</td>.*<td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td>.*</tr>~iUs', $parts[1], $matches, PREG_SET_ORDER);
-
-
// Look for all the information for the other drivers, store in $matches2
-
preg_match_all('~<tr class="ysprow.*">.*<td.*>.*([\d]*)</td>.*<td.*>([+|-][\d]*)</td><td class=".*padded2px.*".*>.*<a.*>(.*)</a></td>.*<td class=".*ysptblclbg6.*".*>(.*)</td>.*<td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td>.*</tr>~iUs', $parts[2], $matches2, PREG_SET_ORDER);
-
-
// Prepare our array for population
-
$drivers = array(
-
'contention' => array(),
-
'field' => array()
-
);
-
-
// Add the drivers in contention
-
foreach($matches as $match)
-
{
-
$drivers['contention'][$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]
-
);
-
}
-
-
// Add those drivers that are in the rest of the field
-
foreach($matches2 as $match)
-
{
-
$drivers['field'][$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]
-
);
-
}
-
-
echo '<pre>'.print_r($drivers, 1).'</pre>';
this prints out arrays (approx 65) like the following - Array
-
(
-
[contention] => 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
-
)
-
-
[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
-
)
-
-
....
-
-
[field] => Array
-
(
-
[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
-
)
-
-
[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
-
)
-
)
-
)
So I need to update my database with this, then later get it to insert into a table on my page?
I have built the database like the following (not sure if it was the best/easiest way? ) - CREATE TABLE `standings` (
-
`rank` INT,
-
`rank_delta` INT,
-
`driver` TEXT,
-
`points` INT,
-
`behind` INT,
-
`start` INT,
-
`poles` INT,
-
`wins` INT,
-
`top5` INT,
-
`top10` INT,
-
`dnf` INT,
-
`winnings` BIGINT
-
)
3 2015
I was hoping the following would do the trick, but it causes a blank page :(
Am I close though? -
// Connect and select database
-
$link = mysqli_connect("localhost", "user", "password", "dbname") or die('Could not connect');
-
-
// now, you either update in a loop like so:
-
-
foreach ($drivers) {
-
$result = mysqli_query("UPDATE table SET Build = 'standings' WHERE Area='$drivers'");
-
echo "$drivers updated. <";
-
}
so far this is what I have,
it prints the array to browser, but does not fill db or give any messages -
<?php
-
$url = 'http://sports.yahoo.com/nascar/standings';
-
$html = file_get_contents($url);
-
-
// First, get that one single table.
-
preg_match('~(<table width="100%" border="0" cellspacing="0" cellpadding="0">.*<tr.*class="ysptblthbody1".*>.*</table>)~iUs', $html, $matches);
-
-
// $matches[0] is now the full match (e.g. the whole $html content)
-
// $matches[1] is just the table we need
-
-
// We'll split it up between those in the contention and those not
-
preg_match('~(<tr class="ysprow(?:1|2)">.*)<tr><td.*><img.*></td></tr>(<tr class="ysprow(?:1|2)".*>.*</tr>.*)</table>~iUs', $matches[1], $parts);
-
-
// $parts[1] are those in contention
-
// $parts[2] are the other drivers in the field
-
-
// Look for all the information for those in contention, save to $matches
-
preg_match_all('~<tr class="ysprow.*">.*<td.*>.*([\d]*)</td>.*<td.*>([+|-][\d]*)</td><td class=".*padded2px.*".*>.*<a.*>(.*)</a></td>.*<td class=".*ysptblclbg6.*".*>(.*)</td>.*<td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td>.*</tr>~iUs', $parts[1], $matches, PREG_SET_ORDER);
-
-
// Look for all the information for the other drivers, store in $matches2
-
preg_match_all('~<tr class="ysprow.*">.*<td.*>.*([\d]*)</td>.*<td.*>([+|-][\d]*)</td><td class=".*padded2px.*".*>.*<a.*>(.*)</a></td>.*<td class=".*ysptblclbg6.*".*>(.*)</td>.*<td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td><td.*>(.*)</td>.*</tr>~iUs', $parts[2], $matches2, PREG_SET_ORDER);
-
-
// Prepare our array for population
-
$drivers = 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
-
);
-
}
-
echo '<pre>'.print_r($drivers, 1).'</pre>';
-
-
$link = mysqli_connect('localhost', 'ninfouser', '********', 'ninfo');
-
if (!$link) {
-
die('Could not connect: ' . mysql_error());
-
}
-
echo 'Connected successfully';
-
-
-
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.';
-
side note: you can simplify the driver array creation by using array_combine() - // because the original $match[0] is not used in array creation,
-
// the only constant value is used to overwrite it so that there is no offset
-
$keys = array('in_contention', 'rank', 'rank_delta', …);
-
-
foreach($matches as $match)
-
{
-
$match[0] = 1; // or whatever value is currently needed
-
$drivers[$match[1]] = array_combine($keys, $match);
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: shank |
last post by:
Visually, the page will look somewhat like a spreadsheet. It could have
hundreds of records (rows) displayed. I want to enable the user to edit any
one or any number of records and any fields, then...
|
by: Mark A Framness |
last post by:
Greetings,
I am working on a project and we need to write a conversion script to
initialize a new field on a table. The number of records on this table
is on the order of millions so routine...
|
by: smorrey |
last post by:
Hello all,
I am writing an app in PHP that uses a PostGres database.
One thing i have noticed is that what should/could be a single line of
SQL code takes about 6 lines of PHP. This seem...
|
by: Philip Boonzaaier |
last post by:
I want to be able to generate SQL statements that will go through a list of
data, effectively row by row, enquire on the database if this exists in the
selected table- If it exists, then the colums...
|
by: teddysnips |
last post by:
I need a trigger (well, I don't *need* one, but it would be optimal!)
but I can't get it to work because it references ntext fields.
Is there any alternative? I could write it in laborious code...
|
by: David Eades |
last post by:
Hi all
Complete newbie here, so apologies if this is the wrong forum.
I've been asked to use mysql and asp to make a simple bidding system (rather
like a simple ebay), whereby users can use a...
|
by: Ian Davies |
last post by:
Hello
Needing help with a suitable solution.
I have extracted records into a table under three columns 'category',
'comment' and share (the category column also holds the index no of the
record...
|
by: MLH |
last post by:
Suppose, in a multi-user environment, you
have append query SQL in a VBA procedure
that looks like INSERT INTO MyTable...
and the next line reads MyVar=DMax("","MyTable...
You can never be...
|
by: SaltyBoat |
last post by:
Needing to import and parse data from a large PDF file into an Access
2002 table: I start by converted the PDF file to a html file. Then
I read this html text file, line by line, into a table...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |