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 21/10/2009, 15h43   #1
Futur Membre du Club
 
Inscription : novembre 2008
Messages : 62
Détails du profil
Informations forums :
Inscription : novembre 2008
Messages : 62
Points : 15
Points : 15
Par défaut Calcul pourcentage

Bonjour à tous,

J'ai crée un script en php qui calculant le pourcentage.

Voici mon script :

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
$result = pg_query($db, "SELECT KBarticleID, pourcentage_valide, pourcentage_invalide FROM $graphe WHERE date_ajout='$date' AND unite='$division'");
if (!$result)
{
  echo "Une erreur s'est produite.\n";
  exit;
}
 
$KBarticleID = array();
$pourcentage_valide = array();
$pourcentage_invalide = array();
$resultat_valide = array();
$resultat_valide_final = array();
 
while($row = pg_fetch_assoc($result))
{
        $KBarticleID[] = $row['KBarticleID'];
        $pourcentage_valide[] = $row['pourcentage_valide'];
        $pourcentage_invalide[] = $row['pourcentage_invalide'];
        $resultat_valide = $row['pourcentage_valide']/$row['pourcentage_valide']+$row['pourcentage_invalide'];
        $resultat_valide_final=$resultat_valide*100;
        echo $resultat_valide_final;
}
Voici ma table :

kbarticleid | pourcentage_valide | pourcentage_invalide
-------------+--------------------+-------------------
973540 | 0 | 0 |
973540 | 7 | 0 |
973507 | 0 | 0 |
973507 | 7 | 0 |
973354 | 0 | 0 |
973354 | 7 | 0 |
973346 | 14 | 0 |
973346 | 696 | 0 |

Code :
1
2
$row['pourcentage_valide']/$row['pourcentage_valide']+$row['pourcentage_invalide'];
$resultat_valide_final=$resultat_valide*100;
Donc voici le calcul :

Si je fais (0 + 0)/0 c'est impossible.
Par contre ce que je voudrais obtenir dans ce cas c'est 100% pour pourcentage valide et pourcentage_invalide 0

Est-ce-que pouvez-vous m'aider ?

Cordialement,
cycy_88 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 21/10/2009, 16h08   #2
Membre chevronné
 
Avatar de micetf
 
Homme Fred
Professeur des Ecoles
Inscription : mai 2009
Messages : 502
Détails du profil
Informations personnelles :
Nom : Homme Fred
Localisation : France, Rhône (Rhône Alpes)

Informations professionnelles :
Activité : Professeur des Ecoles
Secteur : Enseignement

Informations forums :
Inscription : mai 2009
Messages : 502
Points : 700
Points : 700
Personnellement, je calculerais $resultat_valide ainsi
Code :
1
2
3
$resultat_valide = ($row['pourcentage_valide']+$row['pourcentage_invalide']!=0) ?
								$row['pourcentage_valide']/$row['pourcentage_valide']+$row['pourcentage_invalide'] :
								1;
Fred
micetf est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/10/2009, 10h02   #3
Futur Membre du Club
 
Inscription : novembre 2008
Messages : 62
Détails du profil
Informations forums :
Inscription : novembre 2008
Messages : 62
Points : 15
Points : 15
Citation:
Envoyé par micetf Voir le message
Personnellement, je calculerais $resultat_valide ainsi
Code :
1
2
3
$resultat_valide = ($row['pourcentage_valide']+$row['pourcentage_invalide']!=0) ?
								$row['pourcentage_valide']/$row['pourcentage_valide']+$row['pourcentage_invalide'] :
								1;
Fred


j'ai utilisé ce que tu as fais et j'ai mis une condition.

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
while($row = pg_fetch_assoc($result))
{
        $kbarticleid[] = $row['kbarticleid'];
        $pourcentage_valide[] = $row['pourcentage_valide'];
        $pourcentage_invalide[] = $row['pourcentage_invalide'];
 
        $resultat_valide_final[] = ($row['pourcentage_valide']+$row['pourcentage_invalide']!=0)
        ? $row['pourcentage_valide']/$row['pourcentage_valide']+$row['pourcentage_invalide'] : 1;
 
        if ($resultat_valide_final[] = 1)
        {
                $resultat_valide_final[]=100;
        }
        else
        {
        }
        $resultat_invalide_final[] = 100 - $resultat_valide_final;
}
sauf que j'ai un problème il me met un message d'erreur

Citation:
Fatal error: Unsupported operand types in .....
En faites, je voudrais que les valeurs de resultat_valide_final et resultat_invalide_final soient dans un tableau pour qu'après, je puisse les afficher.

Peux-tu m'aider ?

Cdt,
cycy_88 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/10/2009, 23h27   #4
Membre chevronné
 
Avatar de micetf
 
Homme Fred
Professeur des Ecoles
Inscription : mai 2009
Messages : 502
Détails du profil
Informations personnelles :
Nom : Homme Fred
Localisation : France, Rhône (Rhône Alpes)

Informations professionnelles :
Activité : Professeur des Ecoles
Secteur : Enseignement

Informations forums :
Inscription : mai 2009
Messages : 502
Points : 700
Points : 700
Attention ici :
Code :
if ($resultat_valide_final[] = 1)
= : opérateur d'affectation et non de comparaison.
Mais
Code :
if ($resultat_valide_final[] == 1)
ne marcherait pas non plus puisque il manque l'entrée du tableau $resultat_valide_final[?].

Attention ici également :
Code :
$resultat_invalide_final[] = 100 - $resultat_valide_final;
où $resultat_valide_final n'existe pas, ce qui existe, c'est $resultat_valide_final[0], $resultat_valide_final[1],....

J'opterais donc plutôt pour ceci :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
while($row = pg_fetch_assoc($result))
{
	$kbarticleid[] = $row['kbarticleid'];
	$pourcentage_valide[] = $row['pourcentage_valide'];
	$pourcentage_invalide[] = $row['pourcentage_invalide'];
 
	if ($row['pourcentage_valide']+$row['pourcentage_invalide']==0)
	{
		$resultat_valide_final[]=100;
		$resultat_invalide_final[] = 0;
	}
	else
	{
		$resultat_valide_final = $row['pourcentage_valide']/$row['pourcentage_valide']+$row['pourcentage_invalide'];
 
		$resultat_valide_final[] = $resultat_valide_final;
		$resultat_invalide_final[] = 100 - $resultat_valide_final;
	}
}
Fred
micetf est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/10/2009, 10h14   #5
Futur Membre du Club
 
Inscription : novembre 2008
Messages : 62
Détails du profil
Informations forums :
Inscription : novembre 2008
Messages : 62
Points : 15
Points : 15
Citation:
Envoyé par micetf Voir le message
Attention ici :
Code :
if ($resultat_valide_final[] = 1)
= : opérateur d'affectation et non de comparaison.
Mais
Code :
if ($resultat_valide_final[] == 1)
ne marcherait pas non plus puisque il manque l'entrée du tableau $resultat_valide_final[?].

Attention ici également :
Code :
$resultat_invalide_final[] = 100 - $resultat_valide_final;
où $resultat_valide_final n'existe pas, ce qui existe, c'est $resultat_valide_final[0], $resultat_valide_final[1],....

J'opterais donc plutôt pour ceci :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
while($row = pg_fetch_assoc($result))
{
	$kbarticleid[] = $row['kbarticleid'];
	$pourcentage_valide[] = $row['pourcentage_valide'];
	$pourcentage_invalide[] = $row['pourcentage_invalide'];
 
	if ($row['pourcentage_valide']+$row['pourcentage_invalide']==0)
	{
		$resultat_valide_final[]=100;
		$resultat_invalide_final[] = 0;
	}
	else
	{
		$resultat_valide_final = $row['pourcentage_valide']/$row['pourcentage_valide']+$row['pourcentage_invalide'];
 
		$resultat_valide_final[] = $resultat_valide_final;
		$resultat_invalide_final[] = 100 - $resultat_valide_final;
	}
}
Fred
J'ai toujours un problème à cette ligne :
Code :
$resultat_valide_final[] = $resultat_valide_final;
Il me met ce message d'erreur : Warning: Cannot use a scalar value as an array
cycy_88 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/10/2009, 19h20   #6
Membre chevronné
 
Avatar de micetf
 
Homme Fred
Professeur des Ecoles
Inscription : mai 2009
Messages : 502
Détails du profil
Informations personnelles :
Nom : Homme Fred
Localisation : France, Rhône (Rhône Alpes)

Informations professionnelles :
Activité : Professeur des Ecoles
Secteur : Enseignement

Informations forums :
Inscription : mai 2009
Messages : 502
Points : 700
Points : 700
Je n'aurais pas dû appeler ma variable ($resultat_valide_final) comme le tableau ($resultat_valide_final[]).
Appelons-là $rvf.
Voici la correction :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
while($row = pg_fetch_assoc($result))
{
	$kbarticleid[] = $row['kbarticleid'];
	$pourcentage_valide[] = $row['pourcentage_valide'];
	$pourcentage_invalide[] = $row['pourcentage_invalide'];
 
	if ($row['pourcentage_valide']+$row['pourcentage_invalide']==0)
	{
		$resultat_valide_final[]=100;
		$resultat_invalide_final[] = 0;
	}
	else
	{
		$rvf = $row['pourcentage_valide']/$row['pourcentage_valide']+$row['pourcentage_invalide'];
 
		$resultat_valide_final[] = $rvf;
		$resultat_invalide_final[] = 100 - $rvf;
	}
}
Fred
micetf est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +1. Il est actuellement 00h18.


 
 
 
 
Partenaires

Hébergement Web