Jump to content


Photo

insert array data into mysql table?


  • Please log in to reply
5 replies to this topic

#1 php_b34st

php_b34st
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts

Posted 26 August 2006 - 05:36 PM

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?

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

echo '<pre>', print_r($score, true), '</pre>';

?>

would it be best to put it into one table or have a seperarte table for each category?

#2 php_b34st

php_b34st
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts

Posted 27 August 2006 - 12:39 AM

at the moment the data is printed like:

Overall
1
2178
709,946,285

Attack
21
99
55,837,963

Defence
26
99
35,163,394

Strength
45
99
30,161,122

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?

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 27 August 2006 - 09:05 AM

Undoubtedly, one table. Well 2 actually

[pre]
Stats              Category
--------          ---------
userID      +---  categoryID
categoryID --+    category
statA
statB
statC
[/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]
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 php_b34st

php_b34st
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts

Posted 27 August 2006 - 12:49 PM

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?

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 27 August 2006 - 06:53 PM

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
$data = explode (' ', $txt);
$key = 'header';
to
$data = explode ("\n", $txt);
$key = '';

At end of code , add
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]');
    }
}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 php_b34st

php_b34st
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts

Posted 28 August 2006 - 12:48 AM

Thank ypu that worked great, now i shpuld be able to finish the script




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users