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
https://forums.phpfreaks.com/topic/18735-insert-array-data-into-mysql-table/
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?
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]
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]

Archived

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

×
×
  • 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.