in_array php - poo

0

I have an object in which there are modules in the form of an array. What I want is that when I click on Select, on the right side they can not appear repeated. It does not give me errors but it does not perform the function that I want.

Code:

    public function anadirAlArray($codigo) {
      $modulo=DB::consultaModulo($codigo);
      array_push($this->modulosSel, $modulo);

echo '<table border="1">';
echo '</tr>';
echo '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
      foreach ($this->modulosSel as $modulo){
        echo '<tr>';
        $cod = $modulo->getCodigo();
        $nombre= $modulo->getNombre();

        if (in_array($nombre,$this->modulosSel)) {
          echo "Modulo existente";
        }else{
          echo '<td>'.$nombre." ".'<input type="submit" name="quitarModulo[' .$cod.']" value="Quitar">'."<br>";
          echo '</tr>';
        }
      }
      echo "</form>";
      echo '</table>';

  }
    
asked by Code Lite 25.01.2018 в 20:14
source

1 answer

0

I understand that to check if there are repeated, you execute the line if (in_array($nombre,$this->modulosSel)) {... . But on line 3, what you are adding to $this->modulosSel are not texts but the resultset $modulo .

In other words, for your validation to work, you must modify line 3 to read:

array_push($this->modulosSel, $modulo->getNombre());
    
answered by 25.01.2018 в 22:15