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 2046
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.';
-
Dormilich 8,658
Recognized Expert Moderator Expert
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 click a save button to
UPDATE the SQL table. I'd like to use stored procedures if possible. How is
this done? Where do I start?
thanks
|
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 selection and update takes a
long time.
I am tasked with writing a pl/sql proc that utilizes array processing
to update the column.
|
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 wasteful and redundant
to me.
Here is a sample of what I'm talking about ($db is a PDO already
defined and created).
|
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 must be UPDATED, if not, they
must be INSERTED.
Logically then, I would like to SELECT * FROM <TABLE>
WHERE ....<Values entered here>, and then IF FOUND
UPDATE <TABLE> SET .... <Values entered here> ELSE
INSERT INTO <TABLE> VALUES <Values...
|
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 in the
application, but I'd rather not!
DDL for table and trigger below.
TIA
| |
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 web browser to view a highest
bid and can make a bid.
My question is; how can I be sure that when a user submits a bid, that
another user isn't also currently submittimg a bid, i.e i can tell user 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 in a hidden field)
I wish the user to be able to edit the data in the table, so I have
extracted the records into hiddenfield, textareas, dropdown list and
checkbox so that they can make changes.
I named these elements as arrays and wish to run an...
|
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 certain that MyVar will be
set to the key-field value that was created
when the Append query ran. Now, there are
other ways to do it - I know - that will ensure
you 'nab' the correct record. But I was wondering
|
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 using a code
loop and an INSERT INTO query. About 800,000 records of raw text.
Later, I can then loop through and parse these 800,000 strings into
usable data using more code.
The problem I have is that the conversion of the text file, using a...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |