Jump to content

insert array data into mysql table?


php_b34st

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.