0

Hello everyone,I need a help to back values from php to javascript...

Listen:

__hasInventory = true;
	_iMatrix = [];

	$.ajax({
		url: 'index.php?acao=test',
		dataType: 'script',
		type: 'post',
		data: {mode: 0},
		success: function () {

If is all Ok with post method,I'd like it back with results passing PHP results to Javascript:

Something like:

_iMatrix[id]['descricao']

      _iMatrix[id]['hp']	

      _iMatrix[id]['nome']

Can someone Help me please = (((((

THanks

2
Contributors
8
Replies
9
Views
5 Years
Discussion Span
Last Post by Helinxed
0

Helinxed,

If I understand correctly, then you can pass your php object/array back by JSON encoding it.

Your code will be something like this :

...
$arr = array(
  'descricao' => 'A',
  'hp' => 'B',
  'nome' => 'C'
);
exit(json_encode($arr));
//at this point, id  must be defined
$.ajax({
  url: 'index.php?acao=test',
  type: 'POST',
  data: {mode: 0},
  dataType: "JSON",
  success: function(data){
    _iMatrix[id] = data;
  }
});

untested

Airshow

Edited by Airshow: n/a

0

Oh,I'm almost there ^^

This script is for an Online Map-Game...I need it to get info in map for each player

This _iMatrix[[B]id[/B]] is the ID of each Player in Map.

_iMatrix[[B]999[/B]] -> _iMatrix[[B]999[/B]] get the info descricao of the player 999

There's some way of get each info by ID of the player?

Thanks a lot!

Edited by Helinxed: n/a

0

Helinxed,

Aha, that's a bit different.

Do you want :-

  • to request all players in a single request?
  • to request each player by id, one at a time?

Airshow

0

I have a table in Mysql called Map. There's: ID , MAP_ID, DESCRICAO, HP, NOME

I'd like to get All players that's in the Map_ID.

Select * FROM Map where MAP_ID = '1'

Get this result and pass it to javascript BY ID of the player. So I guess request each player in the map by id one at a time...

I'll make this request in each 4 seconds to update users in map in real time

update = setInterval(function () {

            }, 4000);

Thank you a lot for helping me bro.

Edited by pyTony: fixed formating

0

Helinxed,

I think you probably want something like this:

$map_id = (isset($_REQUEST['map_id'])) ? intval($_REQUEST['map_id']) : false;
if($map_id){
	$sql = "Select * FROM Map where MAP_ID = $map_id";
	//execute $sql here to give the results as (array) $rows
	exit(json_encode($rows));
}
else{
	exit(json_encode(array()));
}
function fetch_matrix(map_Id){
	$.ajax({
		url: 'index.php',
		type: 'POST',
		data: {acao:'test', mode:0, map_id:map_id},
		dataType: "JSON",
		success: function(data){
			return data;
		}
	});
}


var _iMatrix = fetch_matrix(1);

You may need to work on this a bit to get exactly what you want.

Airshow

Edited by Airshow: n/a

0
$map_id = (isset($_REQUEST['map_id'])) ? intval($_REQUEST['map_id']) : false;
if($map_id){
	$sql = "Select * FROM Map where MAP_ID = $map_id";
        while($rows = mysql_fetch_array($sql)) {
	//execute $sql here to give the results as (array) $rows
	exit(json_encode($rows));
}
}
else{
	exit(json_encode(array()));
}
function fetch_matrix(map_Id){
	$.ajax({
		url: 'index.php',
		type: 'POST',
		data: {acao:'test', mode:0, map_id:map_id},
		dataType: "JSON",
		success: function(data){
			return data;
		}
	});
}
 
var _iMatrix = fetch_matrix(1);

In javascript the results would be like this:?

_iMatrix['id']['descricao']
1

Try:

$map_id = (isset($_REQUEST['map_id'])) ? intval($_REQUEST['map_id']) : false;
if($map_id){
	$sql = "Select * FROM Map where MAP_ID = $map_id";
        while($rows = mysql_fetch_array($sql)) {
	//execute $sql here to give the results as (array) $rows
	exit(json_encode($rows));
}
}
else{
	exit(json_encode(array()));
}
function fetch_matrix(map_id, target){
	$.ajax({
		url: 'index.php',
		type: 'POST',
		data: {acao:'test', mode:0, map_id:map_id},
		dataType: "JSON",
		success: function(data){
			$(data).each(function(){
				target[this.ID] = {
					descricao: this.descricao,
					hp: this.hp,
					nome: this.nome
				};
			});
		}
	});
}

var _iMatrix = [];
fetch_matrix(1, _iMatrix);

Airshow

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.