Skip to content
Snippets Groups Projects
Commit 92fb4095 authored by ALGLAVE Ivan's avatar ALGLAVE Ivan
Browse files

js -> php -> js communication

parent 45202806
No related branches found
No related tags found
No related merge requests found
......@@ -11,10 +11,12 @@
<ul>
<?php
include "Donnees.inc.php";
foreach($Recettes as $r)
include "scripts/Functions.php";
$ar = getRecipes(['Malibu'], [], 10, true, true);
foreach($ar as $r)
{
echo '<li>' . $r['titre'] . '</li>';
echo '<li>' . $r['recipe']['titre'] . ' (unsatisfied ingredients = ' . $r['score'] . ')' . '</li>';
}
?>
......
......@@ -4,25 +4,39 @@
<title>Drinkpedia</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="css/Hierarchy.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript" src="scripts/DataQuery.js"></script>
</head>
<body id="body">
<select id="recipeSelection">
<?php
include "Donnees.inc.php";
for($i = 0; $i < count($Recettes); $i++)
{
echo '<option value="' . $i . '">' . $Recettes[$i]['titre'] . '</option>';
}
?>
</select>
<button onclick="notifyParent(document.getElementById('recipeSelection').value);">Show</button>
<ul id="origin_list"></ul>
<!-- JAVASCRIPT -->
<script>
let n_list_origin = document.getElementById('origin_list');
//let ingredients = getIngredients();
function detailIngredients()
{
let d = document.getElementsByClassName('detailed');
let ud = document.getElementsByClassName('undetailed');
for(let el in ud)
{
while (el.firstChild) {
el.removeChild(el.firstChild);
}
}
for(let el in d)
{
let c = getIngredients(el.value, function (data, status)
{
});
}
}
function notifyParent(e)
{
var data = {
......
function getRecipes(wanted_ingredients = [], unwanted_ingredients = [], max_unsatisfied = 0, allow_missing = true, allow_unwanted = true, callback)
{
$.post("scripts/DataQuery.php", {'functionname': 'getRecipe', 'arguments': [wanted_ingredients, unwanted_ingredients, max_unsatisfied, allow_missing, allow_unwanted]}, callback);
}
function getIngredients(parent_category = 'Aliment', callback)
{
$.post("scripts/DataQuery.php", {'functionname': 'getIngredients', 'arguments': [parent_category]}, callback);
}
\ No newline at end of file
<?php
include "Functions.php";
$funcName = $_POST['functionname'];
if($funcName == 'test')
{
echo 'test';
}
else if($funcName == 'getRecipes')
{
echo getRecipes($_POST['arguments'][0], $_POST['arguments'][1], $_POST['arguments'][2], $_POST['arguments'][3], $_POST['arguments'][4]);
}
else if($funcName == 'getIngredients')
{
echo getIngredients($_POST['arguments'][0]);
}
else
{
echo 'error - unknown func';
}
?>
\ No newline at end of file
<?php
function recipeContains($recipe, $ingredient)
{
foreach($recipe['index'] as $ing) if($ing === $ingredient) return true;
return false;
}
function getRecipes($wanted_ingredients, $unwanted_ingredients, $max_unsatisfied, $allow_missing, $allow_unwanted)
{
include "../Donnees.inc.php";
$Recipes = '';
for($i = 0; $i < count($Recettes); $i++)
{
$recipe = $Recettes[$i];
$score = 0;
$valid = true;
foreach($wanted_ingredients as $w_ing)
{
if(!recipeContains($recipe, $w_ing))
{
if(!$allow_missing)
{
$valid = false;
break;
}
$score++;
}
}
if($valid)
{
foreach($unwanted_ingredients as $uw_ing)
{
if(recipeContains($recipe, $uw_ing))
{
if(!$allow_unwanted)
{
$valid = false;
break;
}
$score++;
}
}
if($valid)
{
if($score <= $max_unsatisfied)
{
$Recipes .= $i . ':' . $score . '|';
}
}
}
}
return $Recipes;
}
function getIngredients($parent_category)
{
include "../Donnees.inc.php";
$res = '';
$ingredients = $Hierarchie[$parent_category]['sous-categorie'];
foreach($ingredients as $i)
{
$res .= $i . '|';
}
return $res;
}
?>
\ No newline at end of file
<?php
echo $_POST['data'];
?>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment