Commit dafd4e0f authored by Raulin's avatar Raulin

journal (Create, Read)

parent ed771044
......@@ -12,6 +12,7 @@
function resetForm() {
document.getElementById("id").value=""
document.getElementById("aliment").value = "";
document.getElementById("calories").value = "";
document.getElementById("cat").value = "";
document.getElementById("submit_button").value="Ajouter";
};
......@@ -20,7 +21,7 @@
</script>
<script type="text/javascript">
$(document).ready( function() {
$.getJSON('https://eden.imt-lille-douai.fr/~charlotte.raulin/Projet/api/afficher_aliments.php',
$.getJSON('https://eden.imt-lille-douai.fr/~charlotte.raulin/Projet/api/read_aliments.php',
function(json){
table = $('#tbody_aliment');
table.html('');
......@@ -29,17 +30,18 @@
$.each( json, function( key, val ) {
// table.append('<tr><td>'+val[0]+'</td><td>'+val[1]+'</td><td>'+val[2]+'</td><td><button '+
// 'onclick="onEdit('+val[0]+',\''+val[1]+'\',\''+val[2]+'\')">Modifier</button></td></tr>');
table.append('<tr><td>'+val[0]+'</td><td>'+val[1]+'</td><td>'+val[2]+'</td><td><button '+
'onclick="onEdit('+val[0]+',\''+val[1]+'\',\''+val[2]+'\')">Modifier</button></td><td><button '+
table.append('<tr><td>'+val[0]+'</td><td>'+val[1]+'</td><td>'+val[2]+'</td><td>'+val[3]+'</td><td><button '+
'onclick="onEdit('+val[0]+',\''+val[1]+'\',\''+val[2]+'\',\''+val[3]+'\')">Modifier</button></td><td><button '+
'onclick="onDelete('+val[0]+')">Supprimer</button></td></tr>');
});
}
);
});
function onEdit(id, nom, type) {
function onEdit(id, nom, cal, type) {
document.getElementById("id").value = id;
document.getElementById("aliment").value = nom;
document.getElementById("calories").value = cal;
document.getElementById("cat").value = type;
document.getElementById("submit_button").value='Modifier';
};
......@@ -70,6 +72,7 @@
<tr>
<th>ID</th>
<th>Aliment</th>
<th>Calories (KCal)</th>
<th>Type</th>
<th>Modifier</th>
<th>Supprimer</th>
......@@ -85,7 +88,7 @@
<table>
<tr>
<td>
<form id="form_aliment" action="api/ajouter_aliment.php" method="POST" >
<form id="form_aliment" action="api/create_aliment.php" method="POST" >
<div>
<input type="text" id="id" name="id" style="display : none">
</div>
......@@ -93,13 +96,19 @@
<label>Aliment</label>
<input type="text" id="aliment" name="aliment" required>
</div>
<div>
<label>Calories (KCal)</label>
<input type="number" id="calories" name="calories" required>
</div>
<div>
<label>Type d'aliment </label>
<input type="text" id="cat" name="cat" required>
<!-- <select id="type" size="1">
<option value="fruit"> Fruit </option>
<option value="legume"> Legume </option>
</select> -->
<!-- <input type="text" id="cat" name="cat" required> -->
<select id="cat" name="cat" size="1">
<option value="Fruit"> Fruit </option>
<option value="Legume"> Legume </option>
<option value="Sucrerie"> Sucrerie </option>
</select>
</div>
<div class="form-action-buttons">
......
<?php
try{
DEFINE('DB_USERNAME','charlotte.raulin');
DEFINE('DB_PASSWORD','MIZWqEaY');
DEFINE('DB_USERNAME','liang.wu');
DEFINE('DB_PASSWORD','oMmyE2aw');
DEFINE('DB_HOST','127.0.0.1');
DEFINE('DB_DATABASE','charlotte_raulin');
DEFINE('DB_DATABASE','liang_wu');
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$retour["success"]=true;
......
......@@ -3,7 +3,7 @@ require_once("template.php");
if(isset($_POST['id']) && $_POST['id']!=""){ //si l'id est renseigné il s'agit d'une modification
if(isset($_POST["aliment"]) && isset($_POST["cat"])){
$requete= "UPDATE `ALIMENTS` SET `NAME_ALIMENT` = '".$_POST['aliment']."', `TYPE_ALIMENT` = '".$_POST['cat']."' WHERE `ALIMENTS`.`ID_ALIMENT` = ".$_POST['id'];
$requete= "UPDATE `aliments` SET `aliment` = '".$_POST['aliment']."', `calories` = '".$_POST['calories']."', `type` = '".$_POST['cat']."' WHERE `aliments`.`id_aliment` = ".$_POST['id'];
$mysqli->query($requete);
$msg="L'aliment a bien été modifié";
}
......@@ -13,8 +13,8 @@ if(isset($_POST['id']) && $_POST['id']!=""){ //si l'id est renseigné il s'agit
}
else{ //sinon il s'agit d'un ajout
if(isset($_POST["aliment"]) && isset($_POST["cat"])){
$requete="INSERT INTO `ALIMENTS` (`ID_ALIMENT`,`NAME_ALIMENT`, `TYPE_ALIMENT`) VALUES (NULL, '" .$_POST['aliment'] . "', '" . $_POST['cat'] . "')";
if(isset($_POST["aliment"]) && isset($_POST["calories"]) && isset($_POST["cat"])){
$requete="INSERT INTO `aliments` (`id_aliment`,`aliment`, `calories`,`type`) VALUES (NULL, '" .$_POST['aliment'] . "', '" .$_POST['calories'] . "', '" . $_POST['cat'] . "')";
$mysqli->query($requete);
$msg="L'aliment a bien été ajouté";
} else{
......@@ -24,3 +24,4 @@ else{ //sinon il s'agit d'un ajout
echo $msg;
<?php
require_once("template.php");
// if($_POST['id']!=""){ //si l'id est renseigné il s'agit d'une modification
// if(isset($_POST["date"]) && isset($_POST["quantite"]) && isset($_POST["select_aliment"])){
// $requete= "UPDATE `journal` SET `aliment` = '".$_POST['aliment']."', `calories` = '".$_POST['calories']."', `type` = '".$_POST['cat']."' WHERE `aliments`.`id_aliment` = ".$_POST['id'];
// $mysqli->query($requete);
// $msg="L'aliment a bien été modifié";
// }
// else{
// $msg="Il manque des informations";
// }
// }
// else{ //sinon il s'agit d'un ajout
if(isset($_POST["date"]) && isset($_POST["quantite"]) && isset($_POST["select_aliment"])){
$idprovisoire=1;
$a='SELECT `id_aliment` FROM `aliments` WHERE `aliment` ="'.$_POST["select_aliment"].'"';
$b = $mysqli->query($a);
$c=$b->fetch_all();
$idaliment= $c[0][0];
$requete="INSERT INTO `journal` (`date`,`id`, `id_aliment`,`quantite`) VALUES ( '" .$_POST['date'] . "', '" .$idprovisoire . "', '" .$idaliment . "', '" . $_POST['quantite'] . "')";
$mysqli->query($requete);
$msg="L'élément a bien été ajouté au journal";
} else{
$msg="Il manque des informations";
}
//
echo $msg;
......@@ -2,7 +2,7 @@
require_once("template.php");
$requete= "DELETE FROM `ALIMENTS` WHERE `ALIMENTS`.`ID_ALIMENT` = ".$_POST['varID'];
$requete= "DELETE FROM `aliments` WHERE `aliments`.`id_aliment` = ".$_POST['varID'];
$mysqli->query($requete);
$msg= "L'aliment a été supprimé";
......
<?php
require_once("template.php");
$requete="DELETE FROM `journal` WHERE `journal`.`id_aliment` = ".$_POST['varIdAliment']."AND `journal`.`id` = ".$_POST['varIdUser'];
$result = $mysqli->query($requete);
<?php
require_once("template.php");
if(isset($_POST["Id"]) && isset($_POST["aliment"]) && isset($_POST["cat"])){
$requete= "UPDATE `ALIMENTS` SET `NAME_ALIMENT` = '".$_POST['aliment']."', `TYPE_ALIMENT` = '".$_POST['cat']."' WHERE `ALIMENTS`.`ID_ALIMENT` = 23";
$mysqli->query($requete);
$msg="L'aliment a bien été modifié";
} else{
$msg="Il manque des informations";
}
echo $msg;
\ No newline at end of file
<?php
require_once("template.php");
$requete="SELECT * FROM ALIMENTS";
$requete="SELECT * FROM aliments";
$result = $mysqli->query($requete);
$aliments=$result->fetch_all();
......
<?php
require_once("template.php");
$requete="SELECT `date`, `aliment`,`type`,`calories`,`quantite`, `journal`.`id_aliment`,`journal`.`id` FROM `journal` JOIN `aliments` ON `journal`.`id_aliment`=`aliments`.`id_aliment`";
$result = $mysqli->query($requete);
$journal=$result->fetch_all();
reponse_json($journal);
\ No newline at end of file
<?php
require_once("template.php");
$ali='coco';
$a='SELECT `id_aliment` FROM `aliments` WHERE `aliment` ="'.$ali.'"';
$b = $mysqli->query($a);
$c=$b->fetch_all();
echo $c[0][0];
?>
<head>
<meta charset="utf-8">
<meta charset="utf-8"/>
<script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#table_conso').DataTable();
$('#table_aliments').DataTable();
resetForm();
} );
function resetForm() {
document.getElementById("submit_button").value="Ajouter";
document.getElementById("quantite").value = "";
document.getElementById("date").value = "";
document.getElementById("mode").value = "";
};
};
</script>
<script type="text/javascript">
$(document).ready( function() {
$.getJSON('https://eden.imt-lille-douai.fr/~charlotte.raulin/Projet/api/read_aliments.php',
function(json){
options = $('#select_aliment');
options.html('');
// cf. https://api.jquery.com/jQuery.getJSON/
$.each( json, function( key, val ) {
options.append('<option value=\''+val[1]+'\'>'+val[1]+'</option>'); //liste de sélection de l'aliment dans le formulaire
});
}
);
});
$(document).ready( function() {
$.getJSON('https://eden.imt-lille-douai.fr/~charlotte.raulin/Projet/api/read_journal.php',
function(json){
table = $('#tbody_journal');
table.html('');
// cf. https://api.jquery.com/jQuery.getJSON/
$.each( json, function( key, val ) {
table.append('<tr><td>'+val[0]+'</td><td>'+val[1]+'</td><td>'+val[2]+'</td><td>'+val[3]+'</td><td>'+val[4]+'</td><td><button '+
'onclick="onEdit(\''+val[0]+'\',\''+val[1]+'\',\''+val[4]+'\')">Modifier</button></td><td><button '+
'onclick="onDelete('+val[5]+','+val[6]+')">Supprimer</button></td></tr>');
});
}
);
});
function onEdit(date, aliment, quantite) {
document.getElementById("date").value = date;
document.getElementById("aliment").value = aliment;
document.getElementById("quantite").value = quantite;
document.getElementById("mode").value = "modifier";
document.getElementById("submit_button").value='Modifier';
};
function onDelete(varIdUser, id_user){
var varIdAliment= id_aliment;
var varIdUser= id_user;
$.ajax({
url: 'api/delete_journal.php',
type: 'POST',
data: {'varIdAliment' : varIdAliment},
success: function(varIdAliment) {
alert(varIdAliment); // reponse contient l'affichage du fichier PHP (soit echo)
}
});
$.ajax({
url: 'api/delete_journal.php',
type: 'POST',
data: {'varIdUser' : varIdUser},
success: function(varIdUser) {
alert(varIdUser); // reponse contient l'affichage du fichier PHP (soit echo)
}
});
location.reload();
}
</script>
</head>
<section id="journal" style="display : flex; flex-direction : row; ">
<section id="journal_contenu" style="display : flex; flex-direction : row; ">
<div style="margin-right:100px">
<table id="table_journal" class="dataTable" style="color : #212529; ">
<div style="margin-right:100px">
<table id="table_conso" class="dataTable" style="color : #212529; ">
<thead>
<tr>
<th>Date</th>
<th>Aliment</th>
<th>Type</th>
<th>Date</th>
<th>Calories (KCal)</th>
<th>Quantité</th>
<th>Modifier</th>
<th>Supprimer</th>
</tr>
</thead>
<tbody id="t">
<tbody id="tbody_journal"><tbody>
</tbody>
</table>
<script src="crud.js"></script>
</div>
\ No newline at end of file
</div>
<div>
<table>
<tr>
<td>
<form id="form_journal" action="api/create_journal.php" method="POST" >
<div>
<label>Date</label>
<input type="date" id="date" name="date" required>
</div>
<div>
<label>Aliment</label>
<select id="select_aliment" name="select_aliment" size="1">
</select>
</div>
<div>
<label>Quantité</label>
<input type="number" id="quantite" name="quantite" required>
</div>
<div>
<input type="text" id="mode" name="mode" style="display : none" >
</div>
<div class="form-action-buttons">
<input type="submit" value="Ajouter" id='submit_button'>
</div>
</form>
</td>
</tr>
</table>
</div>
</section>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment