Erreur de tableau

Résolu
valentin -  
jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   -
bonjour,

$matrice[$donneeseffect][0] = $donneeseffect;

 if($donneestag==1){
  $matrice[$donneeseffect][1] = $matrice [$donneeseffect] [1] +1;
 }
 if($donneestag==2){
  $matrice[$donneeseffect][2] = $matrice [$donneeseffect] [2] +1;
 }
 if($donneestag==5){
  $matrice[$donneeseffect][3] = $matrice [$donneeseffect] [3] +1;
 }
 if($donneestag==6){
  $matrice[$donneeseffect][4] = $matrice [$donneeseffect] [4] +1;
 }


EDIT : Ajout des balises de code

voici mon code : il m'indique Warning: Illegal offset
mais quesqu'y vas pas
merci de votre attention

4 réponses

  1. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830
     
    Bonjour,

    Où as tu initialisé ton array $matrice ?
    De quelle manière ?

    1
  2. valentin
     
    oui : $matrice = array_fill ( 1 , $nbLignes , array(0 , 4 , "0" ));
    1
    1. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830
       
      Donc... ta matrice ressemble à ça :
      Array
      (
          [1] => Array
              (
                  [0] => 0
                  [1] => 4
                  [2] => 0
              )
      
          [2] => Array
              (
                  [0] => 0
                  [1] => 4
                  [2] => 0
              )
      
          [3] => Array
              (
                  [0] => 0
                  [1] => 4
                  [2] => 0
              )
      
      )
      


      .. comme tu peux le voir .. les "indexes" commencent à ZERO et non à 1 .... (et vont jusqu'à 2 )
      Donc
      if($donneestag==5){
        $matrice[$donneeseffect][3] = $matrice [$donneeseffect] [3] +1;
       }
       if($donneestag==6){
        $matrice[$donneeseffect][4] = $matrice [$donneeseffect] [4] +1;
      

      Ne peuvent pas fonctionner.... 3 et 4 sont en dehors de la dimension de ton array
      0
    2. valentin
       
      oups : oky je viens de comprendre un truc merci: mais comment je fait un tableau qui depend de nombre de ligne
      0
  3. AssassinTourist Messages postés 409 Date d'inscription   Statut Contributeur Dernière intervention   1 313
     
    0
    1. valentin
       
      je sais pas comment faire avec sublime texte, sinon pour le reste j'ais une matrice qui safiche avec des 0 quand meme a la fin
      0
    2. valentin
       
      oups dsl lu un peut tros vite : voici l'erreur maintenant :
      Warning: Illegal offset type in /Users/alainmeo/Desktop/base.php on line 65

      Warning: Illegal offset type in /Users/alainmeo/Desktop/base.php on line 65

      Warning: Illegal offset type in /Users/alainmeo/Desktop/base.php on line 65

      Warning: Illegal offset type in /Users/alainmeo/Desktop/base.php on line 65

      Notice: Undefined offset: 0 in /Users/alainmeo/Desktop/base.php on line 94
      0
      Notice: Undefined offset: 0 in /Users/alainmeo/Desktop/base.php on line 94
      0
      Notice: Undefined offset: 0 in /Users/alainmeo/Desktop/base.php on line 94
      0
      Notice: Undefined offset: 0 in /Users/alainmeo/Desktop/base.php on line 94
      0
      Notice: Undefined offset: 0 in /Users/alainmeo/Desktop/base.php on line 94
      0

      Notice: Undefined offset: 1 in /Users/alainmeo/Desktop/base.php on line 94
      0
      Notice: Undefined offset: 1 in /Users/alainmeo/Desktop/base.php on line 94
      0
      Notice: Undefined offset: 1 in /Users/alainmeo/Desktop/base.php on line 94
      0
      Notice: Undefined offset: 1 in /Users/alainmeo/Desktop/base.php on line 94
      0
      Notice: Undefined offset: 1 in /Users/alainmeo/Desktop/base.php on line 94
      0

      Notice: Undefined offset: 2 in /Users/alainmeo/Desktop/base.php on line 94
      0
      Notice: Undefined offset: 2 in /Users/alainmeo/Desktop/base.php on line 94
      0
      Notice: Undefined offset: 2 in /Users/alainmeo/Desktop/base.php on line 94
      0
      Notice: Undefined offset: 2 in /Users/alainmeo/Desktop/base.php on line 94
      0
      Notice: Undefined offset: 2 in /Users/alainmeo/Desktop/base.php on line 94
      0
      0
  4. valentin
     
    le script en entier :
     <?php
    
    //$pass = "root";
    //$user = "root";
    //$host = "localhost";
    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    try
    {
     $bdd = new PDO('mysql:linux_socket=/Applications/MAMP/tmp/mysql/mysql.sock;host=127.0.0.1;port=8889;dbname=teste','root','root');
     //;charset=utf8
    //$user = 'root';
    //$password = 'root';
    //$db = 'teste';
    //$host = 'localhost';
    //$port = 8889;
    
    //$link = mysqli_init();
    //$success = mysqli_real_connect(
       //$link, 
       //$host, 
       //$user, 
       //$password, 
       //$db,
      // $port
    //);
    // $user = 'root';
    //$password = 'root';
    //$db = 'inventory';
    //$host = '127.0.0.1';
    //$port = 8889;
    //$socket = 'localhost:/Applications/MAMP/tmp/mysql/mysql.sock';
    
    //$link = mysqli_init();
    //$success = mysqli_real_connect(
      // $link, 
       //$host,
       //$user, 
       //$password, 
       //$db,
       //$port,
       //$socket
    //);
    }
    catch(Exception $e)
    {
     
          die('Erreur : '.$e->getMessage());
    }
    //$bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    //$compteur = 0;
    $nbLignes = $bdd->query('select count(*) from maTable');
    // ou nb d'effect
    $matrice = array_fill ( 1 , $nbLignes , array(0 ,  4  , "0" ));
    
    $reponcetag = $bdd->query('SELECT ID_tag FROM vote_tag_effect');
    $reponceuser = $bdd->query('SELECT ID_user FROM vote_tag_effect');
    $reponseeffect = $bdd->query('SELECT ID_effect FROM vote_tag_effect'); 
    while ($donneeseffect = $reponseeffect->fetch()){
    while ($donneesuser = $reponceuser->fetch()){
    while ($donneestag = $reponcetag->fetch()){
    
    
    $matrice[$donneeseffect][0] = $donneeseffect;
    
     if($donneestag==1){
      $matrice[$donneeseffect][1] = $matrice [$donneeseffect] [1] +1;
     }
     if($donneestag==2){
      $matrice[$donneeseffect][2] = $matrice [$donneeseffect] [2] +1;
     }
     if($donneestag==5){
      $matrice[$donneeseffect][3] = $matrice [$donneeseffect] [3] +1;
     }
     if($donneestag==6){
      $matrice[$donneeseffect][4] = $matrice [$donneeseffect] [4] +1;
     }
    
    
    
    //$compteur = $compteur +1;
    
    
    
    }
    }
    }
    $bdd = null;
    $nbdeffect=3;
    
    for ($i=0;$i<$nbdeffect;$i++) {
     for ($j=0;$j<=4;$j++){
       printf("%b",$matrice[$i][$j]);
       
     }
    print("\n");
    //print_r($matrice)
    }
    
    ?>
    


    EDIT : AJout des balises de code (encore.......!!! )
    0
    1. valentin
       
      oups dsl
      0
    2. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830
       
      Je ne comprend pas l'intéret de faire 3 requêtes sur la même table... alors qu'une seule suffit
      $reponcetag = $bdd->query('SELECT ID_tag FROM vote_tag_effect');
      $reponceuser = $bdd->query('SELECT ID_user FROM vote_tag_effect');
      $reponseeffect = $bdd->query('SELECT ID_effect FROM vote_tag_effect');
      


      $tesDatas = $bdd->query('SELECT ID_tag, ID_user,ID_effect  FROM vote_tag_effect');
      
      0
    3. valentin
       
      oky mais dans ce ca comment je diferenci les variable id tag id user et id effect?
      0
    4. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830
       
      regarde donc ce que fait :
      while ($donnees = $tesDatas->fetch()){
         echo "<br>ID_tag : " . $donnees['ID_tag'];
         echo "  ID_user : " . $donnees['ID_user'];
         echo " ID_effect: " . $donnees['ID_effect'];
      }
      
      
      0
    5. valentin
       
      oky merci
      0