php_b34st Posted August 26, 2006 Share Posted August 26, 2006 Hi I have the following code which gets information form a website, stores it into an array and then prints it, instead of printing it i would like to put it into a database. what would be the best way to do this?[code]<?php$user = 'zezima';function RSstats($url){$rssstring = file_get_contents($url);preg_match_all('#<table cellpadding="3" cellspacing="0" border=0>(.*?)</table>#s', $rssstring, $stat); $score = $stat[1][0];$score = strip_tags($score);return $score;}$txt = RSstats('http://hiscore.runescape.com/lang/en/aff/runescape/hiscorepersonal.ws?user1=' . $user);$cats = array('Overall', 'Attack', 'Defence', 'Strength', 'Hitpoints', 'Ranged', 'Prayer', 'Magic', 'Cooking', 'Woodcutting', 'Fletching', 'Fishing', 'Firemaking', 'Crafting', 'Smithing', 'Mining', 'Herblore', 'Agility', 'Thieving', 'Slayer', 'Farming', 'Runecraft', 'Construction');$score = array(); $data = explode (' ', $txt);$key = 'header';foreach ($data as $word) { if (in_array($word, $cats)) { $score[$word] = array(); $key = $word; } elseif ($word=='Not') { $score[$key][] = 'Not ranked'; } elseif ($word=='Ranked') { continue; } else $score[$key][] = $word;}// view the resultsecho '<pre>', print_r($score, true), '</pre>';?>[/code]would it be best to put it into one table or have a seperarte table for each category? Quote Link to comment Share on other sites More sharing options...
php_b34st Posted August 27, 2006 Author Share Posted August 27, 2006 at the moment the data is printed like:[code]Overall12178709,946,285Attack219955,837,963Defence269935,163,394Strength459930,161,122[/code]I was wanting the data putting into the database in the same way. for example the 3 categorys i have showed above should be in 3 different tables with the relevant stats displayed in each table. Can anyone assist me? Quote Link to comment Share on other sites More sharing options...
Barand Posted August 27, 2006 Share Posted August 27, 2006 Undoubtedly, one table. Well 2 actually[pre]Stats Category-------- ---------userID +--- categoryIDcategoryID --+ categorystatAstatBstatC[/pre]So the table data for the examples you gave would look like this[pre]User |Cat| A | B | C | CatID | Category |--------|---|----|------|-------------| ------|--------------|zezima | 1 | 1 | 2178 | 709,946,285 | 1 | Overall |zezima | 2 | 21 | 99 | 55,837,963 | 2 | Attack |zezima | 3 | 26 | 99 | 35,163,394 | 3 | Defence |zezima | 4 | 45 | 99 | 30,161,122 | 3 | Strength | etc | etc etc |[/pre] Quote Link to comment Share on other sites More sharing options...
php_b34st Posted August 27, 2006 Author Share Posted August 27, 2006 Thanks for the help but i was also wondering how to actually insert the data into mysql rather than print it out like i am doing at the moment? Quote Link to comment Share on other sites More sharing options...
Barand Posted August 27, 2006 Share Posted August 27, 2006 To make it easier, and ignoring the category table and writing the cat descriptions into the table for now (code above needs changing pick up cat id instead)change[code]$data = explode (' ', $txt);$key = 'header';[/code]to[code]$data = explode ("\n", $txt);$key = '';[/code]At end of code , add[code]foreach (score as $cat => $stats) { if ($cat) { $sql = "INSERT into stats (userID, cat, statA, statB, statC) VALUES ('$user', '$cat', '$stats[0]', '$stats[1]', '$stats[2]'); }}[/code] Quote Link to comment Share on other sites More sharing options...
php_b34st Posted August 28, 2006 Author Share Posted August 28, 2006 Thank ypu that worked great, now i shpuld be able to finish the script Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.