Use data from a JSON result into a table [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Sorry for the bad titling, I had difficulty naming it.
I am trying to take this data:
<?php
require '../lib/config.php';
// Get the server statuses
$pdb = connectPDO();
$stmt = $pdb->prepare('SELECT * FROM serverlist');
$stmt->execute();
echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
Which gives this result:
http://ichris.xyz/ajax/server-status.ajax.php
and then take that data and insert it into a table
http://ichris.xyz/server_list.php (the table I have made to insert this data).

Use JSON DECODE: http://php.net/manual/en/function.json-decode.php
Once you serialize the json feed, then using a while or for loop run your inserts using php.

You can use
$.get('ajax/server-status.ajax.php').done(function(results) {
var statusList = $.parseJSON(results);
var tableBody = $("table tbody");
if ($.isArray(statusList)){
for(var index in statusList){
var status = statusList[index];
$("<tr>").append("<td>"+status["name"]+"</td>")
.append("<td>"+status["faction2_name"]+"</td>")
.append("<td>"+status["map"]+"</td>")
.append("<td>"+status["players_online"]+"/"+status["players_max"]+"</td>")
.append("<td>"+(status["has_password"] ? 'Yes' : 'No')+"</td>")
.appendTo( tableBody );
}
}
});
See it in action in a JSFiddle demo

Related

is possible to insert data automatic process a action.php? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
EXAMPLE
I want to insert the data automatic everyday without press the button action or whatever.
In my case,
my data is ready to input every day on system but i must press the button action every day too.
thakyou
Handle an auto-click parameter in URL:
in URL: ?automode=1
in Javascript:
var is_automode = false,
$btn = YOUR_BUTTON,
$form = YOUR_FORM,
$mode = HIDDEN_INPUT_TEXT;
location.search.replace(/^\?/,'').split('&').forEach(function(val) {
var pair = val.split('=');
if (pair.length && pair[0]==='automode' && pair[1]!=0) {
is_automode = true;
}
});
if (is_automode) {
setInterval(function () {
$form.target = '_blank';
$mode.value = 'automode'; // detect this value in the target page, call window.close() if is 'automode'
$btn.click(); // or $form.submit()
}, 86400*1000);
}
Or add a cron job to open the URL (YOUR_URL/action.php?automode=1) without setInterval()

Cannot Add value to the Number Using Jquery [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to Add One Value to the "0001". When I am adding one value to this number the result shows only 2.How can i get 0002?
I am using this code:
var pcode = $('#productcode').val(); // Now the pcode value is 0001
var num= parseInt(pcode) + 1;
But the result shows only 2
Just call this function after adding..
function z4(n) {
var sign = +n < 0;
n = ''+n;
while(n.length<4) n = '0' + n;
return (sign ? '-' : '' ) + n;
}
z4(2); // 0002

php variable changes when clicked on html link [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to make a game in HTML and PHP and I'm pretty new to coding. The point is I need to execute a PHP script by clicking an HTML element. Is this possible?
My code would look something like this
$money = 100;
and by clicking on:
<a>rob a shop</a>
it must excecute:
function rob_shop(){
$money += 75;
echo "You now have $money $";}
Try this, is PHP:
<?php
if (isset($_GET['money'])) {
$money = rob_shop($_GET['money']);
echo 'You now have: '.$money.'<br>';
} else {
$money = 100;
echo 'You now have: '.$money.'<br>';
}
echo 'rob a shop';
function rob_shop($money){
$money = $money + 75;
return $money;
}
?>
But the best way to do it is with ajax to avoid refreshing the page.
Add extra param to link and check with php and call function.
rob money
Then check for link
if (isset($_GET['fun']))
{
runFun();
}
you will need to use javascript and an ajax call or you could just do it in javascript.
<a id="shop">rob a shop</a>
<div id="response"></div>
// include jquery
<script>
$( "#shop" ).click(function() {
var money = money + 75;
$("#response").html( "YOU NOW HAVE $"+money );
// or make an ajax call to a php script and return the value
});
</script>

jquery select adding extra rows [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a small ajax call:
$.ajax({
type:'POST',
dataType: 'json',
url: 'inc/getfunctions.php?q='+l_id+'&func=load_po',
success:function(data){
//alert ('hi');
if (data.po_num) {
$('#po_num_s').append($('<option>').text('Select a PO').attr('value', 0));
var po_num = data.po_num;
var $subType = $("#po_num_s");
$.each(data, function () {
$subType.append($('<option></option>').attr("value", data.l_id).text(data.po_num));
});
}
}
});
it is apending 2 rows :
<'option value="11">112212<'/option>
<'option value="11">112212<'/option>
is the output
Thanks in advance
The $.each function will take an array or an object and iterate over its items, while providing the item key and value as parameters to the callback. Like so:
$.each(["aaaa", "bb", "ccc"], function(key, value){
console.log(value.length);
});
// Will output:
// 4
// 2
// 3
But you aren't using the each-loop for something useful, as you don't receive any item in the callback function. The only reason you're getting as few as 2 lines is that your data object only has 2 keys in it. Try adding another property to data at the same place as your commented alert, like so:
......
success:function(data){
//alert ('hi');
data.foo = "bar"
if (data.po_num) {
......
and watch the each-loop go three rounds.

how to access jquery variable in php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My question is very very similar to this one
How to get jQuery variable value in PHP file
The difference is I have some code samples. I just cant seem to get a satisfactory answer for what I am looking for. I am doing all these in one php file. Is this even possible?
I want to store categoryChoiceVal into $category
Here is the html
<select class="categoryChoice ">
<option>Gym class</option>
<option>Math tutorial</option>
<option>Basketball court</option>
</select>
the php
<?php
$category = '';
$query_string = "https//this_is_a_dummy_string.htm?{$category}";
$data = file_get_contents($query_string);
?>
I am catching the selection from the select tag by doing this
$(function(){
$('select').change(function() {
var categoryChoiceVal = $('.categoryChoice option:selected').text();
});
});
Thanks in advance
<?php
$category = '';
if (isset($_POST['categoryChoiceVal'])){
$category = $_POST['categoryChoiceVal'];
}
$query_string = "https//this_is_a_dummy_string.htm?{$category}";
$data = file_get_contents($query_string);
?>
$(function(){
$('select').change(function() {
$.post('phpUrl',{
categoryChoiceVal:$('.categoryChoice option:selected').text()
});
});
});

Categories