Jump to content

Archived

This topic is now archived and is closed to further replies.

php_b34st

insert array data into mysql table?

Recommended Posts

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 results

echo '<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?

Share this post


Link to post
Share on other sites
at the moment the data is printed like:

[code]Overall
1
2178
709,946,285

Attack
21
99
55,837,963

Defence
26
99
35,163,394

Strength
45
99
30,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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.