2

So I am trying to grab something from another website via PHP and put the content into MYSQL. I get the data fine, but it's all out of order and can't think of a way to plug it into MYSQL.

Heres my code to get the data:

<?php
$i = 0;
$homepage = file_get_contents('http://linesfeed.com/nfl');
preg_match_all(
    "'<td style=\'width:auto;color:#424241\' align=\'center\' valign=\'top\'>([^<]*)</td>'si",
    $homepage, $match
);
foreach ($match[1] as $val) {
    $i++;
    $bracktextnumber = '<text' . $i . '>';
    $bracktextnumberclose = '</text' . $i . '>';
    echo $bracktextnumber . $val . $bracktextnumberclose . '<br>';
}
echo "End";
?>

Which outputs something like:

09.05.13
Ravens (Baltimore)
8.0 (-110)
+290
48.5o (-110)
8:35 PM
Broncos (Denver)
-8.0 (-110)
-350
48.5u (-110)
09.08.13
Patriots (NewEngland)
-11.5 (-110)
-630
48.5o (-110)
1:05 PM
Bills (Buffalo)
11.5 (-110)
+465
48.5u (-110)

So I want text1 (09.05.13) and text11 (09.08.13) to be entered as their own records as date in MYSQL. Text 2 (Ravens) and Text 6 (Broncos) to be added to the same entry as text 1, the Date. Text 12 and text 17 teams are added to the Text 11 dates record And so on.

Several things. The number of games loaded from this URL will vary, but the position will always remain the same. So like text 1 will always be a date, text 2 is always the first team...

Any help will be greatly appreciated! Specially if sample code is provided :)

Alexander Yancharuk
  • 13,817
  • 5
  • 55
  • 55
drewg23
  • 391
  • 3
  • 10
  • `INSERT INTO table VALUES ()` – u_mulder Aug 30 '13 at 17:12
  • 4
    YOu shouldn't be using regexes on html. Down that path lies [madness and chaos](http://stackoverflow.com/a/1732454/118068). – Marc B Aug 30 '13 at 17:14
  • Lol I know how to INSERT into table. I don't know how I should organize this gathered data so that I can insert it – drewg23 Aug 30 '13 at 17:48
  • You organize it to match your table layout(s). If you're asking how to design your database, that depends on what you want to do and how you're going to be using the data. – keithhatfield Aug 30 '13 at 18:21

1 Answers1

0

Marc B is correct. But, sometimes you just need a little brute force for the moment. Maybe Marc B can provide a true html parsed answer.

$homepage = file_get_contents('http://linesfeed.com/nfl');
preg_match_all("'(<tr)|<td style=\'width:auto;color:#424241\' align=\'center\' valign=\'top\'>([^<]*)</td>'si", $homepage, $match);
$games = trim( implode('|',$match[2]), "| ");
$games = explode('||', str_replace( '||', '|', $games ));
foreach($games as $key => $game)
{
    $gamedata = explode('|', $game );
    $date  = $gamedatas[0];
    $visit = $gamedata[1];
    $home  = $gamedata[6];
//  insert into games (date,visiting_team,home_team) values( $date, $visit, $home ) ...
}

Results for $games

Array
(
    [0] => 09.05.13|Ravens (Baltimore)|8.0 (-110) |+290|48.5o (-110)|8:35 PM|Broncos (Denver)|-8.0 (-110) |-350|48.5u (-110)
    [1] => 09.08.13|Patriots (NewEngland)|-11.5 (-110) |-630|48.5o (-110)|1:05 PM|Bills (Buffalo)|11.5 (-110) |+465|48.5u (-110)
    [2] => 09.08.13|Titans (Tennessee)|7.0 (-110) |+250|42.0o (-110)|1:05 PM|Steelers (Pittsburgh)|-7.0 (-110) |-300|42.0u (-110)
    [3] => 09.08.13|Falcons (Atlanta)|3.0 (even) |+140|54.5o (-110)|1:05 PM|Saints (NewOrleans)|-3.0 (-120) |-160|54.5u (-110)
    [4] => 09.08.13|Buccaneers (TampaBay)|-3.5 (-110) |-180|40.0o (-110)|1:05 PM|Jets (NewYork)|3.5 (-110) |+160|40.0u (-110)
    [5] => 09.08.13|Chiefs (KansasCity)|-4.0 (-110) |-190|41.0o (-110)|1:05 PM|Jaguars (Jacksonville)|4.0 (-110) |+165|41.0u (-110)
    [6] => 09.08.13|Bengals (Cincinnati)|3.0 (-120) |+130|41.5o (-110)|1:05 PM|Bears (Chicago)|-3.0 (even) |-150|41.5u (-110)
    [7] => 09.08.13|Dolphins (Miami)|0.0 (-110) |OFF|41.0o (-110)|1:05 PM|Browns (Cleveland)|0.0 (-110) |OFF|41.0u (-110)
    [8] => 09.08.13|Seahawks (Seattle)|-3.5 (-115) |-170|45.0o (-110)|1:05 PM|Panthers (Carolina)|3.5 (-105) |+150|45.0u (-110)
    [9] => 09.08.13|Vikings (Minnesota)|5.0 (-110) |+175|46.5o (-110)|1:05 PM|Lions (Detroit)|-5.0 (-110) |-210|46.5u (-110)
    [10] => 09.08.13|Raiders (Oakland)|10.0 (-115) |+350|47.0o (-110)|1:05 PM|Colts (Indianapolis)|-10.0 (-105) |-450|47.0u (-110)
    [11] => 09.08.13|Cardinals (Arizona)|4.5 (-110) |+180|40.0o (-110)|4:30 PM|Rams (StLouis)|-4.5 (-110) |-220|40.0u (-110)
    [12] => 09.08.13|Packers (GreenBay)|4.5 (-110) |+180|48.5o (-110)|4:30 PM|49ers (SanFrancisco)|-4.5 (-110) |-220|48.5u (-110)
    [13] => 09.08.13|Giants (NewYork)|3.0 (-105) |+145|48.5o (-110)|8:35 PM|Cowboys (Dallas)|-3.0 (-115) |-165|48.5u (-110)
    [14] => 09.09.13|Eagles (Philadelphia)|3.5 (-110) |+165|51.0o (-110)|7:15 PM|Redskins (Washington)|-3.5 (-110) |-190|51.0u (-110)
    [15] => 09.09.13|Texans (Houston)|-4.0 (-110) |-200|44.0o (-110)|10:25 PM|Chargers (SanDiego)|4.0 (-110) |+170|44.0u (-110)
)

Results for database inserts

Date        Visiting Team           Home Team
09.05.13    Ravens (Baltimore)      Broncos (Denver)
09.08.13    Patriots (NewEngland)   Bills (Buffalo)
09.08.13    Titans (Tennessee)      Steelers (Pittsburgh)
09.08.13    Falcons (Atlanta)       Saints (NewOrleans)
09.08.13    Buccaneers (TampaBay)   Jets (NewYork)
09.08.13    Chiefs (KansasCity)     Jaguars (Jacksonville)
09.08.13    Bengals (Cincinnati)    Bears (Chicago)
09.08.13    Dolphins (Miami)        Browns (Cleveland)
09.08.13    Seahawks (Seattle)      Panthers (Carolina)
09.08.13    Vikings (Minnesota)     Lions (Detroit)
09.08.13    Raiders (Oakland)       Colts (Indianapolis)
09.08.13    Cardinals (Arizona)     Rams (StLouis)
09.08.13    Packers (GreenBay)      49ers (SanFrancisco)
09.08.13    Giants (NewYork)        Cowboys (Dallas)
09.09.13    Eagles (Philadelphia)   Redskins (Washington)
09.09.13    Texans (Houston)        Chargers (SanDiego)
gwc
  • 1,273
  • 7
  • 12
  • Thank you so much!! What if I dont know how many games there are though? I cant just say $date = $vals[0]; $team1 = $vals[1]; $team2 = $vals[6]; $date2 = $vals[10]; $team3 = $vals[11]; $team4 = $vals[16]; is there a way I can $date = $vals[0+10]; and then also if $vals[0+10] DNE then break; – drewg23 Aug 30 '13 at 19:13
  • I edited the answer to (hopefully) make it a little more understandable. You don't need to know how many games. Each element in the $games array represents a single game. The foreach loop processes each individual game independently. $date2 comes from $vals[0] (now $gamedata[0]) on the second iterration of the foreach loop. All of the first game's data is in $games[0] and the second game's data is in $games[1], etc. – gwc Aug 30 '13 at 19:48
  • So I am getting this error http://drewgarcia23.3owl.com/sports/extract_remote/gettest4.php From this code: http://drewgarcia23.3owl.com/sports/whatitried.txt – drewg23 Aug 30 '13 at 20:39
  • Ok. I'm confused. What error are you getting? Never mind. You hadn't completed the comment. :) You need to put single quotes around the text variables in your SQL: $strSQL = "insert into games (id,date,sport,team1,team2) values('','$date','NFL', '$visit', '$home' )"; – gwc Aug 30 '13 at 20:40
  • MySQL server version for the right syntax to use near '.13,'',Ravens (Baltimore),Broncos (Denver))' at line 1. Also I echoed out $date and $visit, and $home. only one record showed up. Maybe im confused, but didn't you say that should grab all dates and games? – drewg23 Aug 30 '13 at 20:44