Sauvegarder les valeurs d'un array et les réutilisés
Bonjour, désolé pour le titre il est pas bien explicite.
J'expose mon problème je débute en php j'ai une page index.php ou il y a un formulaire qui représente une machine à café.
C'est des boutons radio, on à 4 choix : café, cappuccino, chocolat et thé.
Et un menu déroulant pour choisir le sucre 5 maximums.
J'ai aussi une page functions.php ou il y a notamment un array associatif ou je déclare les ingrédients.
Code:
$tIngStock=array('café'=> $stockCaf=10, 'chocolat' => $stockChoco=10, 'thé' => $stockThe=10, 'lait'=>$stockLait=10, 'sucre'=>$stockSugar=10, 'eau'=>$eau=10);
Donc quand je choisi disons café et que j'appuie sur envoyé la valeur de l'ingrédient 'café' diminue de 1 dose donc on passe de 10 à 9 pour le café et sur ma page index.php la fonction displayIngredient() affichera la valeur.
Donc ce que je voudrai faire c'est que quand je reprend par exemple café je passe de 9 à 8.
Mais malheureusement à chaque fois que je choisie de nouveau une boisson disons à nouveau café et bien le tableau est réinitialisé à 10 ce qui fait que je repasse à 9.
Pourriez-vous m'indiquer comment faire pour garder en mémoire les valeurs du tableau des ingrédients ?
Merci par avance.
Ps. : je n'ai pas encore appris les bases de données.
Voici ma page index.php
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
|
<?php
$time=date('d-m-y');//Création d'une variable $time dans celle-ci on met la chaîne renvoyé par la fonction date
?>
<!DOCTYPE html>
<html>
<head>
<title>Machine à café</title>
<meta charset="utf-8" />
<?php include 'functions.php' //inclusion du fichier functions.php dans l'index.php?>
</head>
<body>
<h2>Ma machine à café</h2>
<p>Date : <?= $time?></p> <!--Affichage de l'heure-->
<p>Somme d'argent insérée : 0</p>
<form method="post" action="index.php">
<p>Choisissez votre boisson :</p>
<input type="radio" name="drink" value="café" id="café"/>
<label for="café">café</label>
<input type="radio" name="drink" value="cappuccino" id="cappuccino"/>
<label for="cappuccino">cappuccino</label>
<input type="radio" name="drink" value="chocolat" id="chocolat"/>
<label for="chocolat">chocolat</label>
<input type="radio" name="drink" value="thé" id="thé"/>
<label for="thé">thé</label>
<br>
<label for="choiceSugar">Choisissez votre nombre de sucre Maximum 5</label><br><br>
<select name="choiceSugar" id="choiceSugar">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select><br><br>
<input type="submit" value="Envoyer"/>
</form>
<p></p>
<?php
displayDrinkAviable();
//Vérification avec isset si valeur $_POST est vide ou non
if(isset($_POST['drink'])){
//displayRecette();
echo "<p>Liste ingrédients pour : ".$_POST['drink']."</p>"; //Affichage de la boisson
prepareDrink($recette[$_POST['drink']], $_POST['choiceSugar']);//Envoi des paramètres $_POST dans la fonction prepareDrink
}
displayIngredient();
?>
</body>
</html> |
Et ma page function.php :
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
|
<?php
$drink=array('café', 'cappuccino', 'chocolat', 'thé');
//Tableau associatif pour les recettes des boissons stock l'array dans la variable $recette
$recette=array('café'=> array('café' => 1 , 'eau' => 3),
'cappuccino' => array('café' => 2, 'lait' => 2),
'chocolat' => array('chocolat' => 3, 'lait' => 3),
'thé' => array('thé'=> 2, 'eau' => 4));
//Tableau associatif gère le stock des ingrédients
$tIngStock=array('café'=> $stockCaf=10, 'chocolat' => $stockChoco=10, 'thé' => $stockThe=10, 'lait'=>$stockLait=10, 'sucre'=>$stockSugar=10, 'eau'=>$eau=10);
//fonction initStock initialise le stock à 100 doses;
/*function initStock()
{
global $tIngStock;
foreach($tIngStock as $key=>$val){
$tIngStock[$key]=100;
}
}*/
function displayDrinkAviable()
{
global $tIngStock, $recette, $drink;
$displayDrink=array(); $tmp=array();
$var=0;
$drinkLength = sizeof($drink);
foreach($tIngStock as $key=>$val){
if($key != 'sucre' && $tIngStock[$key] > 0){
foreach($recette as $key1=>$val1){
foreach($val1 as $key2=>$val2)
if($key===$key2){
{
$tmp[]=$key1;
}
}
}
}
}
for($j=0; $j < $drinkLength; $j++){
for($i=0; $i < sizeof($tmp); $i++){
if($drink[$j]==$tmp[$i]){
$var++;
}
}
if($var >= 1){
$displayDrink[]=$drink[$j];
}
}
$displayDrinkLength = sizeof($displayDrink);
if($displayDrinkLength)
{
echo "Boisson disponible : $displayDrinkLength <br>";
for($i=0; $i<sizeof($displayDrink); $i++){
echo "$displayDrink[$i] <br>";
}
}
else
{ echo "Aucune boisson disponible."; }
}
//prepareDrink prend 2 paramettres : le tableau à 2 dimensions $recette et le nombre de sucre et affiche la recette de la boisson et le sucre
function prepareDrink($tab, $sucre)
{
global $tIngStock;
$strRecet="";
foreach($tab as $key => $val)
{
$strRecet .= $val.' doses de '.$key.' et <br>';
$tIngStock[$key]-=$val;
}
$tIngStock['sucre']-=$sucre;
echo $strRecet .= $sucre." sucre(s)";
}
function displayIngredient()
{
global $tIngStock;
echo "<pre>";
print_r($tIngStock);
echo "</pre>";
}
?> |