0

I wish to extract one variable by one variable from my json file. My goal is to use this variable in php, to be able to do mysql query for exemple.

So my json file is here: pages/getRank-classic.php?idstart=0

[
    {"rank":1,"id":"111","site_name":"test1","site_vip":"No","boost":"0","site_banner":"test.png","site_banner_wallrank":"","site_pointstotaux":"5044","site_motclef1":"Pvp\/Fac","site_motclef2":"Skyblock","site_motclef3":"Cr\u00e9atif","site_motclef4":"Hunger-Games","site_motclef5":"SKywars\/Mini-Gam","site_presentationvideo":"3TGjebmNOfs"},
    {"rank":2,"id":"222","site_name":"test2","site_vip":"No","boost":"0","site_banner":"test.jpg","site_banner_wallrank":"","site_pointstotaux":"4114","site_motclef1":"hunger","site_motclef2":"games","site_motclef3":"pvp","site_motclef4":"survival","site_motclef5":null,"site_presentationvideo":"3TGjebmNOfs"}
]

I am trying to use it in include like:

<script type="text/javascript" >
$.ajax({
    type: 'POST',
    dataType: 'json',
    url: '/pages/getRank-classic.php?idstart=0',
    data: data,
    cache: false,
    success: function(test) {
        alert(test.id);
        alert(test.rank);
    }
});
</script>

So how I can do it please?

2
  • $myvar = json_decode($_REQUEST['yourvar']); Commented Mar 27, 2014 at 18:33
  • I finally used php like that => <?php $jsonlink = '/pages/getRank-classic.php?idstart=0'; $json = file_get_contents($jsonlink); $link = mysql_connect('sql.n1-servers.fr', 'www-top-mc', 'cd06bc5eprivate'); if (!$link) { die('Could not connect: ' . mysql_error()); } $db_selected = mysql_select_db('www-top-minecraft-serveurs', $link); $result = json_decode($json); foreach($result as $key => $value) { if($value) { mysql_query("UPDATE www-top-minecraft-serveurs.sites SET site_rank = '$value->rank' WHERE sites.site_id = '$value->id'"); } mysql_close; } ?> Commented Mar 28, 2014 at 15:38

2 Answers 2

1

Since it is an array of json, you need to specify the index too.

test[0].id

Also you can iterate through the object using this way,

    var data = [{
    "rank": 1,
    "id": "111",
    "site_name": "test1",
    "site_vip": "No",
    "boost": "0",
    "site_banner": "test.png",
    "site_banner_wallrank": "",
    "site_pointstotaux": "5044",
    "site_motclef1": "Pvp/Fac",
    "site_motclef2": "Skyblock",
    "site_motclef3": "Cr\u00e9atif",
    "site_motclef4": "Hunger-Games",
    "site_motclef5": "SKywars/Mini-Gam",
    "site_presentationvideo": "3TGjebmNOfs"
}, {
    "rank": 2,
    "id": "222",
    "site_name": "test2",
    "site_vip": "No",
    "boost": "0",
    "site_banner": "test.jpg",
    "site_banner_wallrank": "",
    "site_pointstotaux": "4114",
    "site_motclef1": "hunger",
    "site_motclef2": "games",
    "site_motclef3": "pvp",
    "site_motclef4": "survival",
    "site_motclef5": null,
    "site_presentationvideo": "3TGjebmNOfs"
    }];

    $(data).each(function () {
        alert(this.id);
    });

You can fetch the json using the ajax, then in the success event, put this code like,

$.ajax({
type: 'POST',
dataType: 'json',
url: '/pages/getRank-classic.php?idstart=0',
data: data,
cache: false,
success: function(test) {
    $(test).each(function () {
        alert(this.id);
    });
}
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but I want to use it with a json output with more than those 2 exemples. And how I use it as variable in php ?
0

I finally used php like that =>

<?php

$jsonlink = '/pages/getRank-classic.php?idstart=0';
$json = file_get_contents($jsonlink);

$link = mysql_connect('localhost', 'database', 'password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db('database', $link);

$result = json_decode($json);
foreach($result as $key => $value) {
    if($value) {

    mysql_query("UPDATE `database`.`sites` SET `site_rank` = '$value->rank' WHERE `sites`.`site_id` = '$value->id'");
    }
    mysql_close;
}

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.