Calcul dans un tableau

Eléphanteau du PHP | 37 Messages

18 avr. 2014, 18:09

Bonsoir à tous,

J'ai un tableau dans lequel sont effectué des calculs. Pour chaque colonne, il y a la somme a atteindre. Par exemple si nous prenons la colonne "Frais directs" le total ne doit pas excéder 10.000; alors j'ai besoin d'un code en php qui me permettra d'afficher un message d'erreur au cas ou les 10.000 seront dépassés.

Merci.

Voici le code de mon tableau :

<html>
<head>
  <script language="JavaScript">
  <!--
  
    function test_champ(champ) {
      expr_reg = /^\d*$/ ;
      // Ci-dessus : expression régulière qui match 0,1 ou plusieurs chiffres ;
      // de cette façon on vérifie que l'utilisateur entre bien un nombre entier
      if ( expr_reg.test(champ.value) ) {
        // c'est bien un nombre entier
        calcul_form() ;
      } else {
        // ce n'est pas un nombre entier
        alert ("Ce n'est par un nombre entier !") ;
        document.form1.elements[champ.name].value = "" ;   // on efface la valeur entrée erronée
        calcul_form() ;
      }
    }
    
    function calcul_form() {
      // le with ci-dessous permet d'abréger "document.forms.form1.p1.value" en "p1.value"
      with (document.forms.form1) {
        total1.value = fraisdirects1.value *1 + fraisindirects1.value *1 ;
        total2.value = fraisdirects2.value *1 + fraisindirects2.value *1 ;
        total3.value = fraisdirects3.value *1 + fraisindirects3.value *1 ;
		total4.value = fraisdirects4.value *1 + fraisindirects4.value *1 ;
		total5.value = fraisdirects5.value *1 + fraisindirects5.value *1 ;
		
        totalgeneral.value = total1.value *1 + total2.value *1 + total3.value *1 + total4.value *1 + total5.value *1;
           // Ai mis les *1 ci-dessus afin que les + fassent une somme arithmétique
           // et non pas une concaténation de chaînes de caractères
      }
    }
  // -->
  </script>
 
</head>

<body>
<form name="form1">
<table border="1" cellspacing="0" cellpadding="10">
	<tr bgcolor="#CCCCCC">
		<th>Ann&eacute;e</th>
		<th>Frais directs</th>
		<th>Frais indirects</th>
		<th>Total</th>
	</tr>	
	<tr>
		
		<th><input type=text value="<?=date('Y');?>" readonly></th>
		<th><input type="text" name="fraisdirects1"    style="text-align:right;"  onkeyup="test_champ(this)"/></th>
		<th><input type="text" name="fraisindirects1"  style="text-align:right;"  onkeyup="test_champ(this)"/></th>
		<th bgcolor="#CCCCCC"><input type="text" name="total1" style="text-align:right;" readonly /></th>
		</tr>
	<tr>
		<th><input type=text value="<?=date('Y')+1;?>" readonly></th>
		<th><input type="text" name="fraisdirects2"    style="text-align:right;"  onkeyup="test_champ(this)"/></th>
		<th><input type="text" name="fraisindirects2"  style="text-align:right;"  onkeyup="test_champ(this)"/></th>
		<th bgcolor="#CCCCCC"><input type="text" name="total2" style="text-align:right;" readonly /></th>
	</tr>
	<tr>
		<th><input type=text value="<?=date('Y')+2;?>" readonly></th>
		<th><input type="text" name="fraisdirects3"    style="text-align:right;"  onkeyup="test_champ(this)"/></th>
		<th><input type="text" name="fraisindirects3"  style="text-align:right;"  onkeyup="test_champ(this)"/></th>
		<th bgcolor="#CCCCCC"><input type="text" name="total3" style="text-align:right;" readonly /></th>
	</tr>
	<tr>
		<th><input type=text value="<?=date('Y')+3;?>" readonly></th>
		<th><input type="text" name="fraisdirects4"    style="text-align:right;"  onblur="test_champ(this)"/></th>
		<th><input type="text" name="fraisindirects4"  style="text-align:right;"  onkeyup="test_champ(this)"/></th>
		<th bgcolor="#CCCCCC"><input type="text" name="total4" style="text-align:right;" readonly /></th>
	</tr>
	<tr>
		<th><input type=text value="<?=date('Y')+4;?>" readonly></th>
		<th><input type="text" name="fraisdirects5"    style="text-align:right;"  onkeyup="test_champ(this)"/></th>
		<th><input type="text" name="fraisindirects5"  style="text-align:right;"  onkeyup="test_champ(this)"/></th>
		<th bgcolor="#CCCCCC"><input type="text" name="total5" style="text-align:right;" readonly /></th>
	</tr>
	<tr>
		<td bgcolor="#CCCCCC"><b>Total g&eacute;n&eacute;ral</b>&nbsp;&nbsp;&nbsp;
		<th bgcolor="#CCCCCC"><input type="text" name="totalfraisdirects"   value="10000" style="text-align:right; font-weight: bold;" readonly />
		<th bgcolor="#CCCCCC"><input type="text" name="totalfraisindirects" value="90000" style="text-align:right; font-weight: bold;" readonly />
		<th bgcolor="#CCCCCC"><input type="text" name="totalgeneral" style="text-align:right; font-weight: bold;" readonly />
	</th>
</table>
</form>
</body>
</html>
Bonne soirée

Avatar du membre
Modérateur PHPfrance
Modérateur PHPfrance | 8758 Messages

18 avr. 2014, 22:08

Salut,

Il faut faire la somme des cinq champs donc $_POST['champ1'] + $_POST['champ2'] etc
Ensuite un simple if.


@+
Il en faut peu pour être heureux ......