IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Difficulté de concaténation ou mauvaise définition d'array


Sujet :

Langage PHP

  1. #21
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2009
    Messages
    185
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2009
    Messages : 185
    Points : 79
    Points
    79
    Par défaut
    Le pire dans tout ça, c'est que tu as raison !
    Effectivement, çà marche
    Donc le code complet si cela intéresse:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    	function record($compte) 	{
     
    	$codeHtml = '';
    	$montant = array();
    	$ninput = 3;
     	$label = ucfirst(htmlspecialchars($compte['label']));
    	$input = $compte['input'];
     
    	if (!isset($_POST['Button'])) {
                $codeHtml = "<tr>\r\n";
                $codeHtml .= '<td class="libelle">'.$label.'</label></td>';
                for ($i = 0; $i < $ninput; $i++) {
                    $codeHtml .= '<td class="montant"><input type="text" size="6" name="'.$input . '[]" /></td>';
                }
                $codeHtml .= "</tr>\r\n";					
    	}
       elseif ($_POST['Button'] == 'Enregistrer')
    	{
         $codeHtml = "<tr>\r\n";
         $codeHtml .= '<td class="libelle">'. $label .'</td>';
         for ($i = 0; $i < $ninput ; $i++) {
             $montant = isset($_POST[$input][$i]) ? $_POST[$input][$i] : '';
             $codeHtml .= '<td class="montant">'. htmlspecialchars ($montant).'</td>';
    		}
      	}
    	else // ($_POST['Button'] == 'Modifier')
    	{
    	}
    	return $codeHtml;
    }
    Encore merci à toi (et à ta patience) Sabotage.
    (Je pense qu'il faut trouver un titre parlant pour ce genre de problème car je n'avais rien trouvé sur GOO...., je t'en laisse l'initiative en tant que modérateur.)

  2. #22
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2009
    Messages
    185
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2009
    Messages : 185
    Points : 79
    Points
    79
    Par défaut
    Bonjour SABOTAGE, et bonjour le forum,
    En fait, si le sujet est bien résolu, il me vient une autre question.
    Comment dois je faire pour réaliser le total de mes "éléments" d'array ? ce que je souhaite c'est additionner tous les montants 0, tous les montants 1 et tous les montants 2. J'ai essayé
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    $total = array();
      foreach ($montant as $row) {
      $total[$i] += $row;
      }
    et dans la partie html :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    <tr>
    <td>TOTAL</td>
    <td><?php echo $total[0]; ?>
    <td><?php echo $total[1]; ?>
    <td><?php echo $total[2]; ?>
    </tr>
    Sans succès. Qu'en pensez vous ?

  3. #23
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    La fonction doit alors regrouper toutes les catégories :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    <?php	
    function record($fields) 	{
     
    	$codeHtml = '';
    	$montant = array();
    	$total = array();
    	$ninput = 3;
     
    	foreach($fields as $compte) {
     
    		$label = htmlspecialchars($compte['label']);
    		$input = $compte['input'] ;
     
    		if (!isset($_POST['Button'])) {
    			$codeHtml = "<tr>\r\n";
    			$codeHtml .= '<td class="libelle">'.$label.'</label></td>';
    			for ($i = 0; $i < $ninput, $i++) {
    				$codeHtml .= '<td class="montant"><input type="text" size="6" name="'.$input . '[]" /></td>';
    			}
    			$codeHtml .= "</tr>\r\n";					
    		}
    		elseif ($_POST['Button'] == 'Enregistrer') 	{
    			$codeHtml = "<tr>\r\n";
    			$codeHtml .= '<td class="libelle">'. $label .'</td>';
    			for ($i = 0; $i < $ninput, $i++) {
    				if (isset($_POST[$input][$i])) {
    					$montant = intval($_POST[$input][$i]);
    					@$total[$i] += $montant
    				}
    				else {
    					$montant = '';
    				}
    				$codeHtml .= '<td class="montant">'.$montant.'</td>';
    			}
     
    			$codeHtml = "<tr>\r\n";
    			$codeHtml .= '<td class="libelle">Total</td>';
    			for ($i = 0; $i < $ninput, $i++) {
    				$montant = isset($total[$i]) ? $total[$i] : '';
    				$codeHtml .= '<td class="montant">'.$montant.'</td>';
    			}
    			$codeHtml .= "</tr>\r\n";
    		}
     
    	}
    	return $codeHtml;
    }
     
    $fields = array(
      array('label'=>'fournitures', 'input'=>'fournitures'),
      array('label'=>'autres approvisionnements', 'input'=>'autres_appro'),
      array('label'=>'achat de matériel', 'input'=>'achat_materiel'),
      array('label'=>'achats non stockés', 'input'=>'achat_nonstockes')
      );
     
    echo record($fields);
    N'oubliez pas de consulter les FAQ PHP et les cours et tutoriels PHP

  4. #24
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2009
    Messages
    185
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2009
    Messages : 185
    Points : 79
    Points
    79
    Par défaut
    Bonsoir,
    En testant ton code, sur le formulaire je n'ai que le dernier compte présent (achats non stockés)
    et mais c'est sans doute normal, si je 'poste' le libelle du compte est remplacé par 'total'.
    Je te reposte le code au complet complet.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    <?php	
    function record($fields) 	{
     
    	$codeHtml = '';
    	$montant = array();
    	$total = array();
    	$ninput = 3;
     
    	foreach($fields as $compte) {
     
    		$label = htmlspecialchars($compte['label']);
    		$input = $compte['input'] ;
     
    		if (!isset($_POST['Button'])) {
    			$codeHtml = "<tr>\r\n";
    			$codeHtml .= '<td class="libelle">'.$label.'</label></td>';
    			for ($i = 0; $i < $ninput ; $i++) {
    				$codeHtml .= '<td class="montant"><input type="text" size="6" name="'.$input . '[]" /></td>';
    			}
    			$codeHtml .= "</tr>\r\n";					
    		}
    		elseif ($_POST['Button'] == 'Enregistrer') 	{
    			$codeHtml = "<tr>\r\n";
    			$codeHtml .= '<td class="libelle">'. $label .'</td>';
    			for ($i = 0; $i < $ninput ; $i++) {
    				if (isset($_POST[$input][$i])) {
    					$montant = intval($_POST[$input][$i]);
    					@$total[$i] += $montant ;
    				}
    				else {
    					$montant = '';
    				}
    				$codeHtml .= '<td class="montant">'.$montant.'</td>';
    			}
     
    			$codeHtml = "<tr>\r\n";
    			$codeHtml .= '<td class="libelle">Total</td>';
    			for ($i = 0; $i < $ninput ; $i++) {
    				$montant = isset($total[$i]) ? $total[$i] : '';
    				$codeHtml .= '<td class="montant">'.$montant.'</td>';
    			}
    			$codeHtml .= "</tr>\r\n";
    		}
     
    	}
    	return $codeHtml;
    }
     
    $fields = array(
      array('label'=>'fournitures', 'input'=>'fournitures'),
      array('label'=>'autres approvisionnements', 'input'=>'autres_appro'),
      array('label'=>'achat de matériel', 'input'=>'achat_materiel'),
      array('label'=>'achats non stockés', 'input'=>'achat_nonstockes')
      );
     
    ?>
    		<div class="col-sm-8 blog-main">
    <?php
    if( !isset( $_POST['Button'] ) ) {
    ?>				
    			<form id="" name="frmFraisgeneraux" method="post" action="index.php?page=4">
    <?php 
    }
    ?>				
    			<table class="table-width" cellspacing='0'>
    <?php	   		
    			echo '<tr><td colspan="4">ACHATS</td></tr>';
    			echo record($fields);		
                            if (!isset($_POST['Button'])) { 	
    ?>
    					<td colspan="4" style="text-align:center;">					
    					<input type="submit" class="btn btn-primary" name="Button" value="Enregistrer">
    					</td>
    <?php
    }
    else {
    ?>
    				<tr>
    					<td>TOTAL</td>
    					<td></td>
    					<td></td>
    					<td></td>
    				</tr>	
    				<tr>
    					<td colspan="4" style="text-align:center;">					
    					<input type="submit" class="btn btn-primary" name="Button" value="Modifier">
    					</td>	
    <?php
    }
    ?>						
    				</tr>			
    				<thead>
    					<th>Compte</th>
    					<th class="entete">Année N</th>
    					<th class="entete">Année N+1(P)</th>
    					<th class="entete">Année N+2(P)</th>
    				</thead>			
    			</table>			
    <?php
    if( !isset( $_POST['Button'] ) ) { echo '</form>'; }
    ?>
    		</div><!-- /.blog-main --> 
    	</div><!-- /.row --> 
    </div><!-- /.container -->

Discussions similaires

  1. Mauvaise récupération d'array avec serialize
    Par GYK dans le forum Langage
    Réponses: 8
    Dernier message: 28/06/2012, 18h49
  2. [MySQL] concaténer 2 colonnes d'un array
    Par bigs3232 dans le forum PHP & Base de données
    Réponses: 4
    Dernier message: 19/12/2011, 20h35
  3. Difficulté à sommer des numpy.arrays
    Par Loïc B. dans le forum Général Python
    Réponses: 4
    Dernier message: 09/12/2009, 10h34
  4. [XL-2002] Type définit par l'utilisateur et array
    Par christophe_fr dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 17/11/2009, 14h39
  5. Définition d'un produit scalaire pour boost::array
    Par vmfa-2 sven dans le forum Boost
    Réponses: 3
    Dernier message: 13/09/2009, 21h27

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo