Ankündigung

Einklappen
Keine Ankündigung bisher.

[Erledigt] Undefined index

Einklappen

Neue Werbung 2019

Einklappen
X
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • [Erledigt] Undefined index

    Habe folgende Fehlermeldung:Undefined index: bild in C:\xampp\htdocs\map.php on line 96

    Was habe ich falsch gemacht oder besser gesagt wie kann ich diese Fehlermeldung umgehen!

    PHP-Code:
    class MyMap
    {
        private 
    $map_width;            
        private 
    $map_height;        
        private 
    $field_width_px;    
        private 
    $field_height_px;    
     
        private 
    $window_width;       
        private 
    $window_height;        
        private 
    $window_topleft_x;    
        private 
    $window_topleft_y;    
     
        private 
    $fields;    
     
        private 
    $x;        
        private 
    $y;       
        
        public function 
    setFields($fields)
        {
            
    $this->fields $fields;
        }
        public function 
    setField($x$y$field)
        {
            if ((
    $x 0) || ($y 0) || ($x >= $this->map_width) || ($y >= $this->map_height)) return false;
            
    $this->fields[$x][$y] = $field;
        }
        public function 
    getField($x$y)
        {
            if (@isset(
    $this->fields[$x][$y])) return $this->fields[$x][$y]; else return false;
        }
     
        public function 
    setSize($width$height)
        {
            
    $this->map_width intval($width);
            
    $this->map_height intval($height);
        }
        public function 
    setFieldSize($width_px$height_px)
        {
            
    $this->field_width_px intval($width_px);
            
    $this->field_height_px intval($height_px);
        }
        public function 
    setPosition($x$y)
        {
            if (
    $x 0$x 0;
            if (
    $y 0$y 0;
            if (
    $x >= $this->map_width$x $this->map_width 1;
            if (
    $y >= $this->map_height$y $this->map_height 1;
            
    $this->$x;
            
    $this->$y;
            
    $this->setWindowPosition($x - ($this->window_width >> 1), $y - ($this->window_height >> 1));
        }
        public function 
    setWindowSize($width$height)
        {
            
    $this->window_width intval($width);
            
    $this->window_height intval($height);
        }
        public function 
    setWindowPosition($x$y)
        {
            if (
    $x 0$x 0;
            if (
    $y 0$y 0;
            if (
    $x+$this->window_width $this->map_width$x $this->map_width $this->window_width;
            if (
    $y+$this->window_height $this->map_height$y $this->map_height $this->window_height;
            
    $this->window_topleft_x intval($x);
            
    $this->window_topleft_y intval($y);
        }
        public function 
    getWindowHtml()
        {
            
            
    $code '<div id="mymapwindow">';
            
    $window_bottomright_x = ($this->window_topleft_x + ($this->window_width-1) );
            
    $window_bottomright_y = ($this->window_topleft_y + ($this->window_height-1) );
            for(
    $y=$this->window_topleft_y$y <= $window_bottomright_y$y++)
            {
                for(
    $x=$this->window_topleft_x$x <= $window_bottomright_x$x++)
                
                {
                    
    $field $this->getField($x$y);
                    if (
    $field)

                    {
                        
                        
    $color $field['color'];
                        
    $caption $field['caption'];                    
                        
    $test $field['bild'];
                        
                    }
                    else
                    {
                        
    $color '#009933';
                        
    $caption '';
                        
    $test '';
                    };
                    
    $code .= '<div style="float: left; width: '.($this->field_width_px-1).'px; height: '.($this->field_height_px-1).'px; margin-right: 1px; margin-bottom: 1px; background-color: '.$color.'; text-align: center;" title=" '.$caption.' ">('.$x.'|'.$y.')'.$test.'</div>';
                };
                
    $code .= '<div style="clear:both;"></div>';
            };
            
    $code .= '</div>';
            return 
    $code;
        }
        public function 
    showWindow()
        {
            echo 
    $this->getWindowHtml();
        }
        public function 
    getHtml()
        {
            
    $code '<div id="mymap">';
            for(
    $y=0$y $this->map_height$y++)
            {
                for(
    $x=0$x $this->map_width$x++)
                {
                    
    $field $this->getField($x$y);
                
                    if (
    $field)
                    {
                    
                        
    $color $field['color'];
                        
    $caption $field['caption'];
                        
    $test $field['bild'];
                        
                        
                    }
                    else
                    {
                        
    $color '#009933';
                        
    $caption '';
                        
    $test '';
                    };
                    
    $code .= '<div style="float: left; width: '.($this->field_width_px-1).'px; height: '.($this->field_height_px-1).'px; margin-right: 1px; margin-bottom: 1px; background-color: '.$color.'; text-align: center;" title=" '.$caption.' ">('.$x.'|'.$y.')'.$bild.'</div>';
                };
                
    $code .= '<div style="clear:both;"></div>';
            };
            
    $code .= '</div>';
            return 
    $code;
        }
        public function 
    show()
        {
            echo 
    $this->getHtml();
        }
    };

    $map = new MyMap();
    $map->setSize(1010);
    $map->setFieldSize(100100); //Feldergrösse
    $map->setField(53, array("color"=>'#006699'"caption"=>'Wasser'"bild"=>'<img src="images/bild.png" width="80" height="80" border="0"/>') );
    $map->setField(52, array("color"=>'#CCCCCC'"caption"=>'Berge') );
    $map->setWindowSize(55);  // Sichtfenster 
    $map->setPosition(52);    // Aktuelle Position 
    $map->showWindow();        // Ausgabe 

  • #2
    Undefined index: bild in C:\xampp\htdocs\map.php on line 96
    Welches ist denn deine Zeile 96 ?

    Die Fehlermeldung kommt dann wenn du - wie dort steht - auf einen undefinierten Index (Array) zugreifst.

    wie kann ich diese Fehlermeldung umgehen!
    Vor Zugriff prüfen ob Index vorhanden ist oder Index vor Zugriff anlegen.

    EDIT: Wenn es diese Zeile ist (die letzte in der Box hier) dann überleg mal woher bild kommt, scheibar gibt getField das nicht zurück.

    PHP-Code:
        public function getHtml() 
        { 
            
    $code '<div id="mymap">'
            for(
    $y=0$y $this->map_height$y++) 
            { 
                for(
    $x=0$x $this->map_width$x++) 
                { 
                    
    $field $this->getField($x$y); 
                 
                    if (
    $field
                    { 
                     
                        
    $color $field['color']; 
                        
    $caption $field['caption']; 
                        
    $test $field['bild']; 
    EDIT 2:

    Und mach bitte das @ weg.

    PHP-Code:
    if (@isset($this->fields[$x][$y])) return $this->fields[$x][$y]; else return false
    The string "()()" is not palindrom but the String "())(" is.

    Debugging: Finde DEINE Fehler selbst! | Gegen Probleme beim E-Mail-Versand | Sicheres Passwort-Hashing | Includes niemals ohne __DIR__
    PHP.de Wissenssammlung | Kein Support per PN

    Kommentar


    • #3
      Ok dann müsste ich an dieser Stelle prüfen ob bild einen wert hat, aber warum
      bekomme ich keine Fehlermeldung bei caption und color?

      Kommentar


      • #4
        Vermutlich weil die vorhanden sind, finde er heraus:

        PHP-Code:
        // ...

        $field $this->getField($x$y);  

        var_dump($field); // mach das mal rein und schau was im Array ist
                  
        if ($field)
        {  
        // ... 
        Und je nachdem was vorhanden ist oder nicht, schaust du dann weiter ob/wie getField das übergibt etc.. Bis du an dem Punkt bist wo es "passiert".

        http://php-de.github.io/jumpto/leitfaden/

        LG
        The string "()()" is not palindrom but the String "())(" is.

        Debugging: Finde DEINE Fehler selbst! | Gegen Probleme beim E-Mail-Versand | Sicheres Passwort-Hashing | Includes niemals ohne __DIR__
        PHP.de Wissenssammlung | Kein Support per PN

        Kommentar


        • #5
          ok Danke für die Hilfe

          Kommentar


          • #6
            Wenn ich jetzt nicht komplett bescheuert bin,wird der array bild,caption und color übergeben.

            Folgendes steht im array:

            PHP-Code:
            bool(falsebool(falsebool(falsebool(falsebool(falsebool(falsebool(falsebool(falsebool(falsebool(falsebool(falsebool(false) array(2) { ["color"]=> string(7"#CCCCCC" ["caption"]=> string(5"Berge" }
            NoticeUndefined indexbild in C:\xampp\htdocs\map.php on line 97
            bool
            (falsebool(falsebool(falsebool(false) array(3) { ["color"]=> string(7"#006699" ["caption"]=> string(6"Wasser" ["bild"]=> string(63"" bool(falsebool(falsebool(falsebool(false
            PHP-Code:
            // ...

            $field $this->getField($x$y);  //genau hier wird alles übergeben

            var_dump($field); // mach das mal rein und schau was im Array ist
                      
            if ($field)
            {  
            // ... 
            und jetzt????????

            Kommentar


            • #7
              Du bist innerhalb von 2 for-Schleifen und bei einem der Durchgänge ist es jedenfalls nicht da. Finde heraus bei welchem und warum genau und beheb es. Das ist deine Aufgabe, wie du es machst "debuggen" weißt du jetzt ja.
              The string "()()" is not palindrom but the String "())(" is.

              Debugging: Finde DEINE Fehler selbst! | Gegen Probleme beim E-Mail-Versand | Sicheres Passwort-Hashing | Includes niemals ohne __DIR__
              PHP.de Wissenssammlung | Kein Support per PN

              Kommentar


              • #8
                is ja auch kein Wunder das es kein Bild gibt
                hast du selber vorgegeben!
                PHP-Code:
                $map->setField(53, array("color"=>'#006699'"caption"=>'Wasser'"bild"=>'<img src="http://www.php.de/images/bild.png" width="80" height="80" border="0"/>') ); 
                $map->setField(52, array("color"=>'#CCCCCC'"caption"=>'Berge') ); 
                Wenn das so beabsichtigt ist dann stimmt deine Kontrollstruktur nicht mit dem überein was du vorhast.

                Wie du das umgehen kannst ist das setzen eines default wertes

                anstatt:
                PHP-Code:
                for($x=$this->window_topleft_x$x <= $window_bottomright_x$x++) 
                             
                            { 
                                
                $field $this->getField($x$y); 
                                if (
                $field

                                { 
                                     
                                    
                $color $field['color']; 
                                    
                $caption $field['caption'];                     
                                    
                $test $field['bild']; 
                                     
                                } 
                                else 
                                { 
                                    
                $color '#009933'
                                    
                $caption ''
                                    
                $test ''
                                }; 
                                
                $code... 
                kannst du das so machen
                PHP-Code:
                for($x=$this->window_topleft_x$x <= $window_bottomright_x$x++) 
                             
                            { 
                            
                $color '#009933'
                            
                $caption NULL;                     
                            
                $test NULL
                            
                            
                $field $this->getField($x$y); 
                            
                            if (isset (
                $field['color']) and isset ($field['caption']) and isset ($field['bild'])) { 
                            
                $color $field['color']; 
                            
                $caption $field['caption'];                     
                            
                $test $field['bild']; 
                            }
                                
                $code... 
                oder eben für jeden index eine einzelne if anweisung
                PHP-Code:
                if ($field) { 
                            
                $color = (isset($field['color']))?$field['color']:'#009933'
                            
                $caption = (isset($field['caption']))?$field['caption']:NULL;                     
                            
                $test = (isset($field['bild']))?$field['bild']:NULL
                            } 
                Der eigentliche Fehler liegt wo anders deine Klasse sieht ziemlich wirr aus und die Methode setFields geht einfach davon aus das alle indexe gesetzt sind da du die Parameter nicht einzeln sondern schon als array übergibst

                Kommentar


                • #9
                  Vielen Dank für die Hilfe,jetzt hab ich es verstanden.

                  Kommentar

                  Lädt...
                  X