11499 sujets

JavaScript, DOM et API Web HTML5

Bonjour,

Je souhaiterai récupérer les colonnes de ma base de données dans des tableaux respectifs, exemple:
Dans ma base de données j ai une colonne "Year" et une colonne "Country" je souhaiterai donc avoir, deux tableaux:
Col_Year[] : contenant toutes les lignes de la base de données avec l'année correspondant à ces commandes (une commande par ligne)
Col_Country[] : contenant toutes les lignes de la base de données avec le pays correspondant à ces commandes (une commande par ligne)

Pour cela en surfant sur internet j ai trouvé la fonction php GetCol, qui permet normalement de justement récupérer une colonne d'une DB ...
Cependant je n'arrive pas à l'utiliser, ça ne marche pas Smiley bawling

Donc j espere que vous pourrez m'aider

Voici mon code:


Javascript:

function ajax_call2(what2do, callback){
$.ajax({	
	type: "GET",
	async: false,
	dataType: "json",
	url: "data.php?" + what2do,
	success: function(json) {
		callback(json);
	}
});		
};

var Col_BPname = new Array();
var Col_Country = new Array();
var Col_Net_EURO = new Array();
var Col_Month = new Array();
var Col_Quarter = new Array();
var Col_Year = new Array();
var Col_Regions = new Array();
var Col_Offer_type = new Array();
var Col_Releas_OXE = new Array();
var Col_Users_with_sip = new Array();
var Col_Additionnal_users_OF_1 = new Array();

function GetDataBase() {
var tab_nomColonne = ["BP_name", "Country", "Net_EURO", "Segment", "Month", "Quarter", "Year", "Regions", "Offer_type", "Releas_OXE", "Users_with_sip", "Additionnal_users_OF_1"];	
for(var j=0;j<tab_nomColonne.length;j++){
	url = "colonne=" + tab_nomColonne[j];
	ajax_call2(url, function(json){
		window["Col_" + tab_nomColonne[j].toString()] = json['lacolonne'];
	});	
}

}

tab_nomColonne, contient donc la liste de toutes mes colonnes


php

$resultat = array();

function DataBase($colonne) {

try{
	$db = new PDO('mysql:host=localhost;dbname=actis', 'root', '');
}
catch (Exception $e){
	die('Erreur : ' . $e->getMessage());
}
$data = array();
$data =& $db->getCol('SELECT '.$colonne.' FROM `actis_table`', $colonne);

return $data;
}

if (isset($_GET["colonne"])){
$colonne = $_GET["colonne"];
$resultat['lacolonne'] = DataBase ($colonne);		
}

print json_encode($resultat);




Je pense que je n'arrive pas a bien utiliser la méthode GetCol, mais je ne trouve pas beaucoup d exemples pour m'aider sur internet, c'est pourquoi je viens vers vous en espérant que vous pourrez m'éclairer=)

j'ai découvert cette méthode ici:
http://pear.php.net/manual/fr/package.database.db.db-common.getcol.php
Modifié par InfoGeo (25 Jul 2013 - 10:33)
Merci de ta réponse, cependant je ne la comprend pas Smiley ohwell

Comment faire pour définir ma méthode GetCol du coup, aurais tu un exemple stp ?

De plus tu dis que c dépassé ? tu parles de la fonction GetCol ?, en suivant ton lien je suis tombé sur une page sans aucune explication ni exemple


Geoffrey
J'ai modifié ma fonction php:

mais ca ne marche toujours pas :'(

function DataBase($colonne) {
	try{
		$db = new PDO('mysql:host=localhost;dbname=actis', 'root', '');
	}
	catch (Exception $e){
		die('Erreur : ' . $e->getMessage());
	}
	$data = array();
	$row = array();
	$sth = $db->query('SELECT DISTINCT '.$colonne.' FROM `actis_table`');
	while ($row= $sth->fetchAll(PDO::FETCH_NUM)) {
		$data[] = $row[0];
	}
	var_dump($data);			
	return $data;
}

if (isset($_GET["colonne"])){
	$colonne = $_GET["colonne"];
	$resultat['lacolonne'] = DataBase ($colonne);		
}


le var_dump affiche: (avec comme exemple: DataBase("Year"); )


array
  0 => 
    array
      0 => string '2011' (length=4)
 
[]

Modifié par InfoGeo (24 Jul 2013 - 16:57)
Essai ça :


function DataBase($colonne) {
  try {
    $db = new PDO('mysql:host=localhost;dbname=actis', 'root', '');
  }
  catch (Exception $e){
    die('Erreur : ' . $e->getMessage());
  }

  $sth = $db->query('SELECT DISTINCT ' . $db->quote($colonne) . ' FROM `actis_table`');
  return $sth->fetchAll(PDO::FETCH_COLUMN);
}

if (isset($_GET["colonne"])) {
  $colonne = $_GET["colonne"];
  $resultat['lacolonne'] = DataBase($colonne);
  var_dump($resultat['lacolonne']);
}


Et regarde dans la doc de PHP les changements que j'ai fait sur ton code.
merci de ta réponse


Ce que je souhaite retourner c'est un tableau en gros si je prend l'exemple de l'année une nouvelle fois, c'est avoir:

$datas[0] = "2011"
$datas[1] = "2012"
$datas[2] = "2012"
$datas[3] = "2011"
....
$datas[1000] = "2012"

Pour chaque ligne de ma base de données donc =)

C'est pour cela qu'il y avait un while .... et c la que ca marche pas je pense:

J'ai adapté ce que tu m'as dit:

DataBase("Year");

function DataBase($colonne) {
try {
	$db = new PDO('mysql:host=localhost;dbname=actis', 'root', '');
}
catch (Exception $e){
	die('Erreur : ' . $e->getMessage());
}
$datas = array();
$sth = $db->query('SELECT DISTINCT ' . $db->quote($colonne) . ' FROM `actis_table`');
$datas[] = $sth->fetchAll(PDO::FETCH_COLUMN);
var_dump ($datas);
return $datas; 
}

if (isset($_GET["colonne"])){
	$colonne = $_GET["colonne"];
	$resultat['lacolonne'] = DataBase ($colonne);		
}



Mais pour $datas j'ai le meme resultat:

array
  0 => 
    array
      0 => string 'Year' (length=4)

[]


Donc pourrais tu me dires comment récupérer un tableau vraiment stp ?
J'ai fais copié coller et ca ne marche pas

De plus le var_dump ne s'affiche pas vu que c de l'ajax ...

Avant j'arrivais a afficher (je test en affichant dans un select): 2011 -undefined - undefined ....

Maintenant c'est juste undefined - undefined - undefined .....

:/




javascript:
var Col_BPname = new Array();
var Col_Country = new Array();
var Col_Net_EURO = new Array();
var Col_Month = new Array();
var Col_Quarter = new Array();
var Col_Year = new Array();
var Col_Regions = new Array();
var Col_Offer_type = new Array();
var Col_Releas_OXE = new Array();
var Col_Users_with_sip = new Array();
var Col_Additionnal_users_OF_1 = new Array();

function GetDataBase() {
	var tab_nomColonne = ["BP_name", "Country", "Net_EURO", "Segment", "Month", "Quarter", "Year", "Regions", "Offer_type", "Releas_OXE", "Users_with_sip", "Additionnal_users_OF_1"];	
	for(var i=0;i<tab_nomColonne.length;i++){
		url = "colonne=" + tab_nomColonne[i];
		ajax_call2(url, function(json){
			window["Col_" + tab_nomColonne[i].toString()] = json['lacolonne'];
		});	
	}	
}
[/i][/i]
Tres bien je retesterai ca demain (je n'ai pas le site sous la main la Smiley ohwell )

Cependant je ne comprend pas ....

$sth->fetchAll(PDO::FETCH_COLUMN);


Retourne donc un tableau ? car à l'arrivée je récupère ca sous forme de tableau .....



Geoffrey
Modifié par InfoGeo (24 Jul 2013 - 23:57)
ha d'accord merci, bon donc j'ai essayé comme ca:

function DataBase($colonne) {
	try {
		$db = new PDO('mysql:host=localhost;dbname=actis', 'root', '');
	}
	catch (Exception $e){
		die('Erreur : ' . $e->getMessage());
	}

	$sth = $db->query('SELECT DISTINCT ' . $db->quote($colonne) . ' FROM `actis_table`');
	return $sth->fetchAll(PDO::FETCH_COLUMN);
}

var_dump (DataBase("Year"));


en testant directement mon php en ouvrant la page:
http://localhost/Actis_Analytics/data.php

Et avec le var_dump j'obtiens ceci :

array
  0 => string 'Year' (length=4)

[]


Smiley bawling


Une idée du pourquoi et du comment ? ^^



Geoffrey
J'ai cherché sur internet et j'ai trouvé y avait un petit quelque chose a changé ! et maintenant ca marche merci beaucoup pour ton aide =)



var_dump(DataBase("Country"));

function DataBase($colonne) {
	try{
		$db = new PDO('mysql:host=localhost;dbname=actis', 'root', '');
	}
	catch (Exception $e){
		die('Erreur : ' . $e->getMessage());
	}
	$data = array();
	$sth = $db->prepare('SELECT '.$colonne.' FROM `actis_table`');
	$sth->execute();
	$data = $sth->fetchAll(PDO::FETCH_COLUMN, 0);					
	return $data;
}


le var_dump donne:

array
  0 => string 'Germany' (length=7)
  1 => string 'Norway' (length=6)
  2 => string 'France' (length=6)
  3 => string 'France' (length=6)
  4 => string 'France' (length=6)
  5 => string 'Martinique' (length=10)
  6 => string 'Martinique' (length=10)
  7 => string 'Germany' (length=7)
  8 => string 'Germany' (length=7)
  9 => string 'Germany' (length=7)
  10 => string 'Germany' (length=7)
  11 => string 'Germany' (length=7)
  12 => string 'Germany' (length=7)
  13 => string 'Germany' (length=7)
  14 => string 'Spain' (length=5)
  15 => string 'France' (length=6)
  16 => string 'France' (length=6)
  17 => string 'France' (length=6)
  18 => string 'France' (length=6)
  19 => string 'France' (length=6)
  20 => string 'France' (length=6)
  21 => string 'Belgium' (length=7)
  22 => string 'Belgium' (length=7)
  23 => string 'Portugal' (length=8)
  24 => string 'Portugal' (length=8)
  25 => string 'Portugal' (length=8)
  26 => string 'Portugal' (length=8)
  27 => string 'Switzerland' (length=11)
  28 => string 'Switzerland' (length=11)
  29 => string 'Italy' (length=5)
  30 => string 'Italy' (length=5)
  31 => string 'France' (length=6)
  32 => string 'Spain' (length=5)
  33 => string 'Spain' (length=5)
  34 => string 'Spain' (length=5)
  35 => string 'Spain' (length=5)
  36 => string 'Spain' (length=5)
  37 => string 'Spain' (length=5)
  38 => string 'Spain' (length=5)
  39 => string 'Spain' (length=5)
  40 => string 'Spain' (length=5)
  41 => string 'France' (length=6)
  42 => string 'Italy' (length=5)
  43 => string 'Italy' (length=5)
  44 => string 'France' (length=6)
  45 => string 'France' (length=6)
  46 => string 'France' (length=6)
  47 => string 'Austria' (length=7)
  48 => string 'Germany' (length=7)
  49 => string 'Germany' (length=7)
  50 => string 'Germany' (length=7)
  51 => string 'Germany' (length=7)
  52 => string 'France' (length=6)
  53 => string 'France' (length=6)
  54 => string 'Italy' (length=5)
  55 => string 'Italy' (length=5)
  56 => string 'Italy' (length=5)
  57 => string 'Italy' (length=5)
  58 => string 'Portugal' (length=8)
  59 => string 'Belgium' (length=7)
  60 => string 'Germany' (length=7)
  61 => string 'Italy' (length=5)
  62 => string 'Ivory Coast' (length=11)
  63 => string 'Panama' (length=6)
  64 => string 'France' (length=6)
  65 => string 'France' (length=6)
  66 => string 'France' (length=6)
  67 => string 'France' (length=6)
  68 => string 'France' (length=6)
  69 => string 'France' (length=6)
  70 => string 'Germany' (length=7)
  71 => string 'France' (length=6)
  72 => string 'France' (length=6)
  73 => string 'France' (length=6)
  74 => string 'France' (length=6)
  75 => string 'France' (length=6)
  76 => string 'France' (length=6)
  77 => string 'France' (length=6)
  78 => string 'France' (length=6)
  79 => string 'France' (length=6)
  80 => string 'France' (length=6)
  81 => string 'France' (length=6)
  82 => string 'France' (length=6)
  83 => string 'France' (length=6)
  84 => string 'Germany' (length=7)
  85 => string 'Netherlands' (length=11)
  86 => string 'Spain' (length=5)
  87 => string 'Spain' (length=5)
  88 => string 'France' (length=6)
  89 => string 'France' (length=6)
  90 => string 'France' (length=6)
  91 => string 'France' (length=6)
  92 => string 'Austria' (length=7)
  93 => string 'Austria' (length=7)
  94 => string 'Austria' (length=7)
  95 => string 'Austria' (length=7)
  96 => string 'Austria' (length=7)
  97 => string 'Germany' (length=7)
  98 => string 'Germany' (length=7)
  99 => string 'Germany' (length=7)
  100 => string 'Germany' (length=7)
  101 => string 'Germany' (length=7)
  102 => string 'Germany' (length=7)
  103 => string 'Germany' (length=7)
  104 => string 'Germany' (length=7)
  105 => string 'Spain' (length=5)
  106 => string 'Spain' (length=5)
  107 => string 'France' (length=6)
  108 => string 'France' (length=6)
  109 => string 'France' (length=6)
  110 => string 'France' (length=6)
  111 => string 'France' (length=6)
  112 => string 'France' (length=6)
  113 => string 'France' (length=6)
  114 => string 'France' (length=6)
  115 => string 'France' (length=6)
  116 => string 'France' (length=6)
  117 => string 'France' (length=6)
  118 => string 'France' (length=6)
  119 => string 'France' (length=6)
  120 => string 'France' (length=6)
  121 => string 'France' (length=6)
  122 => string 'France' (length=6)
  123 => string 'France' (length=6)
  124 => string 'Italy' (length=5)
  125 => string 'Italy' (length=5)
  126 => string 'Italy' (length=5)
  127 => string 'Poland' (length=6)
  more elements...


Exactement ce que je voulais quoi :O !

Geoffrey