-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

您需要在循环中每次迭代 mysql_fetch_row 一次。

for($i = 0; $i < mysql_num_rows($req); $i++) {
     $resultat = mysql_fetch_row($req);
     //the rest of your code goes here
}
于 2013-09-06T15:49:57.983 回答
0

在第二个 foreach 循环中,您设置 $H = 0; 然后 $H++ 在同一个循环中,这将不会正确增加,因为下次它再次遍历循环 $H = 0; 语句将导致此值为 0,因此会继续覆盖您的数据,直到循环完成并且最后只剩下一个 $_SESSION 值。

                for($j = 0; $j < count($panier); $j++)
                { 

                        if($resultat[$i] == $panier[$j][0])
                        {
                            $H = 0; <-- sets to zero on every interation 
                            $_SESSION['Panier']['Id'][$H] = $result[0];
                            $_SESSION['Panier']['CodeCours'][$H] = $result[1];
                            $_SESSION['Panier']['Titre'][$H] = $result[2];
                            $_SESSION['Panier']['Prealable'][$H] = $result[3];
                            $_SESSION['Panier']['NbHeure'][$H] = $result[4];
                            $_SESSION['Panier']['Session'][$H] = $result[5];
                            $_SESSION['Panier']['Credit'][$H] = $result[6];
                            $H++;
                        }
                }

如果打算迭代 $H 变量,建议您将其移到循环之外。

于 2013-09-06T16:22:38.040 回答
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'),);

我写的代码不完全依赖你的数据库和数据,因为我不知道它是什么......不知道。切换其中一些代码行以使用您的数据库内容。

   // $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 但不是 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 回答