Function par ou commencé??

Résolu
Bonjour,

j'ai un problème, j'ai un code que je dois réutilisé plusieurs fois mais j'ai toujours une chose a changé sinon mes requettes et tous sont les mêmes la seul différence c'est que je doit cherché dans différente base de donnée donc celà interviendra seulement dans ce bout là
<?php
$host='localhost';
$user='*****';
$pass='*****';
$dbase='*****';

$connexion = mysql_connect($host,$user,$pass)or die ("Erreur de connexion");
mysql_select_db($dbase,$connexion)or die ("Erreur de connexion base");
?>


pour me connecté a la base non ???

et avec sa une fois que j'ai mes différente base connecté je veu affiché comme celà

<table border="1">
		<tr style="text-align: center; background-color:#FFAD5B; font-weight:bold;">
			<th> Pays </th>
            <th> Prestataire </th>
			<th> V G  </th>
			<th> Commandes </th>
            <th> Tx Conv. </th>
            <th> C.A </th>
            <th> Mmc </th>
		</tr>
        
<?php



$prefixe_query=mysql_query("SELECT sites_encodage FROM sites");
while ($prefixe_values=mysql_fetch_array($prefixe_query)) {
$prefixe=$prefixe_values["sites_encodage"].'_';
}


	// REQUETE 1 : 
	$query = "SELECT substr(customers_from, 1, 5) AS Origin, 
       COUNT(customers_id) AS effectif_vg
FROM ".$prefixe."customers cu
WHERE customers_from NOT LIKE '<%'
" . $condition . "

GROUP BY Origin ";
	$result_vg = mysql_query($query) ;
		
	
 
while ($row = mysql_fetch_array($result_vg)) 
{  

 
	// REQUETE 2 : 		
			$query_produit = 
					" SELECT 
					    count(co.commandes_status) AS command,
					   AVG(co.commandes_montant) AS Mmc 
						FROM ".$prefixe."customers cu
						INNER JOIN ".$prefixe."commandes co   
						ON cu.customers_id=co.customers_id 
						WHERE  co.commandes_status='1'
						and   customers_from LIKE '{$row['Origin']}%' " . $condition . "
						 ";
						
						if ($row['Origin'] == '') {
			$query_produit = " SELECT 
					    count(co.commandes_status) AS command,
					   AVG(co.commandes_montant) AS Mmc 
						FROM ".$prefixe."customers cu
						INNER JOIN ".$prefixe."commandes co  
						ON cu.customers_id=co.customers_id 
						WHERE   co.commandes_status='1'
						and   customers_from = '' " . $condition . "
						 ";
													}
						
						
		$result = mysql_query($query_produit) ;	


	while ($row1 = mysql_fetch_array($result)) 
	{	
					
?>
	 <tr>
			<td style="background-color:#CCCCCC;">  </td>
            <td style="text-align:center;"> <?php echo $row['Origin'] ?> </td>
			 <td style="text-align:center;"> <?php echo $row['effectif_vg'] ?> </td>
			 <td style="text-align:center;"> <?php echo $row1['command'] ?> </td>
             <td style="text-align:center;"><?php echo round(($row1['command'] / $row['effectif_vg']) * 100, 2); ?> % </td>
			 <td style="text-align:center;"> <?php echo round($row1['command']*$row1['Mmc']) ?>  &euro;  </td>
             <td style="text-align:center;"> <?php echo round($row1['Mmc'] ,2) ?>   &euro; </td>
                
	
<?php
} 	
	}
?>
</tr>
</table>


mon problème c'est que j'ai de la peine avec les paramètre a mettre dans ma fonction je regarde les tutos mais rien qui puisse trop m'aider mais ce serai gentille si quelqu'un qui métrise bien le sujet pouvai me guider merci.
Configuration: Windows XP
Firefox 3.0.10

6 réponses

  1. Bonjour,

    <?php
    function connexionBase($host, $user, $pass, $dbase) {

    $connexion = mysql_connect($host,$user,$pass)or die ("Erreur de connexion");
    mysql_select_db($dbase,$connexion)or die ("Erreur de connexion base");
    }
    ?>

    exemple d'appels :

    <?php
    connexionBase("localhost", "root", "root", "mybase") ;

    ?>
    ou avec des variables
    <?php
    $connexionPass = $_POST['pass'];
    connexionBase("localhost", "root", $connexionPass, "mybase") ;
    ?>
    1. Merci

      et pour l'appel des requettes j'ai fait comme sa

      <?php
      
      function stats_pays( $condition)
      {
         
      
      
      $prefixe_query=mysql_query("SELECT sites_encodage FROM sites");
      while ($prefixe_values=mysql_fetch_array($prefixe_query)) {
      $prefixe=$prefixe_values["sites_encodage"].'_';
      }
      
      
      	// REQUETE 1 : 
      	$query = "SELECT substr(customers_from, 1, 5) AS Origin, 
             COUNT(customers_id) AS effectif_vg
      FROM ".$prefixe."customers cu
      WHERE customers_from NOT LIKE '<%'
      " . $condition . "
      
      GROUP BY Origin ";
      	$result_vg = mysql_query($query) ;
      		
      	
       
      while ($row = mysql_fetch_array($result_vg)) 
      {  
      
       
      	// REQUETE 2 : 		
      			$query_produit = 
      					" SELECT 
      					    count(co.commandes_status) AS command,
      					   AVG(co.commandes_montant) AS Mmc 
      						FROM ".$prefixe."customers cu
      						INNER JOIN ".$prefixe."commandes co   
      						ON cu.customers_id=co.customers_id 
      						WHERE  co.commandes_status='1'
      						and   customers_from LIKE '{$row['Origin']}%' " . $condition . "
      						 ";
      						
      						if ($row['Origin'] == '') {
      			$query_produit = " SELECT 
      					    count(co.commandes_status) AS command,
      					   AVG(co.commandes_montant) AS Mmc 
      						FROM ".$prefixe."customers cu
      						INNER JOIN ".$prefixe."commandes co  
      						ON cu.customers_id=co.customers_id 
      						WHERE   co.commandes_status='1'
      						and   customers_from = '' " . $condition . "
      						 ";
      													}
      						
      						
      		$result = mysql_query($query_produit) ;	
      
      
      	while ($row1 = mysql_fetch_array($result)) 
      	{	
      	
      }
      
      ?>
      


      mais ce que je comprend pas c'est commen sa va sortire je doi fair un return??
    2. @siniferJe suppose que tu veux retourner le $result :
      $result = mysql_query($query_produit) ;

      return $result;

      }

      et lors de l'appel

      $res = stats_pays( $condition);

      while ($row1 = mysql_fetch_array($res))
      {
      //lecture du resultat
      }
  2. donc après j'ai juste a renseigné le $condition par

    <?php

    stats_pays($condition) ;
    ?>

    car au début de mon code j'ai
    <?php 
    $date_du = $_POST['Du'];
    $date_au = $_POST['Au'];
    
    	$condition = "";
       if (isset($date_du) && $date_du !='') {
                $condition .= " AND customers_date_inscription>='" . $date_du . "' ";
             }
       
          if (isset($date_au) && $date_au !='') {
                 $condition .= " AND customers_date_inscription<='" . $date_au . "'  ";
            }
    ?>
    1. oui mais ne pas oublier de récupérer le résultat avec une affectation :
      $result = stats_pays($condition) ;
  3. donc sa doit donnée comme sa

    Je suppose que tu veux retourner le $result :
    oui mais comme j'ai une autre requete je doi les prendre les deu comme sa?

    $result_vg = mysql_query($query) ;

    return $result_vg;

    $result = mysql_query($query_produit) ;

    return $result;

    }

    et lors de l'appel
    while ($row = mysql_fetch_array($result_vg))
    {
    $res_vg = stats_pays( $condition);

    while ($row1 = mysql_fetch_array($res_vg))
    {
    $res = stats_pays( $condition);

    while ($row1 = mysql_fetch_array($res))
    {
    <tr>
    <td style="background-color:#CCCCCC;"> </td>
    <td style="text-align:center;"> <?php echo $row['Origin'] ?> </td>
    <td style="text-align:center;"> <?php echo $row['effectif_vg'] ?> </td>
    <td style="text-align:center;"> <?php echo $row1['command'] ?> </td>
    <td style="text-align:center;"><?php echo round(($row1['command'] / $row['effectif_vg']) * 100, 2); ?> % </td>
    <td style="text-align:center;"> <?php echo round($row1['command']*$row1['Mmc']) ?> € </td>
    <td style="text-align:center;"> <?php echo round($row1['Mmc'] ,2) ?> € </td>

    }

    }

    et quand tu regarde tous le code mon seul paramètre est juste???
    1. Voilà je sais pas si j'ai bien compris mais j'ai fais comme sa pour la mise en fonction

      
      <?php
      
      function stats_pays($condition)
      {
         
      
      
      $prefixe_query=mysql_query("SELECT sites_encodage FROM sites");
      while ($prefixe_values=mysql_fetch_array($prefixe_query)) {
      $prefixe=$prefixe_values["sites_encodage"].'_';
      }
      
      
      	// REQUETE 1 : 
      	$query = "SELECT substr(customers_from, 1, 5) AS Origin, 
             COUNT(customers_id) AS effectif_vg
      FROM ".$prefixe."customers cu
      WHERE customers_from NOT LIKE '<%'
      " . $condition . "
      
      GROUP BY Origin ";
      	$result_vg = mysql_query($query) ;
      		
      return $result_vg;
      
      
      // REQUETE 2 : 		
      			$query_produit = 
      					" SELECT 
      					    count(co.commandes_status) AS command,
      					   AVG(co.commandes_montant) AS Mmc 
      						FROM ".$prefixe."customers cu
      						INNER JOIN ".$prefixe."commandes co   
      						ON cu.customers_id=co.customers_id 
      						WHERE  co.commandes_status='1'
      						and   customers_from LIKE '{$row['Origin']}%' " . $condition . "
      						 ";
      						
      						if ($row['Origin'] == '') {
      			$query_produit = " SELECT 
      					    count(co.commandes_status) AS command,
      					   AVG(co.commandes_montant) AS Mmc 
      						FROM ".$prefixe."customers cu
      						INNER JOIN ".$prefixe."commandes co  
      						ON cu.customers_id=co.customers_id 
      						WHERE   co.commandes_status='1'
      						and   customers_from = '' " . $condition . "
      						 ";
      													}
      		$result = mysql_query($query_produit) ;	
      
      return $result;
      ?>
      
      


      et j'ai fait comme sa pour l'appel de la fonction
      
      <?php
      
      $res_vg = stats_pays( $condition);
      
      while ($row1 = mysql_fetch_array($res_vg))
      {
      $res = stats_pays( $condition);
      
      while ($row1 = mysql_fetch_array($res))
      {
      	?> 
       <tr>
      			<td style="background-color:#CCCCCC;">  </td>
                  <td style="text-align:center;"> <?php echo $row['Origin'] ?> </td>
      			 <td style="text-align:center;"> <?php echo $row['effectif_vg'] ?> </td>
      			 <td style="text-align:center;"> <?php echo $row1['command'] ?> </td>
                   <td style="text-align:center;"><?php echo round(($row1['command'] / $row['effectif_vg']) * 100, 2); ?> % </td>
      			 <td style="text-align:center;"> <?php echo round($row1['command']*$row1['Mmc']) ?>  &euro;  </td>
                   <td style="text-align:center;"> <?php echo round($row1['Mmc'] ,2) ?>   &euro; </td>
      
      <?php
      }
      
      }
      ?>
      
      es-ce que tous mes paramètre sont correct??
      
      1. es-ce que tous mes paramètre sont correct??
        car sa n'affiche rien??
        1. $res_vg = stats_pays( $condition);

          while ($row1 = mysql_fetch_array($res_vg))
          {
          $res = stats_pays( $condition);

          ça c'est faux.

          <?php

          $res_vg = stats_pays( $condition);

          while ($row1 = mysql_fetch_array($res_vg))
          {
          ?>
          <tr>
          <td style="background-color:#CCCCCC;"> </td>
          <td style="text-align:center;"> <?php echo $row1['Origin'] ?> </td>
          <td style="text-align:center;"> <?php echo $row1['effectif_vg'] ?> </td>
          <td style="text-align:center;"> <?php echo $row1['command'] ?> </td>
          <td style="text-align:center;"><?php echo round(($row1['command'] / $row1['effectif_vg']) * 100, 2); ?> % </td>
          <td style="text-align:center;"> <?php echo round($row1['command']*$row1['Mmc']) ?> € </td>
          <td style="text-align:center;"> <?php echo round($row1['Mmc'] ,2) ?> € </td>

          <?php
          }

          }
          ?>

          y'avait des endroits où tu avais mis $row à la place de $row1
        2. @NookZoui mais c'est normal car j'ai deux requet les deux premier resultat ce trouve dans row et les autres ce trouve dans row1

          c'est pour sa que j'ai fait :

          <?php
          
          $res_vg = stats_pays( $condition);
          
          while ($row = mysql_fetch_array($res_vg))
          {
          $res = stats_pays( $condition);
          
          while ($row1 = mysql_fetch_array($res))
          {
          	?> 


          car je prend le '$res_vg' de ma première requet puis le '$res' est dans ma boucle et c'est a la deuxième boucle que je fais l'affichage
        3. @siniferma première requete est comme sa: (là je prend deux info qui iront dans
          <td style="text-align:center;"> <?php echo $row['Origin'] ?> </td>
          <td style="text-align:center;"> <?php echo $row['effectif_vg'] ?> </td> )

          	// REQUETE 1 : 
          	$query = "SELECT substr(customers_from, 1, 5) AS Origin, 
                 COUNT(customers_id) AS effectif_vg
          FROM ".$prefixe."customers cu
          WHERE customers_from NOT LIKE '<%'
          " . $condition . "
          
          GROUP BY Origin ";
          	$result_vg = mysql_query($query) ;
          		
          	
           
          while ($row = mysql_fetch_array($result_vg)) 
          {  

          Ma boucle est row.

          Pour la deuxième requete qui est dans la boucle row : ( là je prend des infos qui iront dans <td style="text-align:center;"> <?php echo $row1['command'] ?> </td>
          <td style="text-align:center;"><?php echo round(($row1['command'] / $row['effectif_vg']) * 100, 2); ?> % </td>
          <td style="text-align:center;"> <?php echo round($row1['command']*$row1['Mmc']) ?> € </td>
          <td style="text-align:center;"> <?php echo round($row1['Mmc'] ,2) ?> € </td>)

          // REQUETE 2 : 		
          			$query_produit = 
          					" SELECT 
          					    count(co.commandes_status) AS command,
          					   AVG(co.commandes_montant) AS Mmc 
          						FROM ".$prefixe."customers cu
          						INNER JOIN ".$prefixe."commandes co   
          						ON cu.customers_id=co.customers_id 
          						WHERE  co.commandes_status='1'
          						and   customers_from LIKE '{$row['Origin']}%' " . $condition . "
          						 ";
          						
          						if ($row['Origin'] == '') {
          			$query_produit = " SELECT 
          					    count(co.commandes_status) AS command,
          					   AVG(co.commandes_montant) AS Mmc 
          						FROM ".$prefixe."customers cu
          						INNER JOIN ".$prefixe."commandes co  
          						ON cu.customers_id=co.customers_id 
          						WHERE   co.commandes_status='1'
          						and   customers_from = '' " . $condition . "
          						 ";
          													}
          						
          						
          		$result = mysql_query($query_produit) ;	
          
          
          	while ($row1 = mysql_fetch_array($result)) 
          	{	
          

          donc après cette boucle je fais mon affichage là j'ai fais san la fonction pour mieu te montrer
        4. @sinifermais mon problème n'est pas au niveau de ce qui a dedans

          function stats_pays($condition) {

          je doit avoir juste $condition ou je doit en avoir d'autre???
        5. @siniferne peu-je pas tous simplement faire comme sa crée ma fonction et après l'appelé

          <?php
          
          function stats_pays($condition) {
          
          
          $prefixe_query=mysql_query("SELECT sites_encodage FROM sites");
          while ($prefixe_values=mysql_fetch_array($prefixe_query)) {
          $prefixe=$prefixe_values["sites_encodage"].'_';
          }
          
          
          	// REQUETE 1 : 
          	$query = "SELECT substr(customers_from, 1, 5) AS Origin, 
                 COUNT(customers_id) AS effectif_vg
          FROM ".$prefixe."customers cu
          WHERE customers_from NOT LIKE '<%'
          " . $condition . "
          
          GROUP BY Origin ";
          	$result_vg = mysql_query($query) ;
          		
          	
           
          while ($row = mysql_fetch_array($result_vg)) 
          {  
          
           
          	// REQUETE 2 : 		
          			$query_produit = 
          					" SELECT 
          					    count(co.commandes_status) AS command,
          					   AVG(co.commandes_montant) AS Mmc 
          						FROM ".$prefixe."customers cu
          						INNER JOIN ".$prefixe."commandes co   
          						ON cu.customers_id=co.customers_id 
          						WHERE  co.commandes_status='1'
          						and   customers_from LIKE '{$row['Origin']}%' " . $condition . "
          						 ";
          						
          						if ($row['Origin'] == '') {
          			$query_produit = " SELECT 
          					    count(co.commandes_status) AS command,
          					   AVG(co.commandes_montant) AS Mmc 
          						FROM ".$prefixe."customers cu
          						INNER JOIN ".$prefixe."commandes co  
          						ON cu.customers_id=co.customers_id 
          						WHERE   co.commandes_status='1'
          						and   customers_from = '' " . $condition . "
          						 ";
          													}
          						
          						
          		$result = mysql_query($query_produit) ;	
          
          
          	while ($row1 = mysql_fetch_array($result)) 
          	{	
          }			
          ?>
          


          et après je l'appel comme sa
          <?php stats_pays($condition); ?>

          mais mon problème c'est que sa marche pas
      2. je l'appelais deux fois car sa marchait pas quant je l'appelais une fois mais là j'ai réussi en prenant même l'affichage.

        Merci pour ton aide sa ma été très utiles bonne journés