Advertisement
anasmansouri

Untitled

Nov 13th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.25 KB | None | 0 0
  1. <!DOCTYPE HTML>
  2. <HTML>
  3.  
  4. <head>
  5.     <title>ingredients</title>
  6. </head>
  7.  
  8. <body>
  9.     <form>
  10.  
  11.     </form>
  12.  
  13.     <!-----------------------------------inputs--------------------------------->
  14.     <form name="myform" method='get'>
  15.         <input name="search" type="text" placeholder="what in your fridge "><br>
  16.         <input name="time" type="number" placeholder="type number of minutes ">
  17.         <!-----------------------------------list des choix--------------------------------->
  18.         <p>enter option : </p>
  19.         <select name='option'>
  20.             <option value="none">none</option>
  21.             <option value="vegetarian">vegetarian</option>
  22.             <option value="vegan">vegan</option>
  23.             <option value="cheap">cheap</option>
  24.             <option value="veryHealthy">very healthy</option>
  25.             <option value="dairyFree">dairy free</option>
  26.             <option value="whole30">whole 30</option>
  27.         </select>
  28.         <button type="submit" name="button" value="submit">Search</button>
  29.         </p>
  30.  
  31.         <p>
  32.             <!-----------------------------------php--------------------------------->
  33.             <?php
  34.             if (isset($_GET['button'])) //------------------->si le bouton est cliquee
  35.             {
  36.  
  37.                 //------------------->test sur les separateur de notre ingredients
  38.                 $str = str_replace(',', ',+', $_GET['search']);
  39.                 $str = str_replace('-', ',+', $str);
  40.                 $str = str_replace(';', ',+', $str);
  41.                 $str = str_replace('/', ',+', $str);
  42.                 $str = str_replace('  ', ',+', $str);
  43.                 $str = str_replace(' ', ',+', $str);
  44.                 $str = str_replace(',+,+', ',+', $str);
  45.                 $str = str_replace(',+,+,+', ',+', $str);
  46.                 //------------------->pswd de notre api
  47.                 $apiKey = "8ce7c20403914b92900a12bff524a951";
  48.                 //------------------->variables
  49.                 $stockIndex = array(); //------------------->tableaux des index de notre plat filtrees
  50.                 $stockID = array(); // ---------------------->tableaux des Id des recipes preferer
  51.                 $number = 2; //nombre max des recettes a afficher
  52.                 $filterOfUser = "" . $_GET["option"]; //-------------------> string de notre choix de la list select soit vegan,vegetarian...
  53.                 $maxTime = (int) $_GET["time"]; //-------------------> le temps en minutes
  54.                 $stockRecipe = array();
  55.                 //------------------->un objet de notre plat a partir des ingredients
  56.                 $url1 = "https://api.spoonacular.com/recipes/findByIngredients?apiKey=" . $apiKey . "&ingredients=" . $str . "&number=" . $number;
  57.                 $data1 = file_get_contents($url1);
  58.                 $characters1 = json_decode($data1, false);
  59.                 //------------------->attribution des id des recettes qui satisfait notre choix dans le tableau $stockId
  60.  
  61.                 for ($i = 0; $i < 2; $i++) {
  62.                     //-------------------> un objet qui contient notre plat en bouclant sur character1->id
  63.                     $url2 = "https://api.spoonacular.com/recipes/" . $characters1[$i]->id . "/information?apiKey=" . $apiKey . "&includeNutrition=false";
  64.                     $data2 = file_get_contents($url2);
  65.                     $characters2 = json_decode($data2, false);
  66.                     //-------------------> test sur list et nbr de minutes
  67.  
  68.                     if ((($filterOfUser == 'none') || ($characters2->$filterOfUser)) && (($characters2->readyInMinutes <= $maxTime) || (empty($maxTime)))) {
  69.                         array_push($stockIndex, $i);
  70.                         array_push($stockID, $characters2->id);
  71.                         array_push($stockRecipe, $characters2->instructions);
  72.                     }
  73.                 }
  74.                 //------------------->pour chaque id on affiche
  75.                 foreach ($stockIndex as $i) {
  76.                     //------------------->titre et image de notre plat
  77.                     echo "<h1>" . $characters1[$i]->title . "</h1><br><img src=" . $characters1[$i]->image . " width='400' /><br>";
  78.                     //-------------------> les ingredient utiliser de votre choix
  79.                     echo '<h4>ingredients :</h4>  ';
  80.                     $url3 = "https://api.spoonacular.com/recipes/" . $stockID[$i] . "/ingredientWidget.json?apiKey=" . $apiKey;
  81.                     $data3 = file_get_contents($url3);
  82.                     $ObjetIngredient = json_decode($data3, false);
  83.                     // 9lab 3la les ingredients
  84.                     foreach ($ObjetIngredient->ingredients as $ingredient) {
  85.                         echo ' <br><img src="https://spoonacular.com/cdn/ingredients_100x100/' . $ingredient->image . '" width="30" /><br>' . $ingredient->name . '<br>quantity : ' . $ingredient->amount->metric->value . ' ' . $ingredient->amount->metric->unit;
  86.                     }
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.                     echo '<h4>recipe : </h4>';
  94.                     echo '<section style="background-color:pink ; border:3px solid red ; padding:15px ; margin:5px">';
  95.                     echo $stockRecipe[$i];
  96.                     echo '</section>';
  97.                 }
  98.             }
  99.  
  100.             ?>
  101.         </p>
  102. </body>
  103.  
  104. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement