Home > PHP + MySQL, Programming > How to parse lotto numbers from liczby.pl and put it to DB?

How to parse lotto numbers from liczby.pl and put it to DB?

Answers is:


<?php
$hostname = "localhost";
$username = "lotto";
$password = "";

try {
$db = new PDO("mysql:host=$hostname;dbname=lotto", $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);

$n = 1;
while ($n <= 126) {
$html = file_get_contents("http://www.liczby.pl/index.php?d=l&dl_d=wyniki_duzego_lotka&dl_w=plansza&w_dl_strona=$n");
preg_match_all("#(<td nowrap>(.*?)</td>)|(<td align=\"center\"  class=\"(.*?)\" title=\"(.*?)\">(.*?)</td>)#", $html, $match);
$i=0;

$match2 = array_mesh($match[2], $match[6]);

foreach ($match2 as $record){
if($i==7 or $i==0){
$time = strtotime($record);
$db->query("INSERT INTO losowania (id, data) VALUES ('', $time)");
$id = $db->lastInsertId();
if($i==7){
$i=0;
}
}
else {
$db->query("INSERT INTO liczby (id, losowania_id, liczby) VALUES ('', $id, $record)");
}
$i++;
}
$n++;
}
$db = null; // close the database connection
}
catch(PDOException $e) {
echo $e->getMessage();
}

function array_mesh() {
// Combine multiple associative arrays and sum the values for any common keys
// The function can accept any number of arrays as arguments
// The values must be numeric or the summed value will be 0

// Get the number of arguments being passed
$numargs = func_num_args();

// Save the arguments to an array
$arg_list = func_get_args();

// Create an array to hold the combined data
$out = array();

// Loop through each of the arguments
for ($i = 0; $i < $numargs; $i++) {
$in = $arg_list[$i]; // This will be equal to each array passed as an argument

// Loop through each of the arrays passed as arguments
foreach($in as $key => $value) {
// If the same key exists in the $out array
if(array_key_exists($key, $out)) {
// Sum the values of the common key
$sum = $in[$key].$out[$key];
// Add the key => value pair to array $out
$out[$key] = $sum;
}else{
// Add to $out any key => value pairs in the $in array that did not have a match in $out
$out[$key] = $in[$key];
}
}
}

return $out;
}
?>

Categories: PHP + MySQL, Programming
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: