0

I'm currently trying to "foreach" some json data, but console is not returning anything, while it should return values (integers) of progress bars.

My current PHP file that should return the json data

$jsonData = [
 'progress' => [
 'health_pc' => $my->hp_percent,
 'energy_pc' => $my->energy_percent,
 'awake_pc' => $my->awake_percent,
 'nerve_pc' => $my->nerve_percent,
 'exp_pc' => $my->exp_percent,
 ]
];
echo json_encode($jsonData);

How I handle it in javascript:

// Progress Bars
 $.each(jsonData.progress, function() {
 $.each(this, function(k, v) {
 console.log(k + '-' + v);
 });
 }); 

Note that the console.log() doesn't log anything.

What i'm trying to achieve is to adjust the progress bars when the %'s change, normally I would do:

$('#health_pc').css('width', jsonData.progress.health_pc + '%');
$('#energy_pc').css('width', jsonData.progress.energy_pc + '%');
$('#awake_pc').css('width', jsonData.progress.awake_pc + '%');
$('#nerve_pc').css('width', jsonData.progress.nerve_pc + '%');
$('#exp_pc').css('width', jsonData.progress.exp_pc + '%');

but I want to foreach this rather than writing this all out.

Any help would be greatly appreciated.

Edit 1 Output of json_encode()

{"progress":{"health_pc":100,"energy_pc":100,"awake_pc":100,"nerve_pc":100,"exp_pc":3}}
asked Jan 20, 2018 at 23:29
2
  • Can you show the output of json_encode? Also why are you doing a nested loop? Try just $.each(jsonData.progress, function(k, v) { ... Commented Jan 20, 2018 at 23:34
  • @puelo added the output of json_encode() - see edit :) Commented Jan 20, 2018 at 23:38

3 Answers 3

1

As long as the php variables are no arrays themselves one level of for each should be enough:

$.each(jsonData.progress, function(k, v) {
 $('#'+k).css('width', v + '%');
})
answered Jan 20, 2018 at 23:41

1 Comment

This did the trick! Thank you so much! Will accept as answer in 1 minute when i'm able to. Again, thank you so much!
1

You must change your jQuery function to the following, you don't need two each methods.

const jsonData = {"progress":{"health_pc":100,"energy_pc":100,"awake_pc":100,"nerve_pc":100,"exp_pc":3}};
console.log(jsonData);
$.each(jsonData.progress, function(k, v) {
 console.log(k + '-' + v);
}); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

answered Jan 20, 2018 at 23:39

Comments

1

You can use a vanilla javascript for loop.

let $jsonData = {
 'progress': {
 'health_pc': 15,
 'energy_pc': 33,
 'awake_pc': 21,
 'nerve_pc': 87,
 'exp_pc': 9,
 }
};
let myFunction = function() {
 for (var key in $jsonData.progress) {
 if ($jsonData.progress.hasOwnProperty(key)) {
 $('#' + key).css('width', $jsonData.progress[key] + '%');
 $('#' + key).children('span').html($jsonData.progress[key] + '%');
 }
 }
};
myFunction();
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <div class="progress">
 <div class="progress-bar" role="progressbar" aria-valuenow="70"
 aria-valuemin="0" aria-valuemax="100" id='health_pc' style="width:70%">
 <span></span>
 </div>
</div> 
 <div class="progress">
 <div class="progress-bar" role="progressbar" aria-valuenow="70"
 aria-valuemin="0" aria-valuemax="100" id='energy_pc' style="width:70%">
 <span></span>
 </div>
</div> 
 <div class="progress">
 <div class="progress-bar" role="progressbar" aria-valuenow="70"
 aria-valuemin="0" aria-valuemax="100" id='awake_pc' style="width:70%">
 <span></span>
 </div>
</div> 
 <div class="progress">
 <div class="progress-bar" role="progressbar" aria-valuenow="70"
 aria-valuemin="0" aria-valuemax="100" id='nerve_pc' style="width:70%">
 <span></span>
 </div>
</div> 
 <div class="progress">
 <div class="progress-bar" role="progressbar" aria-valuenow="70"
 aria-valuemin="0" aria-valuemax="100" id='exp_pc' style="width:70%">
 <span></span>
 </div>
</div> 

Hope it helps!

answered Jan 20, 2018 at 23:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.