json in database plaatsen

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Louis Deconinck

Louis Deconinck

11/07/2014 19:49:48
Quote Anchor link
Ik heb voglende json file gedecoded: http://hearthstonehero.hol.es/json.php

Hoe kan ik deze via php script in mijn mysql database loaden. Graag wat code waarmee ik aan de slag kan, heb namelijk niet echt een idee waar te beginnen.

Mvg
Louis
 
PHP hulp

PHP hulp

29/03/2024 14:31:07
 
Ger van Steenderen
Tutorial mod

Ger van Steenderen

11/07/2014 21:02:04
Quote Anchor link
Met deze wel zeer beperkte informatie, heb ik ook geen idee.
 
- Ariën  -
Beheerder

- Ariën -

11/07/2014 21:08:07
Quote Anchor link
File_get_contents(), json_decode() en foreach() om het uit te lezen.
 
Louis Deconinck

Louis Deconinck

12/07/2014 13:31:00
Quote Anchor link
Wat meer uitleg, ik heb dus een json file (http://hearthstonehero.hol.es/json.php), deze bevat informatie over alle kaarten die gebruikt worden in het online spel Hearthstone. Nu wil ik graag deze informatie overbrengen in mijn mysqldatabase. Het proces dat ik nu volg: plaats de info van de json file in variabelen -> toon de info op het scherm -> als het een kaart is die ik in de database wil hebben klik ik op 'add', als ik ze er niet wil in hebben op 'skip' -> als ik op 'add' geklikt heb, word alle info uit de variabelen in de mysql database geplaats.

Dit is alle code die ik totnogtoe heb, helaas krijg ik hierbij een wit scherm, dus ook geen error -- problem solved

Nu krijg ik echter wel het scherm te zien, maar wanneer ik op add druk, wordt de info niet in de database geplaatst en als ik op skip druk, gaat hij niet naar de volgende kaart

vb pagina: http://hearthstonehero.hol.es/test.php?set=Basic&nr=1
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php

// error reporting
ini_set('display_errors', 1);
error_reporting(E_ALL);

// database connection
$link=mysqli_connect("***","***","***","***");

// collecting json
$a = file_get_contents('http://hearthstonejson.com/json/AllSets.json');

// decoding json
$obj = json_decode($a, true);

// keys needed to specify a card, come from url
$set = $_GET["set"];
$nr = $_GET["nr"];

// setting all variables a card can have, when they exist
$id = $obj[$set][$nr]['id'];
$name = $obj[$set][$nr]['name'];
if (isset($obj[$set][$nr]['text'])) {
  $text = $obj[$set][$nr]['text'];
}

if (isset($obj[$set][$nr]['type'])) {
  $type = $obj[$set][$nr]['type'];
}

if (isset($obj[$set][$nr]['faction'])) {
  $faction = $obj[$set][$nr]['faction'];
}

if (isset($obj[$set][$nr]['rarity'])) {
  $rarity = $obj[$set][$nr]['rarity'];
}

if (isset($obj[$set][$nr]['cost'])) {
  $cost = $obj[$set][$nr]['cost'];
}

if (isset($obj[$set][$nr]['attack'])) {
  $attack = $obj[$set][$nr]['attack'];
}

if (isset($obj[$set][$nr]['health'])) {
  $health = $obj[$set][$nr]['health'];
}

if (isset($obj[$set][$nr]['flavor'])) {
  $flavor = $obj[$set][$nr]['flavor'];
}

if (isset($obj[$set][$nr]['artist'])) {
  $artist = $obj[$set][$nr]['artist'];
}

if (isset($obj[$set][$nr]['howToGetGold'])) {
  $howToGetGold = $obj[$set][$nr]['howToGetGold'];
}

if (isset($obj[$set][$nr]['inPlayText'])) {
  $inPlayText = $obj[$set][$nr]['inPlayText'];
}

if (isset($obj[$set][$nr]['durability'])) {
  $durability = $obj[$set][$nr]['durability'];
}

if (isset($obj[$set][$nr]['howToGet'])) {
  $howToGet = $obj[$set][$nr]['howToGet'];
}

if (isset($obj[$set][$nr]['playerClass'])) {
  $playerClass = $obj[$set][$nr]['playerClass'];
}

if (isset($obj[$set][$nr]['race'])) {
  $race = $obj[$set][$nr]['race'];
}

if (isset($obj[$set][$nr]['mechanics'])) {
  $mechanics = $obj[$set][$nr]['mechanics'];
}

?>


<!DOCTYPE>
<html>
  <head>
    <!-- link to stylesheet -->
    <link rel='stylesheet' type='text/css' href='style.php' />
  </head>
  <body>
    <div id="content">
      <div id="title">
        <!-- cardname as title -->
        <h1 id="name"><?php echo $name; ?></h1>
      </div>
      <div id="leftcard">
        <!-- getting the cardimage from hearthhead using the unique id -->
        <img id="cardimg"  src="http://wow.zamimg.com/images/hearthstone/cards/enus/original/<?php echo $id; ?>.png" /><br/>
        <?php if (isset($flavor)) { ?>
          <div id="flavor"><i><?php echo $flavor; ?></i></div>
        <?php } ?>
      </div>
      <div id="cardcontent">
        <!-- table with all cardinfo using the variables from the json file -->
        <table id="quickinfo">
          <?php if (isset($text)) { ?>
            <tr>
              <td>Description</td>
              <td><?php echo $text; ?></td>
            </tr>
          <?php } if (isset($playerClass)) { ?>
            <tr>
              <td>Class</td>
              <td><?php echo $playerClass; ?></td>
            </tr>
          <?php } if (isset($type)) { ?>
            <tr>
              <td>Type</td>
              <td><?php echo $type; ?></td>
            </tr>
          <?php } if (isset($faction)) { ?>
            <tr>
              <td>Faction</td>
              <td><?php echo $faction; ?></td>
            </tr>
          <?php } if (isset($race)) { ?>
            <tr>
              <td>Race</td>
              <td><?php echo $race; ?></td>
            </tr>
          <?php } if (isset($set)) { ?>
            <tr>
              <td>Set</td>
              <td><?php echo $set; ?></td>
            </tr>
          <?php } if (isset($rarity)) { ?>
            <tr>
              <td>Rarity</td>
              <td><?php echo $rarity; ?></td>
            </tr>
          <?php } if (isset($cost)) { ?>
            <tr>
              <td>Cost</td>
              <td class="card cost"><?php echo $cost; ?></td>
            </tr>
          <?php } if (isset($attack)) { ?>
            <tr>
              <td>Attack</td>
              <td class="card attack"><?php echo $attack; ?></td>
            </tr>
          <?php } if (isset($attack)) { ?>
            <tr>
              <td>Health</td>
              <td class="card health"><?php echo $health; ?></td>
            </tr>
          <?php } if (isset($durability)) { ?>
            <tr>
              <td>Durability</td>
              <td class="card durability"><?php echo $durability; ?></td>
            </tr>
          <?php } if (isset($mechanics)) { ?>
            <tr>
              <td>Mechanics</td>
              <td><?php echo(implode(', ', $mechanics)); ?></td>
            </tr>
          <?php } if (isset($howToGet)) { ?>
            <tr>
              <td>Card</td>
              <td><?php echo $howToGet; ?></td>
            </tr>
          <?php } if (isset($howToGetGold)) { ?>
            <tr>
              <td>Golden card</td>
              <td><?php echo $howToGetGold; ?></td>
            </tr>
          <?php } if (isset($artist)) { ?>
            <tr>
              <td>Artist</td>
              <td><?php echo $artist; ?></td>
            </tr>
          <?php } if (isset($inPlayText)) { ?>
            <tr>
              <td>Inplay text</td>
              <td><?php echo $inPlayText; ?></td>
            </tr>
          <?php } ?>
          <tr>
            <td>Collectible</td>
            <td><?php if (isset($obj[$set][$nr]['collectible'])) { echo "Yes"; $collectible = 1; } else { echo "No"; $collectible = 0; } ?></td>
          </tr>
          <tr>
            <td>Elite</td>
            <td><?php if (isset($obj[$set][$nr]['elite'])) { echo "Yes"; $elite = 1; } else { echo "No"; $elite = 0; } ?></td>
          </tr>
        </table>
      </div>

      <?php
      // if the 'add'-button was pressed
      if(isset($_POST['add'])) {
        // insert all data into the mysqldatabase
        mysqli_query($link,"INSERT INTO cards (id, name, class, type, faction, race, set, rarity, text, flavor, inPlayText, cost, attack, health, durability, mechanics, howToGet, howToGetGold, artist, collectible, elite) VALUES ('$id', '$name', '$class', '$type', '$faction', '$race', '$set', '$rarity', '$text', '$flavor', '$inPlayText', '$cost', '$attack', '$health', '$durability', '$mechanics', '$howToGet', '$howToGetGold', '$artist', '$collectible', '$elite')");
      }

      // if the 'add' or 'skip'-button was pressed
      if(isset($_POST['add']) or isset($_POST['skip'])) {
        // increase cardnumber with 1, gives us the next card in the json file
        $nr++;
        // cycle through all the cards in the json file, by changing the set and number
        if ($set=='Basic' and $nr==212) {
          $set='Credits';
          $nr=0;
        }

        if ($set=='Credits' and $nr==17) {
          $set='Debug';
          $nr=0;
        }

        if ($set=='Debug' and $nr==39) {
          $set='Expert';
          $nr=0;
        }

        if ($set=='Expert' and $nr==392) {
          $set='Missions';
          $nr=0;
        }

        if ($set=='Mission' and $nr==37) {
          $set='Promotion';
          $nr=0;
        }

        if ($set=='Promotion' and $nr==13) {
          $set='Reward';
          $nr=0;
        }

        if ($set=='Reward' and $nr==2) {
          $set='System';
          $nr=0;
        }
?>


        <!-- go to the next card -->
        <meta http-equiv="refresh" content="0; url=http://hearthstonehero.hol.es/test.php?set=<?php echo $set; ?>&nr=<?php echo $nr; ?>" />

      <?php } ?>

      <!-- two buttons to add a card to the database or skip one -->
      <form id="add" method="post" action="">
        <input type="submit" value="Add">
      </form>
      <form id="skip" method="post" action="">
        <input type="submit" value="Skip">
      </form>
      
    </div>
  </body>
</html>


Toevoeging op 12/07/2014 13:46:51:

Op regel 189 was ik een haakje vergeten, my bad

Toevoeging op 12/07/2014 13:49:33:

Nu krijg ik echter wel het scherm te zien, maar wanneer ik op add druk, wordt de info niet in de database geplaatst en als ik op skip druk, gaat hij niet naar de volgende kaart
Gewijzigd op 12/07/2014 13:49:02 door Louis Deconinck
 
- Ariën  -
Beheerder

- Ariën -

12/07/2014 13:53:38
Quote Anchor link
Als je niks in je database krijgt, zou ik toch eens gaan debuggen.
Kijk eens met een simpele echo of je statement op lijn 189 uitgeveord wordt, en voeg eens goede foutafhandeling toe aan je query.
 
Erwin H

Erwin H

12/07/2014 14:14:08
Quote Anchor link
Logica verkeerd om, hoe vaak komt dat niet voor hier....

Als je eerst de data naar het scherm print en daarna pas gaat kijken welke kaart je eigenlijk wilt tonen, dan krijg je natuurlijk altijd de verkeerde kaart te zien.
 
Louis Deconinck

Louis Deconinck

12/07/2014 14:44:08
Quote Anchor link
De logica klopt hoor, eerst ga je naar bv: http://hearthstonehero.hol.es/test.php?set=Basic&nr=1, als je dan op add klikt increased hij het nummer met 1 en ga je dus naar http://hearthstonehero.hol.es/test.php?set=Basic&nr=2

Het probleem heb ik ondertuseen ook kunnen localiseren, ik had geen name en id gegeven aan de input.

Dus nu werkt alles naar behoren, alleen heb ik nog 1 probleem. Bijna altijd is het zo dat er variabelen niet bestaan. Bijvoorbeeld $inPlayText komt maar voor bij een 20/400 kaarten. Als ik dan $inPlayText wil invoeren lukt dit uiteraard niet en wordt de query dus niet uitgevoerd. Hoe kan ik dat het best oplossen? Met een if (!isset($inPlayText)){ $inPlayText = ''; } of bestaat er nog een betere manier?

Toevoeging op 12/07/2014 14:45:13:

Verder zou ik ook graag mijn php skills wat verbeteren, dus als er stukken code zijn, die beter geschreven kunnen worden, laat maar komen :-)
 
Erwin H

Erwin H

12/07/2014 15:39:19
Quote Anchor link
Volgens mij klopt er anders niets van.

Stap 1: Toon de eerste kaart
url: url?nr=1
Kaart getoond: 1
Geen actie
Link: nr=1

Stap 2: klik op add
url: url?nr=1
Kaart getoond: 1
Kaart 1 toegevoegd
Link: nr=2

Stap 3: He, ik zie nog steeds 1, dis had ik toch net toegevoegd, dan maar skippen:
url: url?nr=2
Kaart getoond: 2
Kaart 2 geskipped
Link: nr=3

Stap 4: ok, ik zie kaart 2, die wil ik toevoegen:
url url?nr=3
Kaart getoond: 3
Kaart 3 toegevoegd
Link: nr=4

Dus nu heb ik kaart 1 en 3 toegevoegd, terwijl ik dacht 1 en 2 te hebben gedaan.... Klopt niets van als je het mij vraagt.
 
Louis Deconinck

Louis Deconinck

12/07/2014 18:48:26
Quote Anchor link
Na wat testen ben ik erachter gekomen dat het probleem in de query zit. Wat is hier fout aan?

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
<?php
$insert1
= mysqli_query($link, "INSERT INTO cards (id, name, class, type, faction, race, set, rarity, text, flavor, inPlayText, cost, attack, health, durability, mechanics, howToGet, howToGetGold, artist, collectible, elite)
VALUES ('$id', '$name', '$playerClass', '$type', '$faction', '$race', '$set', '$rarity', '$text', '$flavor', '$inPlayText', '$cost', '$attack', '$health', '$durability', '$mechanics', '$howToGet', '$howToGetGold', '$artist', '$collectible', '$elite')"
) or die(mysql_error());
?>
 
Ger van Steenderen
Tutorial mod

Ger van Steenderen

12/07/2014 20:58:02
Quote Anchor link
Ik denk dat het met het datatype van de velden te maken heeft.
Als een veld datatype een numeriek datatype heeft kan je dit wel als bv. '100' als waarde in de query geven, maar niet ''.

Als ik in jouw schoenen stond, zou ik ook de gegevens uit de json in één keer de database in schieten.
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
$values
= array();
foreach($obj as $set => $input) {
    foreach($input as $item) {
        // maak een array met daarin default waarden
        $card = array('id' => "'0'",
                      'name' => "''",
                      'text' => "''",
                      'type' => "''",
                      'faction' => "''",
                      'set' => "'" . $set . "'",
                      'rarity' => "''",
                      'cost' => "'0.00'"); // etcetera
        foreach($item as $key => $value) {
            $card[$key] = "'" . mysqli_real_escape_string($link, $value) . "'";
        }

        $values[] = '(' . implode(',', $card) . ')';
    }
}

$query = "INSERT INTO cards (id, name, class, type, faction, race, set, rarity, text, flavor,
    inPlayText, cost, attack, health, durability, mechanics, howToGet, howToGetGold, artist, collectible, elite)
    VALUES "
. implode(',', $values);
?>

Zorg er wel voor dat je de keys in de array in de volgorde zet waarin ze in de INSERT zijn opgegeven.
 
Louis Deconinck

Louis Deconinck

13/07/2014 07:38:53
Quote Anchor link
@Ger dat is idd een goede oplossing, ik wil echter niet elke kaart uit de json file in de database, daarom dat ik eerst een manuele controle doe. En de fout heb ik al gevonden, set gebruiken als naam van een database rij kan niet, omdat dit een mysqli argument is.

Toevoeging op 13/07/2014 08:21:01:

Er zitten toch nog 2 fouten 1, eerst en vooral krijg ik de array 'mechanics' niet in de database. Ook krijg ik 'text' niet in de database als deze accenten bevat. Hoe oplossen? Dit is de code:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
if (isset($obj[$set][$nr]['text'])) {
$text = mysqli_real_escape_string($link, $obj[$set][$nr]['text']);
}
else {
$text = '';
}

if (isset($obj[$set][$nr]['mechanics'])) {
$mechanics = implode(', ', $obj[$set][$nr]['mechanics']);
}
else {
$mechanics = '';
}


$insert1 = mysqli_query($link, "INSERT INTO cards (id, name, class, type, faction, race, cardSet, rarity, cardText, flavor, inPlayText, cost, attack, health, durability, mechanics, howToGet, howToGetGold, artist, collectible, elite)
VALUES ('$id', '$name', '$playerClass', '$type', '$faction', '$race', '$set', '$rarity', '$text', '$flavor', '$inPlayText', '$cost', '$attack', '$health', '$durability', '$mechanics', '$howToGet', '$howToGetGold', '$artist', '$collectible', '$elite')"
) or die(mysql_error());
?>
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.