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 :

Tableau multidimensionnel en PHP


Sujet :

Langage PHP

  1. #1
    Candidat au Club
    Homme Profil pro
    Technicien réseaux et télécoms
    Inscrit en
    Octobre 2015
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Technicien réseaux et télécoms
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Octobre 2015
    Messages : 5
    Points : 3
    Points
    3
    Par défaut Tableau multidimensionnel en PHP
    Bonjour,

    Je débute encore en PHP et j'essai de coder une page qui calcul automatiquement le coût de location d'une baie informatique par rapport aux données saisie dans un formulaire donc sans base de donnée.

    J'ai déjà bien avancé mais je bloque sur la sélection et le calcul d'un coût sur un tableau multidimensionnel de la forme suivante :



    Le visiteur sélectionne le type de connexion et le débit puis je souhaiterais ressortir le coût sans faire de if / elseif qui multiplierais mon code x 10.

    Pour le moment, j'ai le code suivant qui me permet d'afficher tous les tarifs et de parcourir le tableau (j'ai d'ailleurs séparé le tableau en deux) :

    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
    <?php
        // Récupération des variables
        $acceslocal = $_POST['acceslocal'];
        $typeservicelocal = $_POST['typeservicelocal'];
        if ($typeservicelocal == "0") {
            echo "<br>Montant accès au réseau local : 0 Fxpf";
        } elseif ($typeservicelocal == "prioritaire") {
        // Déclaration du tableau des valeurs : La cle 1 = numéro de ligne, cle 2  = débit et cle 3 = tarif mensuel
        $tabprio = array(
        1 => array('debit' => '512', 'prix' => '5161'),
        2 => array('debit' => '1', 'prix' => '10080'),
        3 => array('debit' => '2', 'prix' => '20160'),
        4 => array('debit' => '3', 'prix' => '30240'),
        5 => array('debit' => '4', 'prix' => '40320'),
        6 => array('debit' => '5', 'prix' => '50400'),
        7 => array('debit' => '6', 'prix' => '60480'),
        8 => array('debit' => '7', 'prix' => '70560'),
        9 => array('debit' => '8', 'prix' => '80640'),
        10 => array('debit' => '9', 'prix' => '90720'),
        11 => array('debit' => '10', 'prix' => '100800'),
        12 => array('debit' => '20', 'prix' => '163800'),
        13 => array('debit' => '30', 'prix' => '170200'),
        14 => array('debit' => '40', 'prix' => '201600'),
        15 => array('debit' => '50', 'prix' => '252000'),
        16 => array('debit' => '100', 'prix' => '340200'),
        );
     
       foreach($tabprio as $cle1 => $valeur1)
    {
      echo "<br>Abonnement n°:" . $cle1 . "<br />";
     
        foreach ($valeur1 as $cle2=>$valeur2)
     
        {
          echo $cle2 . " : " . $valeur2 . "<br />\n";
        }
    }  
    }
    ?>
    Je veux bien un peu d'aider sur le sujet, je patauge depuis un certain temps dessus ...

    Merci beaucoup.
    Johan.

  2. #2
    Expert éminent sénior
    Avatar de rawsrc
    Homme Profil pro
    Dev indep
    Inscrit en
    Mars 2004
    Messages
    6 142
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Dev indep

    Informations forums :
    Inscription : Mars 2004
    Messages : 6 142
    Points : 16 545
    Points
    16 545
    Billets dans le blog
    12
    Par défaut
    Salut,

    poste aussi le code source du formulaire stp

  3. #3
    Candidat au Club
    Homme Profil pro
    Technicien réseaux et télécoms
    Inscrit en
    Octobre 2015
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Technicien réseaux et télécoms
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Octobre 2015
    Messages : 5
    Points : 3
    Points
    3
    Par défaut
    Bonjour,

    Le code du formulaire :

    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
    <br>--- ACCES INTERNET ---<br>
     
    Accès réseau local : <select name="acceslocal">
    <option value="0">-</option>
    <option value="512">512 Kbits/s</option>
    <option value="1">1 Mbits/s</option>
    <option value="1">2 Mbits/s</option>
    <option value="2">3 Mbits/s</option>
    <option value="3">4 Mbits/s</option>
    <option value="5">5 Mbits/s</option>
    <option value="6">6 Mbits/s</option>
    <option value="7">7 Mbits/s</option>
    <option value="8">8 Mbits/s</option>
    <option value="9">9 Mbits/s</option>
    <option value="10">10 Mbits/s</option>
    <option value="20">20 Mbits/s</option>
    <option value="30">30 Mbits/s</option>
    <option value="40">40 Mbits/s</option>
    <option value="50">50 Mbits/s</option>
    <option value="100">100 Mbits/s</option>
    </select><br>
     
    Type de service : <select name="typeservicelocal">
    <option value="0">-</option>
    <option value="besteffort">"Best effort"</option>
    <option value="prioritaire">Prioritaire</option>
    </select><br><br>
     
    Accès réseau internationnal : <select name="accesinter">
    <option value="0">-</option>
    <option value="512">512 Kbits/s</option>
    <option value="1">1 Mbits/s</option>
    <option value="1">2 Mbits/s</option>
    <option value="2">3 Mbits/s</option>
    <option value="3">4 Mbits/s</option>
    <option value="5">5 Mbits/s</option>
    <option value="6">6 Mbits/s</option>
    <option value="7">7 Mbits/s</option>
    <option value="8">8 Mbits/s</option>
    <option value="9">9 Mbits/s</option>
    <option value="10">10 Mbits/s</option>
    </select><br>
     
    Type de service : <select name="typeserviceinter">
    <option value="0">0</option>
    <option value="besteffort">"Best effort"</option>
    <option value="prioritaire">Prioritaire</option>
    </select><br>
    Merci.

  4. #4
    Expert éminent sénior
    Avatar de rawsrc
    Homme Profil pro
    Dev indep
    Inscrit en
    Mars 2004
    Messages
    6 142
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Dev indep

    Informations forums :
    Inscription : Mars 2004
    Messages : 6 142
    Points : 16 545
    Points
    16 545
    Billets dans le blog
    12
    Par défaut
    Salut,

    avec les maigres éléments que tu as donné, j'opterai pour quelque chose dans ce genre :
    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
    <?php
     
    $price_list = [
        '0'     => ['premium' => 0,      'best' => 0],
        '512 K' => ['premium' => 5161,   'best' => 2150],
        '1 M'   => ['premium' => 10080,  'best' => 4200],
        '2 M'   => ['premium' => 20160,  'best' => 8400],
        '3 M'   => ['premium' => 30240,  'best' => 12600],
        '4 M'   => ['premium' => 40320,  'best' => 16800],
        '5 M'   => ['premium' => 50400,  'best' => 21000],
        '6 M'   => ['premium' => 60480,  'best' => 25200],
        '7 M'   => ['premium' => 70560,  'best' => 29400],
        '8 M'   => ['premium' => 80640,  'best' => 33600],
        '9 M'   => ['premium' => 90720,  'best' => 37800],
        '10 M'  => ['premium' => 100800, 'best' => 42000],
        '20 M'  => ['premium' => 163800, 'best' => 54600],
        '30 M'  => ['premium' => 170200, 'best' => 60400],
        '40 M'  => ['premium' => 201600, 'best' => 70200],
        '50 M'  => ['premium' => 252000, 'best' => 84000],
        '100 M' => ['premium' => 340200, 'best' => 113400]
    ];
     
    $offers = [0 => true, 'best' => '"Best effort"', 'premium' => 'Prioritaire'];
     
    $local_offer = (isset($_POST['local_offer'], $offers[$_POST['local_offer']]))     ? $_POST['local_offer'] : false;
    $local_speed = (isset($_POST['local_speed'], $price_list[$_POST['local_speed']])) ? $_POST['local_speed'] : false;
     
    $inter_offer = (isset($_POST['inter_offer'], $offers[$_POST['inter_offer']]))     ? $_POST['inter_offer'] : false;
    $inter_speed = (isset($_POST['inter_speed'], $price_list[$_POST['inter_speed']])) ? $_POST['inter_speed'] : false;
     
    if (($local_speed !== false) and ($inter_speed !== false) and ($local_offer !== false) and ($inter_offer !== false))
    {
        $total_price = 0;
     
        if ($local_offer != 0)
            $total_price += $price_list[$local_speed][$local_offer];
     
        if ($inter_offer != 0)
            $total_price += $price_list[$inter_speed][$inter_offer];
     
        echo 'Votre prix est de ', $total_price, ' Fxpf';
    }
    ?>
    <br>--- ACCES INTERNET ---<br>
    Accès réseau local :
    <select name="local_speed">
        <option value="0" selected="selected">-</option>
        <?php foreach (array_slice($price_list, 1) as $k => $v) { ?>
        <option value="<?=$k?>"><?=$k?>bits/s</option>
        <?php } ?>
    </select><br>
     
    Type de service :
    <select name="local_offer">
        <option value="0" selected="selected">-</option>
        <?php foreach (array_slice($offers, 1) as $k => $v) { ?>
        <option value="<?=$k?>"><?=$v?></option>
        <?php } ?>
    </select><br><br>
     
    Accès réseau internationnal :
    <select name="inter_speed">
        <option value="0" selected="selected">-</option>
        <?php foreach (array_slice($price_list, 1, 11) as $k => $v) { ?>
        <option value="<?=$k?>"><?=$k?>bits/s</option>
        <?php } ?>
    </select><br>
     
    Type de service :
    <select name="inter_offer">
        <option value="0" selected="selected">-</option>
        <?php foreach (array_slice($offers, 1) as $k => $v) { ?>
        <option value="<?=$k?>"><?=$v?></option>
        <?php } ?>
    </select><br>
    Je n'ai rien testé du tout donc, je croise les doigts...

  5. #5
    Candidat au Club
    Homme Profil pro
    Technicien réseaux et télécoms
    Inscrit en
    Octobre 2015
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Technicien réseaux et télécoms
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Octobre 2015
    Messages : 5
    Points : 3
    Points
    3
    Par défaut
    Merci pour ta réponse.

    Je peux tester ton code mais c'est sur le même fichier non ? Car moi, j'ai une page form.php (mon formulaire) et simulation.php (le traitement des données).

    Johan.

  6. #6
    Expert éminent sénior
    Avatar de rawsrc
    Homme Profil pro
    Dev indep
    Inscrit en
    Mars 2004
    Messages
    6 142
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Dev indep

    Informations forums :
    Inscription : Mars 2004
    Messages : 6 142
    Points : 16 545
    Points
    16 545
    Billets dans le blog
    12
    Par défaut
    Salut,

    j'ai fait en fonction de ce que tu as posté.
    Tu peux tout à fait séparer le script en parties selon tes besoins.

Discussions similaires

  1. Gestion tableau multidimensionnel php
    Par stellou74 dans le forum PHP & Base de données
    Réponses: 4
    Dernier message: 19/02/2014, 18h36
  2. Tableau multidimensionnel en PHP
    Par mra_ch dans le forum Langage
    Réponses: 1
    Dernier message: 30/10/2009, 20h05
  3. Nombre de dimensions d'un tableau multidimensionnel
    Par Bruno75 dans le forum VB 6 et antérieur
    Réponses: 3
    Dernier message: 08/07/2005, 10h03
  4. type de donnée tableau multidimensionnel
    Par opheliegomes dans le forum Débuter
    Réponses: 2
    Dernier message: 03/02/2005, 12h29
  5. [langage] tableau multidimensionnel
    Par totox17 dans le forum Langage
    Réponses: 3
    Dernier message: 03/12/2002, 15h58

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