Page 1 sur 1

Enregistrement automatique de champs

Posté : 24 mai 2013, 15:32
par cash01
Bonjour a toutes et tous,

Voici ma question,

J'ai une formule de type "if" dans ma page php qui me donne un résultat, et je voudrai que ce resultat s'enregistre dans ma base de donnée.

Comment faut il faire ??

Mon code :
<?php


// Connexion à la base de donnée
mysql_connect("localhost", "xxxx", "xxxxxx");
mysql_select_db('xxxx');


// Le nom de notre table
$tablename = 'personnes';


// Tri sur colonne
$tri_autorises = array('date', 'client','ndecde','montant','pose','sm','fournisseur','allegro','livprevue','livconfirmee','statut');
$order_by = in_array($_GET['order'],$tri_autorises) ? $_GET['order'] : 'date'; 'client';'ndecde';'montant';'pose';'sm';'fournisseur';'allegro';'livprevue';'livconfirmee';'statut';


// Sens du tri
$order_dir = isset($_GET['inverse']) ? 'DESC' : 'ASC';


// Préparation de la requête
$sql = "
	SELECT *
	FROM {$tablename}
	ORDER BY {$order_by} {$order_dir}
";
$result = mysql_query($sql);


// Notre fonction qui affiche les liens
function sort_link($text, $order=false)
{
	global $order_by, $order_dir;

	if(!$order)
		$order = $text;

	$link = '<a href="?order=' . $order;
	if($order_by==$order && $order_dir=='ASC')
		$link .= '&inverse=true';
	$link .= '"';
	if($order_by==$order && $order_dir=='ASC')
		$link .= ' class="order_asc"';
	elseif($order_by==$order && $order_dir=='DESC')
		$link .= ' class="order_desc"';
	$link .= '>' . $text . '</a>';

	return $link;
}


// Affichage
?>
<style type="text/css">
a.order_asc,
a.order_desc:hover { 
	padding-right:15px;
	background:transparent url(s_asc.png) right no-repeat;
}
a.order_desc,
a.order_asc:hover {
	padding-right:15px;
	background:transparent url(s_desc.png) right no-repeat;
}
</style>

<table width="100%" border="1">
 
	<tr>
    
	<th><?php echo sort_link('DATE', 'date') ?></th>
	<th><?php echo sort_link('CLIENT', 'client') ?></th>
	<th><?php echo sort_link('N° DE CDE', 'ndecde') ?></th>
        <th><?php echo sort_link('MONTANT', 'montant') ?></th>
        <th><?php echo sort_link('POSE', 'pose') ?></th>
        <th><?php echo sort_link('S/M', 'sm') ?></th>
        <th><?php echo sort_link('FOURNISSEUR', 'fournisseur') ?></th>
        <th><?php echo sort_link('ALLEGRO', 'allegro') ?></th>
        <th><?php echo sort_link('LIVRAISON PREVUE', 'livprevue') ?></th>
        <th><?php echo sort_link('LIVRAISON CONFIRMEE', 'livconfirmee') ?></th>
        <th><?php echo sort_link('COMMENTAIRES', 'commentaires') ?></th>
        <th><?php echo sort_link('STATUT', 'statut') ?></th>
	<th><?php echo sort_link('ETAT', '-') ?></th>
        

</th> 
	</tr>

 
    
<?php while( $row=mysql_fetch_assoc($result) ) : ?>
  

<tr>

<?php    
switch ($row['statut']){ 
case 'EN COURS': 
$couleur='orange';//a remplacer par le code hexa de la couleur voulue #....... 
break; 

case 'LIVRABLE': 
$couleur='green';//a remplacer par le code hexa de la couleur voulue #....... 
break; 

case 'SOLDEE': 
$couleur='gray';//a remplacer par le code hexa de la couleur voulue #....... 
break; 

case 'RETARD': 
$couleur='red';//a remplacer par le code hexa de la couleur voulue #....... 
break; 

default: 
$couleur= 'white';//a remplacer par le code hexa de la couleur par defaut 
break; 

} 
echo "<tr style=\"background-color:$couleur;\"> 

<td width=\"100\">"; 
echo $row['date']; 
echo "</td> 

<td width=\"150\">"; 
echo $row['client']; 
echo "</td> 

<td width=\"100\">"; 
echo $row['ndecde']; 
echo "</td> 

<td width=\"100\">"; 
echo $row['montant']; 
echo "</td> 

<td width=\"150\">"; 
echo $row['pose']; 
echo "</td> 

<td width=\"50\">"; 
echo $row['sm']; 
echo "</td> 

<td width=\"250\">"; 
echo $row['fournisseur']; 
echo "</td> 

<td width=\"100\">"; 
echo $row['allegro']; 
echo "</td> 

<td width=\"100\">"; 
echo $row['livprevue']; 
echo "</td> 

<td width=\"100\">"; 
echo $row['livconfirmee']; 
echo "</td> 

<td width=\"150\">"; 
echo $row['commentaires']; 
echo "</td> 

<td width=\"100\">"; 
echo $row['statut']; 
echo "</td> 

  

<td width=\"200\">"; 
		  
// Voici ma formule :	

$madate=$row['livprevue'];
$madate1=$row['livconfirmee'];
$madate=explode("-",$madate);
$madate1=explode("-",$madate1);
$madate=mktime(0,0,0,$madate[1],$madate[2],$madate[0]);
$madate1=mktime(0,0,0,$madate1[1],$madate1[2],$madate1[0]);
$dateAujourdhui  = mktime(0, 0, 0, date("m"),   date("d"),   date("Y"));

if($madate<$dateAujourdhui&&$row['statut']='SOLDEE'){
    echo "<span style='color:black;'>SOLDEE</span>";
	
}elseif($madate1>$dateAujourdhui){
	echo "<span style='color:green;'>EN PRODUCTION</span>";	
	
}elseif($madate==$dateAujourdhui&&$row['statut']=='SOLDEE'){
    echo "<span style='color:black;'>SOLDEE</span>";
	
}elseif($madate==$dateAujourdhui&&$row['statut']=='LIVRABLE'){
    echo "<span style='color:white;'>LIVRABLE</span>";
		
}elseif($madate<$dateAujourdhui&&$row['statut']=='LIVRABLE'){
    echo "<span style='color:white;'>LIVRABLE</span>";	
	
}elseif($madate==$dateAujourdhui){
	echo "<span style='color:blue;'>DERNIER JOUR</span>";
	
}elseif($madate<$dateAujourdhui){
	echo "<span style='color:red;'>EN RETARD</span>";

}elseif($madate>$dateAujourdhui&&$row['statut']=='LIVRABLE'){
    echo "<span style='color:white;'>LIVRABLE</span>";		

}else{
    echo "<span style='color:black;'>EN COURS</span>";
}


?>		 
</tr>

<?php endwhile ?>
</table>

Merci de vos réponses :D

Re: Enregistrement automatique de champs

Posté : 25 mai 2013, 15:51
par moogli
salut,


Merci d'utiliser les balises bbcode ciblées, par exemple PHP plutôt que code c'est quand même plus facile a lire ;)

ensuite l'insertion en base c'est avec insert.

A toi de voir ce que tu veux ajouter en base et dans quel table, parce que la c'est pas claire il n'y a que de l'affichage.

plus d'info sur le sql : http://sqlpro.developpez.com


@+

Re: Enregistrement automatique de champs

Posté : 25 mai 2013, 22:06
par cash01
<?php


// Connexion à la base de donnée
mysql_connect("localhost", "xxxx", "xxxxxx");
mysql_select_db('xxxx');


// Le nom de notre table
$tablename = 'personnes';


// Tri sur colonne
$tri_autorises = array('date', 'client','ndecde','montant','pose','sm','fournisseur','allegro','livprevue','livconfirmee','statut');
$order_by = in_array($_GET['order'],$tri_autorises) ? $_GET['order'] : 'date'; 'client';'ndecde';'montant';'pose';'sm';'fournisseur';'allegro';'livprevue';'livconfirmee';'statut';


// Sens du tri
$order_dir = isset($_GET['inverse']) ? 'DESC' : 'ASC';


// Préparation de la requête
$sql = "
        SELECT *
        FROM {$tablename}
        ORDER BY {$order_by} {$order_dir}
";
$result = mysql_query($sql);


// Notre fonction qui affiche les liens
function sort_link($text, $order=false)
{
        global $order_by, $order_dir;

        if(!$order)
                $order = $text;

        $link = '<a href="?order=' . $order;
        if($order_by==$order && $order_dir=='ASC')
                $link .= '&inverse=true';
        $link .= '"';
        if($order_by==$order && $order_dir=='ASC')
                $link .= ' class="order_asc"';
        elseif($order_by==$order && $order_dir=='DESC')
                $link .= ' class="order_desc"';
        $link .= '>' . $text . '</a>';

        return $link;
}


// Affichage
?>
<style type="text/css">
a.order_asc,
a.order_desc:hover { 
        padding-right:15px;
        background:transparent url(s_asc.png) right no-repeat;
}
a.order_desc,
a.order_asc:hover {
        padding-right:15px;
        background:transparent url(s_desc.png) right no-repeat;
}
</style>

<table width="100%" border="1">
 
        <tr>
    
        <th><?php echo sort_link('DATE', 'date') ?></th>
        <th><?php echo sort_link('CLIENT', 'client') ?></th>
        <th><?php echo sort_link('N° DE CDE', 'ndecde') ?></th>
        <th><?php echo sort_link('MONTANT', 'montant') ?></th>
        <th><?php echo sort_link('POSE', 'pose') ?></th>
        <th><?php echo sort_link('S/M', 'sm') ?></th>
        <th><?php echo sort_link('FOURNISSEUR', 'fournisseur') ?></th>
        <th><?php echo sort_link('ALLEGRO', 'allegro') ?></th>
        <th><?php echo sort_link('LIVRAISON PREVUE', 'livprevue') ?></th>
        <th><?php echo sort_link('LIVRAISON CONFIRMEE', 'livconfirmee') ?></th>
        <th><?php echo sort_link('COMMENTAIRES', 'commentaires') ?></th>
        <th><?php echo sort_link('STATUT', 'statut') ?></th>
        <th><?php echo sort_link('ETAT', '-') ?></th>
        

</th> 
        </tr>

 
    
<?php while( $row=mysql_fetch_assoc($result) ) : ?>
  

<tr>

<?php    
switch ($row['statut']){ 
case 'EN COURS': 
$couleur='orange';//a remplacer par le code hexa de la couleur voulue #....... 
break; 

case 'LIVRABLE': 
$couleur='green';//a remplacer par le code hexa de la couleur voulue #....... 
break; 

case 'SOLDEE': 
$couleur='gray';//a remplacer par le code hexa de la couleur voulue #....... 
break; 

case 'RETARD': 
$couleur='red';//a remplacer par le code hexa de la couleur voulue #....... 
break; 

default: 
$couleur= 'white';//a remplacer par le code hexa de la couleur par defaut 
break; 

} 
echo "<tr style=\"background-color:$couleur;\"> 

<td width=\"100\">"; 
echo $row['date']; 
echo "</td> 

<td width=\"150\">"; 
echo $row['client']; 
echo "</td> 

<td width=\"100\">"; 
echo $row['ndecde']; 
echo "</td> 

<td width=\"100\">"; 
echo $row['montant']; 
echo "</td> 

<td width=\"150\">"; 
echo $row['pose']; 
echo "</td> 

<td width=\"50\">"; 
echo $row['sm']; 
echo "</td> 

<td width=\"250\">"; 
echo $row['fournisseur']; 
echo "</td> 

<td width=\"100\">"; 
echo $row['allegro']; 
echo "</td> 

<td width=\"100\">"; 
echo $row['livprevue']; 
echo "</td> 

<td width=\"100\">"; 
echo $row['livconfirmee']; 
echo "</td> 

<td width=\"150\">"; 
echo $row['commentaires']; 
echo "</td> 

<td width=\"100\">"; 
echo $row['statut']; 
echo "</td> 

  

<td width=\"200\">"; 
                  
// Voici ma formule :   

$madate=$row['livprevue'];
$madate1=$row['livconfirmee'];
$madate=explode("-",$madate);
$madate1=explode("-",$madate1);
$madate=mktime(0,0,0,$madate[1],$madate[2],$madate[0]);
$madate1=mktime(0,0,0,$madate1[1],$madate1[2],$madate1[0]);
$dateAujourdhui  = mktime(0, 0, 0, date("m"),   date("d"),   date("Y"));

if($madate<$dateAujourdhui&&$row['statut']='SOLDEE'){
    echo "<span style='color:black;'>SOLDEE</span>";
        
}elseif($madate1>$dateAujourdhui){
        echo "<span style='color:green;'>EN PRODUCTION</span>"; 
        
}elseif($madate==$dateAujourdhui&&$row['statut']=='SOLDEE'){
    echo "<span style='color:black;'>SOLDEE</span>";
        
}elseif($madate==$dateAujourdhui&&$row['statut']=='LIVRABLE'){
    echo "<span style='color:white;'>LIVRABLE</span>";
                
}elseif($madate<$dateAujourdhui&&$row['statut']=='LIVRABLE'){
    echo "<span style='color:white;'>LIVRABLE</span>";  
        
}elseif($madate==$dateAujourdhui){
        echo "<span style='color:blue;'>DERNIER JOUR</span>";
        
}elseif($madate<$dateAujourdhui){
        echo "<span style='color:red;'>EN RETARD</span>";

}elseif($madate>$dateAujourdhui&&$row['statut']=='LIVRABLE'){
    echo "<span style='color:white;'>LIVRABLE</span>";          

}else{
    echo "<span style='color:black;'>EN COURS</span>";
}


?>               
</tr>

<?php endwhile ?>
</table>
 

Re: Enregistrement automatique de champs

Posté : 25 mai 2013, 23:50
par moogli
on va faire simple :
- la divination c'est pas ici (pas de Mme irama et autre consœur).
- tu as un série de if / elseif / else en fin de code c'est de ça que tu parle ?
- tu veux stocker en base ce qui sera affiché (ou le choix correspondant) en base ?


tu n'a même un début d'insertion, il faut que tu commencer par écrire la requête ensuite un simple mysql_query suffit.
-Ton premier if comporte une affectation il ne se comportera donc surement pas comme tu le souhaite.

tu peux facilement factoriser ton code.


Par exemple ainsi
<?php
$soldee = '<span style="color:black;">SOLDEE</span>';
$livrable = '<span style="color:white;">LIVRABLE</span>';
$retard = '<span style="color:red;">EN RETARD</span>';
$lastDay = '<span style="color:blue;">DERNIER JOUR</span>';
$enCours = '<span style="color:black;">EN COURS</span>';
$prod = '<span style="color:green;">EN PRODUCTION</span>';
if ($madate < $dateAujourdhui) {
    if ($row['statut'] == 'SOLDEE') {
        echo $soldee;
    } elseif ($row['statut'] == 'LIVRABLE') {
        echo $livrable;
    } else {
        echo $retard;
    }

} elseif ($madate1 > $dateAujourdhui) {
    if ($row['statut'] == 'LIVRABLE') {
        echo $livrable;
    } else {
        echo $prod;
    }

} elseif ($madate == $dateAujourdhui &&){
    if ($row['statut'] == 'SOLDEE') {
        echo $soldee;
    } elseif ($row['statut'] == 'LIVRABLE') {
        echo $livrable;
    } else {
        echo $lastDay;
    }
} else {
    echo $enCours;
}

@+

Re: Enregistrement automatique de champs

Posté : 26 mai 2013, 18:20
par cash01
- la divination c'est pas ici (pas de Mme irama et autre consœur).
je me suis peut être mal exprimer mais je vais essayer de me faire comprendre.
tu as un série de if / elseif / else en fin de code c'est de ça que tu parle ?
Oui c'est ce bout de code que je parle.
tu veux stocker en base ce qui sera affiché (ou le choix correspondant) en base ?
Exactement je veux stocker dans ma base ce qui sera affiché.
tu n'a même un début d'insertion,
C'est justement ma rechercher, car je ne sais pas par où et comment commencer.

merci

Re: Enregistrement automatique de champs

Posté : 26 mai 2013, 22:13
par moogli

Re: Enregistrement automatique de champs

Posté : 28 mai 2013, 20:40
par cash01
J'ai ajouter un "insert to" juste avant la fonction if , mais rien ne s'enregistre.


<?php


// Connexion à la base de donnée
mysql_connect("localhost", "xxxxx", "xxxxxx");
mysql_select_db('xxxxx');


// Le nom de notre table
$tablename = 'pose';


// Tri sur colonne
$tri_autorises = array('date', 'client','forfait','numero','rtpayer','retourtheoriquert','retourreelrt','envoidevis','statut','commande',);
$order_by = in_array($_GET['order'],$tri_autorises) ? $_GET['order'] : 'date'; 'client';'forfait';'dispoproduits';'rtpayer';'retourtheoriquert';'retourreelrt';'envoidevis';'statut';'commande';


// Sens du tri
$order_dir = isset($_GET['inverse']) ? 'DESC' : 'ASC';


// Préparation de la requête
$sql = "
	SELECT *
	FROM {$tablename}
	ORDER BY {$order_by} {$order_dir}
";
$result = mysql_query($sql);


// Notre fonction qui affiche les liens
function sort_link($text, $order=false)
{
	global $order_by, $order_dir;

	if(!$order)
		$order = $text;

	$link = '<a href="?order=' . $order;
	if($order_by==$order && $order_dir=='ASC')
		$link .= '&inverse=true';
	$link .= '"';
	if($order_by==$order && $order_dir=='ASC')
		$link .= ' class="order_asc"';
	elseif($order_by==$order && $order_dir=='DESC')
		$link .= ' class="order_desc"';
	$link .= '>' . $text . '</a>';

	return $link;
}


// Affichage
?>
<style type="text/css">
a.order_asc,
a.order_desc:hover { 
	padding-right:15px;
	background:transparent url(s_asc.png) right no-repeat;
}
a.order_desc,
a.order_asc:hover {
	padding-right:15px;
	background:transparent url(s_desc.png) right no-repeat;
}
</style>

<table width="100%" border="1">
 
	<tr>
    
		<th><?php echo sort_link('DATE', 'date') ?></th>
		<th><?php echo sort_link('CLIENT', 'client') ?></th>
        <th><?php echo sort_link('FORFAIT', 'forfait') ?></th>
        <th><?php echo sort_link('NUMERO DOSSIER', 'numero') ?></th>
        <th><?php echo sort_link('RT PAYER', 'rtpayer') ?></th>
        <th><?php echo sort_link('RETOUR THEORIQUE RT', 'retourtheoriquert') ?></th>
        <th><?php echo sort_link('RETOUR REEL RT', 'retourreelrt') ?></th>
        <th><?php echo sort_link('ENVOIS DEVIS', 'envoidevis') ?></th>
        <th><?php echo sort_link('COMMANDE', 'commande') ?></th>
		<th><?php echo sort_link('STATUT', 'statut') ?></th>
        <th><?php echo sort_link('ETAT', '-') ?></th>
        

</th> 
	</tr>

 
    
<?php while( $row=mysql_fetch_assoc($result) ) : ?>
  

<tr>


<?php   
 
switch ($row['statut']){ 
case 'EN COURS': 
$couleur='orange';//a remplacer par le code hexa de la couleur voulue #....... 
break; 

case 'TRAITE': 
$couleur='green';//a remplacer par le code hexa de la couleur voulue #....... 
break; 

case 'SOLDEE': 
$couleur='grey';//a remplacer par le code hexa de la couleur voulue #....... 
break; 

case 'RECU': 
$couleur='green';//a remplacer par le code hexa de la couleur voulue #....... 
break; 

case 'EN COMMANDE': 
$couleur='yellow';//a remplacer par le code hexa de la couleur voulue #....... 
break;

default: 
$couleur= 'white';//a remplacer par le code hexa de la couleur par defaut 
break; 

} 
echo "<tr style=\"background-color:$couleur;\"> 

<td width=\"100\">"; 
echo $row['date']; 
echo "</td> 

<td width=\"150\">"; 
echo $row['client']; 
echo "</td> 

<td width=\"200\">"; 
echo $row['forfait']; 
echo "</td> 

<td width=\"150\">"; 
echo $row['numero']; 
echo "</td> 

<td width=\"50\">"; 
echo $row['rtpayer']; 
echo "</td> 

<td width=\"100\">"; 
echo $row['retourtheoriquert']; 
echo "</td> 

<td width=\"100\">"; 
echo $row['retourreelrt']; 
echo "</td> 

<td width=\"100\">"; 
echo $row['envoidevis']; 
echo "</td> 


<td width=\"100\">"; 
echo $row['commande']; 
echo "</td> 

<td width=\"100\">"; 
echo $row['statut']; 
echo "</td> 

<td width=\"100\">"; 
echo $row['etat']; 
echo "</td> 

<td width=\"150\">"; 

// on se connecte à notre base
$base = mysql_connect ('localhost', 'cash01', 'ssnbmuq');
mysql_select_db ('cash01_INFOS') ;

// lancement de la requete
$sql = "INSERT INTO pose (etat)
       VALUES ( '$etat')";

$madate=$row['retourtheoriquert'];
$madate=explode("-",$madate);
$madate=mktime(0,0,0,$madate[1],$madate[2],$madate[0]);
$dateAujourdhui  = mktime(0, 0, 0, date("m"),   date("d"),   date("Y"));

if($madate<$dateAujourdhui&&$row['statut']=='TRAITE'){
    echo "<span style='color:black;'>DEVIS ENVOYE AU CLIENT</span>";
	
}elseif($madate==$dateAujourdhui&&$row['statut']=='TRAITE'){
    echo "<span style='color:black;'>DEVIS ENVOYE AU CLIENT</span>";
	
}elseif($madate==$dateAujourdhui&&$row['statut']=='RECU'){
    echo "<span style='color:yellow;'>EN COURS DE TRAITEMENT</span>";
		
}elseif($madate<$dateAujourdhui&&$row['statut']=='RECU'){
    echo "<span style='color:yellow;'>EN COURS DE TRAITEMENT</span>";	
	
}elseif($madate==$dateAujourdhui&&$row['statut']=='EN COMMANDE'){
    echo "<span style='color:black;'>ATTENTE PRODUIT</span>";
		
}elseif($madate<$dateAujourdhui&&$row['statut']=='EN COMMANDE'){
    echo "<span style='color:black;'>ATTENTE PRODUIT</span>";	
	
}elseif($row['statut']=='SOLDEE'){
    echo "<span style='color:black;'>SOLDEE</span>";		
	
}elseif($madate==$dateAujourdhui){
	echo "<span style='color:blue;'>DERNIER JOUR</span>";
	
}elseif($madate<$dateAujourdhui){
	echo "<span style='color:red;'>RETARD</span>";

}else{
    echo "<span style='color:black;'>EN COURS</span>";
}

?>		 
<?php
// on se connecte à notre base
$base = mysql_connect ('localhost', 'xxxxx', 'xxxxx');
mysql_select_db ('xxxxx') ;

// lancement de la requete
$sql = "INSERT INTO pose (etat)
       VALUES ( '$etat')";
?>
</tr>

<?php endwhile ?>
</table>


Re: Enregistrement automatique de champs

Posté : 28 mai 2013, 22:38
par moogli
comment exécute ton une requête sql ?

tu le fait déjà pour le select c'est la même chose, tu n'a pas lu les tutos sur php et mysql jusqu'au bout :D


@+