probleme de recuperation de variable en php
Hors ligneTopvision Le 06/02/2015 à 18:05 Profil de Topvision Configuration de Topvision

BONJOUR,
j'ai une application de gestion de stock et je voudrai lorsque je clique sur valider qu'il recupere les quatite entrees de chaque produit et l'inserre dans la table mouvement pour que je puisse savoir la quanté sortante de chanque produit.



voici la base
[sql]
-- phpMyAdmin SQL Dump
-- version 3.3.9.2
-- http://www.phpmyadmin.net
--
-- Serveur: localhost
-- Généré le : Dim 01 Février 2015 à 01:22
-- Version du serveur: 5.5.9
-- Version de PHP: 5.3.5



SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";




/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;



--
-- Base de données: `administrateur`
--
CREATE DATABASE `administrateur` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `administrateur`;



-- --------------------------------------------------------



--
-- Structure de la table `Mouvement`
--



CREATE TABLE `Mouvement` (
`idarticles` int(11) NOT NULL,
`qt_entree` int(11) NOT NULL,
`qt_sortie` int(11) NOT NULL,
`q_rest` int(11) NOT NULL,
PRIMARY KEY (`idarticles`),
KEY `qt_entree` (`qt_entree`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;



--
-- Contenu de la table `Mouvement`
--




-- --------------------------------------------------------



--
-- Structure de la table `articles`
--



CREATE TABLE `articles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`designation` varchar(35) NOT NULL,
`reference` tinytext NOT NULL,
`categorie` varchar(35) NOT NULL,
`quantite` int(11) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`pachat` float NOT NULL,
`put` float NOT NULL,
PRIMARY KEY (`id`),
KEY `quantite` (`quantite`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;



--
-- Contenu de la table `articles`
--



INSERT INTO `articles` VALUES(1, 'webcam', 'pixel2', 'info', 0, '0000-00-00 00:00:00', 12000, 12500);
INSERT INTO `articles` VALUES(2, 'souris', 'sansfil', 'info', 0, '0000-00-00 00:00:00', 7000, 8000);
INSERT INTO `articles` VALUES(3, 'ecrant plat 19', 'samsung 2prise rca + hdi', 'informatique', 0, '0000-00-00 00:00:00', 75000, 75000);
INSERT INTO `articles` VALUES(4, 'woofer qtec', 'radio 5db rouge-noir', 'informatique', 0, '0000-00-00 00:00:00', 25000, 25000);
INSERT INTO `articles` VALUES(5, 'unite centrale', 'HP dual core 1.8Ghz RAM 3Ghz disque dur 250Giga, couleur gris et noir', 'informatique', 25, '2015-01-25 13:52:43', 55000, 75000);
INSERT INTO `articles` VALUES(6, 'web', '5px rouge-vert', 'informatique', 80, '2015-01-30 19:59:27', 3500, 5000);
INSERT INTO `articles` VALUES(7, 'souris sans fil', 'wireless 25cm noir & gris', 'Informatique', 40, '2015-01-30 19:59:27', 7500, 1000);
INSERT INTO `articles` VALUES(8, 'woofer', '2db 4 baffles noir & gris', 'informatique', 75, '2015-01-30 20:01:40', 22500, 35000);
INSERT INTO `articles` VALUES(9, 'wooffer', '50db 8 baffles noir et biege', 'informatique', 80, '2015-01-30 20:01:40', 32500, 50000);



-- --------------------------------------------------------



--
-- Structure de la table `user`
--



CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`login` varchar(15) NOT NULL,
`pass` varchar(25) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;



--
-- Contenu de la table `user`
--



INSERT INTO `user` VALUES(1, 'admin', 'admin');



--
-- Contraintes pour les tables exportées
--



--
-- Contraintes pour la table `Mouvement`
--
ALTER TABLE `Mouvement`
ADD CONSTRAINT `Mouvement_ibfk_1` FOREIGN KEY (`idarticles`) REFERENCES `articles` (`id`),
ADD CONSTRAINT `Mouvement_ibfk_2` FOREIGN KEY (`qt_entree`) REFERENCES `articles` (`quantite`);
[/sql]



la page index ou il ya liste des articles
[php]
<?php
session_start();



try {
$DB = new PDO('mysql: host=localhost;dbname=administrateur','root','');
$DB->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
echo 'la base de donnnée n\'est pas disponible, merci de réessayer plus tard';
}

$sql = 'SELECT * FROM articles';

?>



<html>
<head><title>AUTH</title></head>
<body>

<table border="1px solid black" align="center">
<caption>LISTE DES ARTICLES DISPONIBLES</caption>
<tbody>
<tr>
<td>id</td><td>stock</td><td>libelle</td><td>prix</td><td>Ajout</td>
</tr>

<?php
try {
$req = $DB->query($sql);
while ($data = $req->fetch(PDO::FETCH_OBJ))
{
?>

<tr>
<td> <?php echo $data->id; ?></td>
<td> <?php echo $data->quantite ; ?></td>
<td> <?php echo $data->designation ; ?></td>
<td><?php echo $data->put ; ?> </td>
<td><a href="panier.php?action=ajout&amp;a=<?php echo $data->id;?>&amp;s=<?php echo $data->quantite;?>&amp;l=<?php echo $data->designation;?>&amp;q=QUANTITEPRODUIT&amp;p=<?php echo $data->put;?>" onclick="window.open(this.href, '',
'toolbar=no, location=no, directories=no, status=yes, scrollbars=yes, resizable=yes, copyhistory=no, width=600, height=350'); return false;">+</a>
</td>
</tr>
<?php
}
}



catch (PDOException $e)
{
echo 'requete plantée';
}
?>
</tbody>
</table>



</body>
</html>



[/php]



pages panier ou se trouveront les articles selectionnees
[php]
<?php
session_start();



include_once("fonctions-panier.php");



$erreur = false;



$action = (isset($_POST['action'])? $_POST['action']: (isset($_GET['action'])? $_GET['action']:null )) ;
if($action !== null)
{
if(!in_array($action,array('ajout', 'suppression', 'refresh')))
$erreur=true;



//récuperation des variables en POST ou GET
$a = (isset($_POST['a'])? $_POST['a']: (isset($_GET['a'])? $_GET['a']:null )) ;
$s = (isset($_POST['s'])? $_POST['s']: (isset($_GET['s'])? $_GET['s']:null )) ;
$l = (isset($_POST['l'])? $_POST['l']: (isset($_GET['l'])? $_GET['l']:null )) ;
$p = (isset($_POST['p'])? $_POST['p']: (isset($_GET['p'])? $_GET['p']:null )) ;
$q = (isset($_POST['q'])? $_POST['q']: (isset($_GET['q'])? $_GET['q']:null )) ;
//Suppression des espaces verticaux
$l = preg_replace('#\v#', '',$l);
//On verifie que $p soit un float
$p = floatval($p);



//On traite $q qui peut etre un entier simple ou un tableau d'entier

if (is_array($q)){
$QteArticle = array();
$i=0;
foreach ($q as $contenu){
$QteArticle[$i++] = intval($contenu);
}
}
else
$q = intval($q);

}



if (!$erreur){
switch($action){
Case "ajout":
ajouterArticle($l,$q,$p);
break;



Case "suppression":
supprimerArticle($l);
break;



Case "refresh" :
for ($i = 0 ; $i < count($QteArticle) ; $i++)
{
modifierQTeArticle($_SESSION['panier']['libelleProduit'][$i],round($QteArticle[$i]));
}
break;



Default:
break;
}
}



echo '<?xml version="1.0" encoding="utf-8"?>';?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
<title>Votre liste d'achat</title>
</head>
<body>



<form method="post" action="panier.php">
<table style="width: 400px" border=1px solid gray>
<tr>
<td colspan="4" align='center'>Votre liste d'acchat</td>
</tr>
<tr>
<td>Libellé</td>
<td>Quantité</td>
<td>Prix Unitaire</td>
<td>Action</td>
</tr>




<?php
if (creationpanier())
{
$nbArticles=count($_SESSION['panier']['libelleProduit']);
if ($nbArticles <= 0)
echo "<tr><td>Votre panier est vide </ td></tr>";
else
{
for ($i=0 ;$i < $nbArticles ; $i++)
{
echo "<tr>";
echo "<td>".htmlspecialchars($_SESSION['panier']['libelleProduit'][$i])."</ td>";
echo "<td><input type=\"text\" size=\"4\" name=\"q[]\" value=\"".htmlspecialchars($_SESSION['panier']['qteProduit'][$i])."\"/></td>";
echo "<td>".htmlspecialchars($_SESSION['panier']['prixProduit'][$i])."</td>";
echo "<td><a href=\"".htmlspecialchars("panier.php?action=suppression&l=".rawurlencode($_SESSION['panier']['libelleProduit'][$i]))."\">XX</a></td>";
echo "</tr>";
}



echo "<tr><td colspan=\"2\"> </td>";
echo "<td colspan=\"2\">";
echo "TotalHT : ".MontantGlobal()." Fcfa";
echo "<br>";
echo " TVA : ".tva()." Fcfa<br>";
echo " TotalTTC : ".plustva()." Fcfa</br>";
echo "</td></tr>";



echo "<tr><td colspan=\"4\" align=\"center\">";
echo "<input type=\"submit\" value=\"Rafraichir\"/>";
echo "<input type=\"hidden\" name=\"action\" value=\"refresh\"/>";



echo "</td></tr>";
}
}
?>
</table>
<input type="submit" name="valider" value="envoyer">
<a href="insert.php" target="parent" name="valider">Valider</a>
</form>
</body>
</html>
[/php]



page insert sert a inserrer les nouvelle quantitee entrees
[php]
<?php
session_start();
if(isset($_POST['valider'])) {
try {
$DB = new PDO('mysql: host=localhost;dbname=administrateur','root','');
$DB->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
//---PDO::ERRMODE_WARNING) affiche les erreurs
}
catch(PDOException $e){

echo 'la base de donnnée n\'est pas disponible, merci de réessayer plus tard';
}
$qt_rest = ($s - $q);
$d = array('$a','$s','$q','$qt_rest');
$req = $DB->prepare('INSERT INTO mouvement(idarticles,qt_entree,qt_sortie,qt_rest) VALUE (?,?,?,?)');
$req->execute($d);



}



?>
[/php]



mes fonctions panier
[php]
<?php



/**
* Verifie si le panier existe, le créé sinon
* @return booleen
*/
function creationpanier(){
if (!isset($_SESSION['panier'])){
$_SESSION['panier']=array();
$_SESSION['panier']['libelleProduit'] = array();
$_SESSION['panier']['qteProduit'] = array();
$_SESSION['panier']['prixProduit'] = array();
$_SESSION['panier']['verrou'] = false;
}
return true;
}




/**
* Ajoute un article dans le panier
* @param string $libelleProduit
* @param int $qteProduit
* @param float $prixProduit
* @return void
*/
function ajouterArticle($libelleProduit,$qteProduit,$prixProduit){



//Si le panier existe
if (creationpanier() && !isVerrouille())
{
//Si le produit existe déjà on ajoute seulement la quantité
$positionProduit = array_search($libelleProduit, $_SESSION['panier']['libelleProduit']);



if ($positionProduit !== false)
{
$_SESSION['panier']['qteProduit'][$positionProduit] += $qteProduit ;
}
else
{
//Sinon on ajoute le produit
array_push( $_SESSION['panier']['libelleProduit'],$libelleProduit);
array_push( $_SESSION['panier']['qteProduit'],$qteProduit);
array_push( $_SESSION['panier']['prixProduit'],$prixProduit);
}
}
else
echo "Un problème est survenu veuillez contacter l'administrateur du site.";
}





/**
* Modifie la quantité d'un article
* @param $libelleProduit
* @param $qteProduit
* @return void
*/
function modifierQTeArticle($libelleProduit,$qteProduit){
//Si le panier éxiste
if (creationpanier() && !isVerrouille())
{
//Si la quantité est positive on modifie sinon on supprime l'article
if ($qteProduit > 0)
{
//Recharche du produit dans le panier
$positionProduit = array_search($libelleProduit, $_SESSION['panier']['libelleProduit']);



if ($positionProduit !== false)
{
$_SESSION['panier']['qteProduit'][$positionProduit] = $qteProduit ;
}
}
else
supprimerArticle($libelleProduit);
}
else
echo "Un problème est survenu veuillez contacter l'administrateur du site.";
}



/**
* Supprime un article du panier
* @param $libelleProduit
* @return unknown_type
*/
function supprimerArticle($libelleProduit){
//Si le panier existe
if (creationpanier() && !isVerrouille())
{
//Nous allons passer par un panier temporaire
$tmp=array();
$tmp['libelleProduit'] = array();
$tmp['qteProduit'] = array();
$tmp['prixProduit'] = array();
$tmp['verrou'] = $_SESSION['panier']['verrou'];



for($i = 0; $i < count($_SESSION['panier']['libelleProduit']); $i++)
{
if ($_SESSION['panier']['libelleProduit'][$i] !== $libelleProduit)
{
array_push( $tmp['libelleProduit'],$_SESSION['panier']['libelleProduit'][$i]);
array_push( $tmp['qteProduit'],$_SESSION['panier']['qteProduit'][$i]);
array_push( $tmp['prixProduit'],$_SESSION['panier']['prixProduit'][$i]);
}



}
//On remplace le panier en session par notre panier temporaire à jour
$_SESSION['panier'] = $tmp;
//On efface notre panier temporaire
unset($tmp);
}
else
echo "Un problème est survenu veuillez contacter l'administrateur du site.";
}




/**
* Montant total du panier
* @return int
*/
function MontantGlobal(){
$total=0;
for($i = 0; $i < count($_SESSION['panier']['libelleProduit']); $i++)
{
$total += $_SESSION['panier']['qteProduit'][$i] * $_SESSION['panier']['prixProduit'][$i];
}
return number_format($total, 0, ',', ' ');
}




//MES FONCTIONS
/**
* Montant total du panier+tva
* @return int
*/
function tva(){

$totaltva=0;
for($i = 0; $i < count($_SESSION['panier']['libelleProduit']); $i++)
{
$totaltva += $_SESSION['panier']['qteProduit'][$i] * $_SESSION['panier']['prixProduit'][$i];
$tva =($totaltva / 100) * 18;
}
return number_format($tva, 0, ',', ' ');

}



function plustva(){

$totaltva=0;
for($i = 0; $i < count($_SESSION['panier']['libelleProduit']); $i++)
{
$totaltva += $_SESSION['panier']['qteProduit'][$i] * $_SESSION['panier']['prixProduit'][$i];
$tva =($totaltva / 100) * 18;
$totaltva2 = $totaltva + $tva ;
}



return number_format($totaltva2, 0, ',', ' ');
}




/**
* Fonction de suppression du panier
* @return void
*/
function supprimepanier(){
unset($_SESSION['panier']);
}



/**
* Permet de savoir si le panier est verrouillé
* @return booleen
*/
function isVerrouille(){
if (isset($_SESSION['panier']) && $_SESSION['panier']['verrou'])
return true;
else
return false;
}



/**
* Compte le nombre d'articles différents dans le panier
* @return int
*/
function compterArticles()
{
if (isset($_SESSION['panier']))
return count($_SESSION['panier']['libelleProduit']);
else
return 0;



}



?>



[/php]

Vous avez résolu votre problème avec VIC ? Faites-le savoir sur les réseaux sociaux !
Vulgarisation-informatique.com
Cours en informatique & tutoriels