Précédent   Forum des professionnels en informatique > PHP > PHP & SGBD
PHP & SGBD Forum d'entraide sur les SGBD avec PHP. Avant de poster : FAQ BDD, toutes les FAQ PHP, cours BDD et sources BDD
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 15/06/2007, 16h30   #1
Membre du Club
 
Étudiant
Inscription : février 2007
Messages : 192
Détails du profil
Informations personnelles :
Âge : 23

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : février 2007
Messages : 192
Points : 52
Points : 52
Envoyer un message via MSN à jeanjean8501
Par défaut [SQL] Récupérer valeur d'une base de donnée

Bonjour a tous !

Voila j'ai creer une base de donnée :
Code php :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 <? 
    echo " <H1>Creation base de données  avec une requete sql <br></H1>";
    $f = mysql_connect("localhost","root","");
    $req = "Create Database BaseNotesIris";
    $ok = mysql_query($req,$f);
    mysql_select_db("BaseNotesIris");
    $req="create Table annee1
        ( 
          Trimestre Int(2)unsigned not null ,
          Francais Int(5) unsigned not null ,
          Anglais  Int(5) unsigned not null ,
          Economie Int(5) unsigned not null ,
          Math     Int(5) unsigned not null ,
          Physique Int(5) unsigned not null ,
          Informatique1  Int(5) unsigned not null ,
          Informatique2  Int(5) unsigned not null ,
          Informatique3  Int(5) unsigned not null ,
          Informatique4  Int(5) unsigned not null ,
          primary key(Trimestre)
        )";
        // primary key(num)
 
    $ok=mysql_query($req,$f);
    mysql_close($f);
?>
j'affiche les resultats dans un tableau :
Code php :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 <?
    /* connexion au serveur */
    $f = mysql_connect ("localhost", "root", "");
 
    mysql_select_db ("BaseNotesIris");
    $req = "select * from annee1";
    $result = mysql_query ($req, $f);
    $nbLignes = mysql_num_rows ($result);
    $nbCols = mysql_num_fields ($result);
 
    echo "<table border = \"3\">";
    echo "<tr> <td>Trimestre </td>
        <td>Francais </td>
         <td>Anglais  </td>
        <td>Economie </td>
        <td>Math     </td>
         <td> Physique </td>
         <td> Informatique1  </td>
         <td> Informatique2  </td>
         <td> Informatique3  </td>
         <td> Informatique4  </td></tr>";
 
    for ($i=0; $i<2; $i++)
    {
        $lig = mysql_fetch_array ($result);
        echo "<tr>";
 
        for ($j=0; $j<$nbCols; $j++)
        {
            echo "<td>";
            echo "$lig[$j]" ;
            echo"</td>";
        }
        echo  "</tr>";
    }
    echo "</table>";
 
    mysql_close ($f);
?>
Mon souhait serait de recuperer les differentes valeurs de ma base par rapport a la clé pour apres pourvoir utiliser ces variables pour creer un graph avec jpgraph ( mon graph est deja cree ya plus qu'a lui affecter les variables de ma base ! )

Je pense que la requete devrait etre un truc dans ce genre $req = "select * from annee1 where Trimestre=1";

ps Trimestre est ma clé !

Toute aide est bienvenue
jeanjean8501 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 17/06/2007, 09h40   #2
Membre Expert
 
Avatar de eric.pommereau
 
Homme ERIC POMMEREAU
Ingénieur intégration
Inscription : décembre 2004
Messages : 683
Détails du profil
Informations personnelles :
Nom : Homme ERIC POMMEREAU
Âge : 38
Localisation : France

Informations professionnelles :
Activité : Ingénieur intégration
Secteur : Administration - Collectivité locale

Informations forums :
Inscription : décembre 2004
Messages : 683
Points : 1 294
Points : 1 294
Les données que tu dois "passer" à jpGraph doivent être sous forme de tableau. Il te suffit donc, à l'extraction des éléments de ta base de données de constituer ce tableau:

Un petit exemple que j'ai en reserve...

Code php :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
include ("../jpGraph/jpgraph.php");
include ("../jpGraph/jpgraph_pie.php");
 
define('MYSQL_HOST', 'localhost');
define('MYSQL_USER', 'root');
define('MYSQL_PASS', '');
define('MYSQL_DATABASE', 'tuto_jp_graph');
 
$tableauAnnees = array();
$tableauNombreVentes = array();
 
// ************************************************************************
// Extraction des données dans la base de données *************************
// ************************************************************************
 
$sql = <<<EOF
	SELECT  
		YEAR(`DTHR_VENTE`) AS ANNEE,
		COUNT(ID) AS NBR_VENTES  
	FROM `ventes`
	GROUP BY YEAR(`DTHR_VENTE`)
EOF;
 
$mysqlCnx = @mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) or die('Pb de connxion mysql');
 
@mysql_select_db(MYSQL_DATABASE) or die('Pb de selection de la base');
 
$mysqlQuery = @mysql_query($sql, $mysqlCnx) or die('Pb de requête');
 
while ($row = mysql_fetch_array($mysqlQuery,  MYSQL_ASSOC)) {
	$tableauAnnees[] = $row['ANNEE'];
	$tableauNombreVentes[] = $row['NBR_VENTES'];
}
 
/*
printf('<pre>%s</pre>', print_r($tableauAnnees,1));
printf('<pre>%s</pre>', print_r($tableauNombreVentes,1));
*/
 
// ************************************************************************
// Création du graph ******************************************************
// ************************************************************************
 
// On spécifie la largeur et la hauteur du graph
$graph = new PieGraph(400,300,"auto");
// Je souhaite ajouter une ombre au graph
$graph->SetShadow();
 
// Donner un titre
$graph->title->Set("Volume des ventes par années");
// Quelle font et quel style pour le titre
$graph->title->SetFont(FF_FONT1,FS_BOLD);
 
// Créer un camenbert 
$p1 = new PiePlot($tableauNombreVentes);
 
// Spécifier des couleurs personnalisées... #FF0000 ok
$p1->SetSliceColors(array('red', 'blue', 'green'));
 
// Légendes qui accompagnent le graphe, ici chaque année avec sa couleur
$p1->SetLegends($tableauAnnees);
$p1->SetCenter(0.4);
$p1->SetValueType(PIE_VALUE_ABS);
$p1->value->SetFormat('%d');
 
$graph->Add($p1);
 
$graph->Stroke();
?>


Voilà
eric.pommereau est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 17/06/2007, 23h09   #3
Membre du Club
 
Étudiant
Inscription : février 2007
Messages : 192
Détails du profil
Informations personnelles :
Âge : 23

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : février 2007
Messages : 192
Points : 52
Points : 52
Envoyer un message via MSN à jeanjean8501
Merci de ton aide ! Grace a toi je suis parvenu a mes fins

Pour ceux que sa interresse :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php   
include ("./src/jpgraph.php"); // Bibliotheque necessaire au type de graph
include ("./src/jpgraph_bar.php");
 
// Definition des Users , pass, bdd ...
define('MYSQL_HOST', 'localhost'); 
define('MYSQL_USER', 'root'); 
define('MYSQL_PASS', ''); 
define('MYSQL_DATABASE', 'BaseNotesIris');   
 
//$tableauMati = array(); 
$tableauNotes = array();   // Création d'un tableau de notes
 
// ************************************************************************ 
// Extraction des données dans la base de données ************************* 
// ************************************************************************   
 
// Selection des differents champs souhaités de  la bdd
$sql = <<<EOF
	SELECT  
		Francais,
		Anglais,
		Economie  ,
		Math      ,
		Physique  ,
		Informatique1 ,
		Informatique2 ,
		Informatique3 ,
		Informatique4 
	FROM `annee1`
EOF;
 
// Requetes sql
$mysqlCnx = @mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) or die('Pb de connxion mysql');   
@mysql_select_db(MYSQL_DATABASE) or die('Pb de selection de la base');   
$mysqlQuery = @mysql_query($sql, $mysqlCnx) or die('Pb de requête');   
 
// Scan de la base de donnée en affectant les données dans le tableau
while ($row = mysql_fetch_array($mysqlQuery, MYSQL_ASSOC)) 
{  
	$tableauNotes[] = $row['Francais']; 
	$tableauNotes[] = $row['Anglais']; 
	$tableauNotes[] = $row['Economie']; 
	$tableauNotes[] = $row['Math']; 
	$tableauNotes[] = $row['Physique']; 
	$tableauNotes[] = $row['Informatique1']; 
	$tableauNotes[] = $row['Informatique2']; 
	$tableauNotes[] = $row['Informatique3']; 
	$tableauNotes[] = $row['Informatique4']; 
} 
 
// ************************************************************************
// *************************Création du graph *******************************
// ************************************************************************
 
// Notes en datay et Matieres en datax 
// En data X on ecrit les titres car on veut les notes en fonction des matieres !  Si on voulait des notes en fonction du num de trimestre il faudrait creer un nouveau tableau et effectuer la meme manip que pour tableauNotes !!!
$datax=array("Français ","Anglais ","Economie ","Mathématiques ","Physique ","Informatique1 ","Informatique2 ","Informatique3 ","Informatique4 ");
$datay=$tableauNotes;
 
// Size of graph
$width=500; 
$height=500;
 
// Set the basic parameters of the graph 
$graph = new Graph($width,$height,'auto');
$graph->SetScale("textlin");
 
$top = 50;
$bottom = 80;
$left = 50;
$right = 40;
$graph->Set90AndMargin('auto',$right,$top,$bottom);
 
$graph->xaxis->SetPos('min');
 
// Nice shadow
$graph->SetShadow();
 
// Setup title
$graph->title->Set("Notes du premier trimestre");
//$graph->title->SetFont(FF_VERDANA,FS_BOLD,14);
$graph->subtitle->Set("(DURAND Adrien)");
 
// Setup X-axis
$graph->xaxis->SetTickLabels($datax);
//$graph->xaxis->SetFont(FF_FONT2,FS_BOLD,12);
 
// Some extra margin looks nicer
$graph->xaxis->SetLabelMargin(1);
 
// Label align for X-axis
$graph->xaxis->SetLabelAlign('right','center');
 
// Add some grace to y-axis so the bars doesn't go
// all the way to the end of the plot area
$graph->yaxis->scale->SetGrace(20);
 
// Setup the Y-axis to be displayed in the bottom of the 
// graph. We also finetune the exact layout of the title,
// ticks and labels to make them look nice.
$graph->yaxis->SetPos('max');
 
// First make the labels look right
$graph->yaxis->SetLabelAlign('center','top');
$graph->yaxis->SetLabelFormat('%d');
$graph->yaxis->SetLabelSide(SIDE_RIGHT);
 
// The fix the tick marks
$graph->yaxis->SetTickSide(SIDE_LEFT);
 
// Finally setup the title
$graph->yaxis->SetTitleSide(SIDE_RIGHT);
$graph->yaxis->SetTitleMargin(35);
 
// To align the title to the right use :
$graph->yaxis->SetTitle('Bts IRIS 2006/2007','high');
$graph->yaxis->title->Align('right');
 
// To center the title use :
//$graph->yaxis->SetTitle('Turnaround 2002','center');
//$graph->yaxis->title->Align('center');
 
//$graph->yaxis->title->SetFont(FF_ARIAL,FS_BOLD,12);
$graph->yaxis->title->SetAngle(0);
 
$graph->yaxis->SetFont(FF_FONT2,FS_NORMAL);
// If you want the labels at an angle other than 0 or 90
// you need to use TTF fonts
//$graph->yaxis->SetLabelAngle(0);
 
// Now create a bar pot
$bplot = new BarPlot($datay);
$bplot->SetFillColor("orange");
$bplot->SetShadow();
 
//You can change the width of the bars if you like
//$bplot->SetWidth(0.5);
 
// We want to display the value of each bar at the top
$bplot->value->Show();
//$bplot->value->SetFont(FF_ARIAL,FS_BOLD,12);
$bplot->value->SetAlign('left','center');
$bplot->value->SetColor("black","darkred");
$bplot->value->SetFormat('%.1f /20');
 
// Add the bar to the graph
$graph->Add($bplot);
 
$graph->Stroke();
?>
Et voici le résultat en image ! ( Ps: Les notes sont fausses )



Encore merci eric190
jeanjean8501 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 18/06/2007, 10h35   #4
Membre du Club
 
Étudiant
Inscription : février 2007
Messages : 192
Détails du profil
Informations personnelles :
Âge : 23

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : février 2007
Messages : 192
Points : 52
Points : 52
Envoyer un message via MSN à jeanjean8501
Dernier petit renseignement !

Dans la requete sql je n'arrive pas a faire afficher que les notes du premier trimestre ! Vu que trimestre et la cle j'avai mis ceci :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
$sql = <<<EOF
	SELECT  
		Francais,
		Anglais,
		Economie  ,
		Math      ,
		Physique  ,
		Informatique1 ,
		Informatique2 ,
		Informatique3 ,
		Informatique4 
	FROM `annee1` WHERE 'Trimestre'=1
EOF;
Mais sa ne fonctionne pas

Comment faire pour ne recuperer que les notes en fonction de la clé ( trimestre) ???

Edit: Pb resolu ! J'ai ecrit Trimeste au lieu de Trimestre !
jeanjean8501 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 24/06/2007, 23h19   #5
Membre à l'essai
 
Inscription : juin 2007
Messages : 50
Détails du profil
Informations forums :
Inscription : juin 2007
Messages : 50
Points : 24
Points : 24
Par défaut recuperer les donnees d'une table pour utiliser jpgraph

Bonsoir

j'avais le meme probleme que jeanjean8501 :
j'ai une bdd contenant la table 'individus', et je voulais creer un graph avec jpgraph (un camembert) representant le nb d'individu selon leur categorie socioprofessionnelle.

J'ai essayé de comprendre le code ci-dessus et de l'appliquer à mon sujet, mais ça n'a pas marché.

Alors j'ai fait comme ça:

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
include("jpgraph-1.21b/src/jpgraph.php"); 
include("jpgraph-1.21b/src/jpgraph_pie.php"); 
 
 
//**********************************************************
//****Requetes pour recuperer les donnees de la table individus****
//********************************************************
 
	mysql_connect("localhost","root","");
	mysql_select_db("site");
 
	//Recuperation du nb d'individu par CSP (6 CSP differentes)
	$req1="SELECT COUNT(*) FROM individus WHERE CSP='Etudiant'; ";
	$res1=mysql_query($req1) or die("La requete 1 ne fonctionne pas");
	$nb_etudiant=mysql_result($res1,0);
 
	$req2="SELECT COUNT(*) FROM individus WHERE CSP='Ouvrier'; ";
	$res2=mysql_query($req2) or die("La requete 2 ne fonctionne pas");
	$nb_ouvrier=mysql_result($res2,0);
 
	$req3="SELECT COUNT(*) FROM individus WHERE CSP='Retraite'; ";
	$res3=mysql_query($req3) or die("La requete 3 ne fonctionne pas");
	$nb_retraite=mysql_result($res3,0);
 
	$req4="SELECT COUNT(*) FROM individus WHERE CSP='Employe'; ";
	$res4=mysql_query($req4) or die("La requete 4 ne fonctionne pas");
	$nb_employe=mysql_result($res4,0);
 
	$req5="SELECT COUNT(*) FROM individus WHERE CSP='Cadre'; ";
	$res5=mysql_query($req5) or die("La requete 5 ne fonctionne pas");
	$nb_cadre=mysql_result($res5,0);
 
	$req6="SELECT COUNT(*) FROM individus WHERE CSP='Autre'; ";
	$res6=mysql_query($req6) or die("La requete 6 ne fonctionne pas");
	$nb_autre=mysql_result($res6,0);
 
        mysql_close();
 
 
// Definitions des tableaux contenant les donnees et les valeurs pour la légende
$data = array($nb_etudiant,$nb_ouvrier,$nb_retraite,$nb_employe,$nb_cadre,$nb_autre);
$caption = array('Etudiant','Ouvrier','Retraite','Employe','Cadre','Autre');
 
// Creation du graphique
$graph = new PieGraph(400,300,"auto");
$graph->SetShadow();
 
// Parametrage du titre
$graph->title->Set("Catégorie Socioprofessionnelle");
$graph->title->SetFont(FF_FONT1,FS_BOLD);
 
// creation camembert
$p1 = new PiePlot($data);
$p1->SetLegends($caption);
$p1->SetCenter(0.35);
 
$graph->Add($p1);
$graph->Stroke();
?>
Ca a marché, certes, mais c'est un peu long.
Donc j'aurais bien aimé comprendre la maniere citée plus haut, et entre autres :
- à quoi sert le
- que signifie le @ dans
Code :
@mysql_select_db(MYSQL_DATABASE)
(j'ai pas mal cherché sur internet et sur ce site mais j'ai pas trouvé...)

Merci d'avance à ceux qui pourront me repondre.
honeydew est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 25/06/2007, 08h27   #6
Membre Expert
 
Avatar de eric.pommereau
 
Homme ERIC POMMEREAU
Ingénieur intégration
Inscription : décembre 2004
Messages : 683
Détails du profil
Informations personnelles :
Nom : Homme ERIC POMMEREAU
Âge : 38
Localisation : France

Informations professionnelles :
Activité : Ingénieur intégration
Secteur : Administration - Collectivité locale

Informations forums :
Inscription : décembre 2004
Messages : 683
Points : 1 294
Points : 1 294
Bonjour,

Code php :
1
2
3
$maVar = <<<EOF
....
EOF;

Permet de mettre du contenu formatté (et lisible surtout comme tu peux le voir très utile dans le cadre d'une requête) dans une variable ou directement pour l'affichage. Le EOF est une étiquette (on pourrait mettre un autre texte...). Une chose importante l'étiquette de fin 'EOF;' doit être à la ligne et ne doit être précédée d'aucun autre caractère.

Citation:
@mysql_select_db(MYSQL_DATABASE)
Le @ devant une fonction PHP permet de désactiver l'affichage automatique des erreurs.

@+
eric.pommereau est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 12h08.


 
 
 
 
Partenaires

Hébergement Web