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
Met deze wel zeer beperkte informatie, heb ik ook geen idee.
File_get_contents(), json_decode() en foreach() om het uit te lezen.
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

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


[size=xsmall]Toevoeging op 12/07/2014 13:46:51:[/size]

Op regel 189 was ik een haakje vergeten, my bad

[size=xsmall]Toevoeging op 12/07/2014 13:49:33:[/size]

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

[size=xsmall]Toevoeging op 12/07/2014 14:45:13:[/size]

Verder zou ik ook graag mijn php skills wat verbeteren, dus als er stukken code zijn, die beter geschreven kunnen worden, laat maar komen :-)
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.
Na wat testen ben ik erachter gekomen dat het probleem in de query zit. Wat is hier fout aan?


<?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());
?>
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.

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

@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.

[size=xsmall]Toevoeging op 13/07/2014 08:21:01:[/size]

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:

<?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());
?>

Reageren