Created
November 21, 2016 09:39
-
-
Save desmayer/8769c32a9ac5064103e6c9c24aa93ba7 to your computer and use it in GitHub Desktop.
PHP to scrap the time data from HLTB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function hltb($hltbID) { | |
// Function use || to extract the time data from howlongtobeat based upon the game ID stored in the data base | |
// 1.Get Data based upon hltbID value | |
$html = file_get_contents("http://howlongtobeat.com/game.php?id=" . $hltbID); | |
// howlongtobeat does not name the div that contains the actual time so I will go one div above and then sort through the data | |
preg_match_all( | |
'/<div>(.*?)<\/div>/s', | |
$html, //scrapped file | |
$posts, | |
PREG_SET_ORDER // formats data into an array of posts | |
); | |
// 2. Sort through array to find values needed & print | |
echo "<br>Main Story " . $posts[0][1]; | |
echo "<br>Main + Extras " . $posts[1][1]; | |
echo "<br>Completionist " . $posts[2][1]; | |
echo "<br>Combined " . $posts[3][1]; | |
}; | |
include 'conn.php'; | |
// Pull gameID from URL | |
$newID = $_GET["id"]; | |
$sql = "SELECT * FROM psnow WHERE gameID=".$newID; | |
$result = mysql_query($sql) or die(mysql_error()); | |
//Pull the howlongtobeat ID, gameName & gameCount from database | |
If (mysql_num_rows($result) > 0) { | |
while ($row = mysql_fetch_array($result)) { | |
$count = $row['gameCount']+1; // When clicked to view HLTB +1 to count (future use) | |
$gameID = $row['gameID']; | |
echo "<p><b>". $row['gameName'] . "</b>"; //Print Game Name | |
hltb($row['gameHLTB']); //Pass the howlongtobeat game ID into the function | |
//Update the gameCount | |
$update = "UPDATE psnow SET gameCount=" . $count . " WHERE gameID=".$gameID; | |
$result2=mysql_query($update); | |
}//end while | |
}; //end if | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment