473,811 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

insert/update arrays into db for later retrieval to table

3 New Member
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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $url = 'http://sports.yahoo.com/nascar/standings';
  3. $html = file_get_contents($url);
  4.  
  5. // First, get that one single table.
  6. preg_match('~(<table width="100%" border="0" cellspacing="0" cellpadding="0">.*<tr.*class="ysptblthbody1".*>.*</table>)~iUs', $html, $matches);
  7.  
  8. // $matches[0] is now the full match (e.g. the whole $html content)
  9. // $matches[1] is just the table we need
  10.  
  11. // We'll split it up between those in the contention and those not
  12. preg_match('~(<tr class="ysprow(?:1|2)">.*)<tr><td.*><img.*></td></tr>(<tr class="ysprow(?:1|2)".*>.*</tr>.*)</table>~iUs', $matches[1], $parts);
  13.  
  14. // $parts[1] are those in contention
  15. // $parts[2] are the other drivers in the field
  16.  
  17. // Look for all the information for those in contention, save to $matches
  18. 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);
  19.  
  20. // Look for all the information for the other drivers, store in $matches2
  21. 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);
  22.  
  23. // Prepare our array for population
  24. $drivers = array(
  25.     'contention' => array(),
  26.     'field' => array()
  27. );
  28.  
  29. // Add the drivers in contention
  30. foreach($matches as $match)
  31. {
  32.     $drivers['contention'][$match[1]] = array(
  33.         'rank' => $match[1],
  34.         'rank_delta' => $match[2],
  35.         'driver' => $match[3],
  36.         'points' => $match[4],
  37.         'behind' => $match[5],
  38.         'start' => $match[6],
  39.         'poles' => $match[7],
  40.         'wins' => $match[8],
  41.         'top5' => $match[9],
  42.         'top10' => $match[10],
  43.         'dnf' => $match[11],
  44.         'winnings' => $match[12]
  45.     );
  46. }
  47.  
  48. // Add those drivers that are in the rest of the field
  49. foreach($matches2 as $match)
  50. {
  51.     $drivers['field'][$match[1]] = array(
  52.         'rank' => $match[1],
  53.         'rank_delta' => $match[2],
  54.         'driver' => $match[3],
  55.         'points' => $match[4],
  56.         'behind' => $match[5],
  57.         'start' => $match[6],
  58.         'poles' => $match[7],
  59.         'wins' => $match[8],
  60.         'top5' => $match[9],
  61.         'top10' => $match[10],
  62.         'dnf' => $match[11],
  63.         'winnings' => $match[12]
  64.     );
  65. }
  66.  
  67. echo '<pre>'.print_r($drivers, 1).'</pre>'; 



this prints out arrays (approx 65) like the following

Expand|Select|Wrap|Line Numbers
  1. Array
  2. (
  3.     [contention] => Array
  4.         (
  5.             [1] => Array
  6.                 (
  7.                     [rank] => 1
  8.                     [rank_delta] => -
  9.                     [driver] => Tony Stewart
  10.                     [points] => 3383
  11.                     [behind] => Leader
  12.                     [start] => 22
  13.                     [poles] => 4
  14.                     [wins] => 3
  15.                     [top5] => 13
  16.                     [top10] => 18
  17.                     [dnf] => 0
  18.                     [winnings] => $5,084,829
  19.                 )
  20.  
  21.             [2] => Array
  22.                 (
  23.                     [rank] => 2
  24.                     [rank_delta] => -
  25.                     [driver] => Jimmie Johnson
  26.                     [points] => 3123
  27.                     [behind] => 260
  28.                     [start] => 22
  29.                     [poles] => 1
  30.                     [wins] => 3
  31.                     [top5] => 9
  32.                     [top10] => 14
  33.                     [dnf] => 1
  34.                     [winnings] => $4,607,420
  35.                 )
  36.  
  37.                 ....
  38.  
  39.     [field] => Array
  40.         (
  41.             [13] => Array
  42.                 (
  43.                     [rank] => 13
  44.                     [rank_delta] => -
  45.                     [driver] => Kyle Busch
  46.                     [points] => 2627
  47.                     [behind] => 756
  48.                     [start] => 22
  49.                     [poles] => 1
  50.                     [wins] => 3
  51.                     [top5] => 5
  52.                     [top10] => 7
  53.                     [dnf] => 2
  54.                     [winnings] => $4,044,855
  55.                 )
  56.  
  57.             [14] => Array
  58.                 (
  59.                     [rank] => 14
  60.                     [rank_delta] => -
  61.                     [driver] => Brian Vickers
  62.                     [points] => 2589
  63.                     [behind] => 794
  64.                     [start] => 22
  65.                     [poles] => 5
  66.                     [wins] => 0
  67.                     [top5] => 3
  68.                     [top10] => 10
  69.                     [dnf] => 3
  70.                     [winnings] => $3,070,173
  71.                 )
  72.         )
  73. )

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? )

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `standings` (
  2.    `rank` INT,
  3.    `rank_delta` INT,
  4.    `driver` TEXT,
  5.    `points` INT,
  6.    `behind` INT,
  7.    `start` INT,
  8.    `poles` INT,
  9.    `wins` INT,
  10.    `top5` INT,
  11.    `top10` INT,
  12.    `dnf` INT,
  13.    `winnings` BIGINT
Aug 12 '09 #1
3 2046
planethax
3 New Member
I was hoping the following would do the trick, but it causes a blank page :(

Am I close though?

Expand|Select|Wrap|Line Numbers
  1. // Connect and select database
  2. $link = mysqli_connect("localhost", "user", "password", "dbname") or die('Could not connect');
  3.  
  4. // now, you either update in a loop like so:
  5.  
  6. foreach ($drivers) {
  7.     $result = mysqli_query("UPDATE table SET Build = 'standings' WHERE Area='$drivers'");
  8.     echo "$drivers updated. <";
  9. }
Aug 12 '09 #2
planethax
3 New Member
so far this is what I have,
it prints the array to browser, but does not fill db or give any messages

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $url = 'http://sports.yahoo.com/nascar/standings';
  3. $html = file_get_contents($url);
  4.  
  5. // First, get that one single table.
  6. preg_match('~(<table width="100%" border="0" cellspacing="0" cellpadding="0">.*<tr.*class="ysptblthbody1".*>.*</table>)~iUs', $html, $matches);
  7.  
  8. // $matches[0] is now the full match (e.g. the whole $html content)
  9. // $matches[1] is just the table we need
  10.  
  11. // We'll split it up between those in the contention and those not
  12. preg_match('~(<tr class="ysprow(?:1|2)">.*)<tr><td.*><img.*></td></tr>(<tr class="ysprow(?:1|2)".*>.*</tr>.*)</table>~iUs', $matches[1], $parts);
  13.  
  14. // $parts[1] are those in contention
  15. // $parts[2] are the other drivers in the field
  16.  
  17. // Look for all the information for those in contention, save to $matches
  18. 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);
  19.  
  20. // Look for all the information for the other drivers, store in $matches2
  21. 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);
  22.  
  23. // Prepare our array for population
  24. $drivers = array(); 
  25.  
  26. // Add the drivers in contention
  27. foreach($matches as $match)
  28. {
  29.     $drivers[$match[1]] = array(
  30.         'rank' => $match[1],
  31.         'rank_delta' => $match[2],
  32.         'driver' => $match[3],
  33.         'points' => $match[4],
  34.         'behind' => $match[5],
  35.         'start' => $match[6],
  36.         'poles' => $match[7],
  37.         'wins' => $match[8],
  38.         'top5' => $match[9],
  39.         'top10' => $match[10],
  40.         'dnf' => $match[11],
  41.         'winnings' => $match[12],
  42.         'in_contention' => 1
  43.     );
  44. }
  45.  
  46. foreach($matches2 as $match)
  47. {
  48.     $drivers[$match[1]] = array(
  49.         'rank' => $match[1],
  50.         'rank_delta' => $match[2],
  51.         'driver' => $match[3],
  52.         'points' => $match[4],
  53.         'behind' => $match[5],
  54.         'start' => $match[6],
  55.         'poles' => $match[7],
  56.         'wins' => $match[8],
  57.         'top5' => $match[9],
  58.         'top10' => $match[10],
  59.         'dnf' => $match[11],
  60.         'winnings' => $match[12],
  61.         'in_contention' => 0
  62.     );
  63. echo '<pre>'.print_r($drivers, 1).'</pre>'; 
  64.  
  65. $link = mysqli_connect('localhost', 'ninfouser', '********', 'ninfo');
  66. if (!$link) {
  67.     die('Could not connect: ' . mysql_error());
  68. }
  69. echo 'Connected successfully';
  70.  
  71.  
  72. foreach($drivers as $driver)
  73. {
  74.     $query = sprintf("INSERT INTO `nascar_standings`
  75.                     (`race_week`, `rank`, `rank_delta`, `driver`, `points`, `behind`, `start`, `poles`, `wins`, `top5`, `top10`, `dnf`, `winnings`, `in_contention`)
  76.                     VALUES (%d, %d, '%s', %d, '%s', %d, %d, %d, %d, %d, '%s', %d)",
  77.             intval($race_week),
  78.             intval($driver['rank']),
  79.             mysql_real_escape_string($driver['driver']),
  80.             intval($driver['points']),
  81.             mysql_real_escape_string($driver['behind']),
  82.             intval($driver['start']),
  83.             intval($driver['poles']),
  84.             intval($driver['wins']),
  85.             intval($driver['top5']),
  86.             intval($driver['top10']),
  87.             intval($driver['dnf']),
  88.             mysql_real_escape_string($driver['winnings']),
  89.             intval($driver['in_contention'])
  90. );
  91.     $result = mysql_query($query);
  92.     if(!$result)
  93.     {
  94.         die('Unable to make the full update!<br />'.mysql_error());
  95.     }
  96. }
  97.  
  98. echo 'Completed update.'; 
  99.  
Aug 13 '09 #3
Dormilich
8,658 Recognized Expert Moderator Expert
side note: you can simplify the driver array creation by using array_combine()
Expand|Select|Wrap|Line Numbers
  1. // because the original $match[0] is not used in array creation, 
  2. // the only constant value is used to overwrite it so that there is no offset
  3. $keys = array('in_contention', 'rank', 'rank_delta', …);
  4.  
  5. foreach($matches as $match)
  6. {
  7.    $match[0] = 1; // or whatever value is currently needed
  8.    $drivers[$match[1]] = array_combine($keys, $match);
  9. }
  10.  
Aug 13 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
6142
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
3
10449
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.
10
2426
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).
16
17026
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...
3
3731
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
9
3223
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
16
3504
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...
10
12723
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
8
6507
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...
0
9727
marktang
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...
0
9605
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,...
0
10386
jinu1996
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...
1
10398
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,...
0
10133
tracyyun
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...
0
9204
agi2029
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...
0
6889
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();...
0
5554
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...
1
4339
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

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.