-2

こんにちは、SQLステートメントについてアドバイスを受けたので、これが私の解決策です...ありがとうございました

$connection = ConnectionBD();
$tag = $_SESSION['nomUtilisateur'];
$panier= array();
$requete = mysql_query("SELECT * FROM cours INNER JOIN elevecours ON cours.idcours = elevecours.IDCours WHERE elevecours.IDEleve= '$tag'",$connection);
$H = 0;
        if(mysql_num_rows($requete) != 0)
        {
            while($row = mysql_fetch_row($requete))
            {
                //$panier[] = $row;
                $_SESSION['Panier']['Id'][$H]        = $row[0];
                $_SESSION['Panier']['CodeCours'][$H] = $row[1];
                $_SESSION['Panier']['Titre'][$H]     = $row[2];
                $_SESSION['Panier']['Prealable'][$H] = $row[3];
                $_SESSION['Panier']['NbHeure'][$H]   = $row[4];
                $_SESSION['Panier']['Session'][$H]   = $row[5];
                $_SESSION['Panier']['Credit'][$H]    = $row[6];
                $H ++;
            }
        }
        mysql_close($connection);
4

3 に答える 3

0

これらは私のテスト変数です...

   $session = array(); 
   $panier = array(array(1,'one'),array(2,'two'),array(3,'three'),);

   $req = array(
         array(1,'one','Titre1','Prealable1','NbHeure1','Session1','Credit1'),
         array(2,'two','Titre2','Prealable2','NbHeure2','Session2','Credit2'),
         array(3,'three','Titre3','Prealable3','NbHeure3','Session3','Credit3'),
         array(4,'four','Titre4','Prealable4','NbHeure4','Session4','Credit4'),
         array(5,'five','Titre5','Prealable5','NbHeure5','Session5','Credit5'),);

私はそれが何であるかわからないので、あなたのDBとデータに正確に依存しないようにコードを書きました... わかりません。これらのコード行のいくつかを切り替えて、データベースのものを使用してください。

   // $req = mysql_query("SELECT * FROM cours", $connection);  uncomment this line for your code

   if ($req!==false) 
   {
      foreach( $req as $i => $resultat )             // testing row, remove for real
      // while( $resultat = mysql_fetch_row($req) )  // real row, uncomment for you 
      {
         $compare = $resultat[0];  
         // we will be using $compare to see if this row
         // matches one in $panier.. this needs to be changed
         // appropriately.  I don't have enough info to define it....
         // this needs to be a unique field, or it should be 
         // it is going to be used like this:  if($resultat[$i] == $panier[$j][0])

         $result[ $compare ] = $resultat;
      } 

      $hi = 0;  
      foreach( $panier as $index => $panier_row )
      {                        
         $compare = $panier_row[0]; 

         if (isset($result[$compare]))
         {
            $H = 0;

            // change $session to $_SESSION for your code...  
            $session['Panier']['Id'][$hi][$H]        = $result[$compare][0];
            $session['Panier']['CodeCours'][$hi][$H] = $result[$compare][1];
            $session['Panier']['Titre'][$hi][$H]     = $result[$compare][2];
            $session['Panier']['Prealable'][$hi][$H] = $result[$compare][3];
            $session['Panier']['NbHeure'][$hi][$H]   = $result[$compare][4];
            $session['Panier']['Session'][$hi][$H]   = $result[$compare][5];
            $session['Panier']['Credit'][$hi][$H]    = $result[$compare][6];
            $H++;
            $hi++;  
         }
      }        
   }   

私の結果は$session['Panier']['Credit']、1、2、3 に対して 3 つのものがあることを示していますが、一致しないため、4 または 5 ではありません$panier

$sessionそのすべての終わりには 何がありますか:

array (
  'Panier' => 
  array (
    'Id' => 
    array (
      0 => 
      array (
        0 => 1,
      ),
      1 => 
      array (
        0 => 2,
      ),
      2 => 
      array (
        0 => 3,
      ),
    ),
    'CodeCours' => 
    array (
      0 => 
      array (
        0 => 'one',
      ),
      1 => 
      array (
        0 => 'two',
      ),
      2 => 
      array (
        0 => 'three',
      ),
    ),
    'Titre' => 
    array (
      0 => 
      array (
        0 => 'Titre1',
      ),
      1 => 
      array (
        0 => 'Titre2',
      ),
      2 => 
      array (
        0 => 'Titre3',
      ),
    ),
    'Prealable' => 
    array (
      0 => 
      array (
        0 => 'Prealable1',
      ),
      1 => 
      array (
        0 => 'Prealable2',
      ),
      2 => 
      array (
        0 => 'Prealable3',
      ),
    ),
    'NbHeure' => 
    array (
      0 => 
      array (
        0 => 'NbHeure1',
      ),
      1 => 
      array (
        0 => 'NbHeure2',
      ),
      2 => 
      array (
        0 => 'NbHeure3',
      ),
    ),
    'Session' => 
    array (
      0 => 
      array (
        0 => 'Session1',
      ),
      1 => 
      array (
        0 => 'Session2',
      ),
      2 => 
      array (
        0 => 'Session3',
      ),
    ),
    'Credit' => 
    array (
      0 => 
      array (
        0 => 'Credit1',
      ),
      1 => 
      array (
        0 => 'Credit2',
      ),
      2 => 
      array (
        0 => 'Credit3',
      ),
    ),
  ),
)

ただし、個人的には...次mysql_fetch_assocのように使用して実行する必要があります。

   $session = array(); 
   $panier = array(array(1,'one'),array(2,'two'),array(3,'three'),);

   $req = array(
        array('id'=>1,'var'=>'one','Titre'=>'Titre1','Prealable'=>'Prealable1','NbHeure'=>'NbHeure1','Session'=>'Session1','Credit'=>'Credit1'), 
        array('id'=>2,'var'=>'two','Titre'=>'Titre2','Prealable'=>'Prealable2','NbHeure'=>'NbHeure2','Session'=>'Session2','Credit'=>'Credit2'), 
        array('id'=>3,'var'=>'thr','Titre'=>'Titre3','Prealable'=>'Prealable3','NbHeure'=>'NbHeure3','Session'=>'Session3','Credit'=>'Credit3'), 
        array('id'=>4,'var'=>'fou','Titre'=>'Titre4','Prealable'=>'Prealable4','NbHeure'=>'NbHeure4','Session'=>'Session4','Credit'=>'Credit4'), 
        array('id'=>5,'var'=>'fiv','Titre'=>'Titre5','Prealable'=>'Prealable5','NbHeure'=>'NbHeure5','Session'=>'Session5','Credit'=>'Credit5'),); 


   // $req = mysql_query("SELECT * FROM cours", $connection); 

   if ($req!==false) 
   {
      foreach( $req as $i => $resultat )             // testing row, remove for real
      // while( $resultat = mysql_fetch_assoc($req) )  // real row, uncomment for you 
                                                       //  mysql_fetch_assoc !!
      {
         $compare = $resultat['id'];  
         // we will be using $compare to see if this row
         // matches one in $panier.. this needs to be changed
         // appropriately.  I don't have enough info to define it....
         // this needs to be a unique field, or it should be 

         $result[ $compare ] = $resultat;
      } 

      foreach( $panier as $index => $panier_row )
      {                        
         $compare = $panier_row[0]; 

         if (isset($result[$compare]))
         { 
            $session[] = $result[$compare];  
         }
      }        
   }   

そしてこれは$session

array (
  0 => 
  array (
    'id' => 1,
    'var' => 'one',
    'Titre' => 'Titre1',
    'Prealable' => 'Prealable1',
    'NbHeure' => 'NbHeure1',
    'Session' => 'Session1',
    'Credit' => 'Credit1',
  ),
  1 => 
  array (
    'id' => 2,
    'var' => 'two',
    'Titre' => 'Titre2',
    'Prealable' => 'Prealable2',
    'NbHeure' => 'NbHeure2',
    'Session' => 'Session2',
    'Credit' => 'Credit2',
  ),
  2 => 
  array (
    'id' => 3,
    'var' => 'thr',
    'Titre' => 'Titre3',
    'Prealable' => 'Prealable3',
    'NbHeure' => 'NbHeure3',
    'Session' => 'Session3',
    'Credit' => 'Credit3',
  ),
)
于 2013-09-06T17:21:20.687 に答える