For + Count in Cakephp

Asked

Viewed 28 times

1

I’m trying to get my for rotate according to how many pictures are 'inputadas' in SELECT. The problem is that regardless of how many images are 'inputadas', it only saves the first one. What’s wrong with this logic?

private function criarfoto($galeria_id, $file) {
    $extension = "";
    $valores = count($file['Foto']); // SALVA em quantidade,os indices que tem dentro do array.
    for ($i = 0; $i < $valores; $i++) { // faz o loop de acordo com a quantidade de indices no array
        $extension = pathinfo($file['Foto'][$i]['name'], PATHINFO_EXTENSION);
        $allowExt = array('jpg', 'jpeg', 'png', 'gif');
        //print_r($file); 
        if (!in_array($extension, $allowExt)) {
            return false;
        }
        $fotos_galeria = array(
            'galeria_id' => $galeria_id,
            'titulo' => $file['titulo'],
            'descricao' => $file['descricao'],
            'extension' => $extension,
        );
        $this->Foto->create();
        if ($this->Foto->save(array('Foto' => $fotos_galeria))) {
            //die(print_r($fotos_galeria));
            $foto_id = $this->Foto->id;
            if (move_uploaded_file($file['Foto'][$i]['tmp_name'], WWW_ROOT . 'files' . DS . 'galeria' . DS . "{$foto_id}_o.{$extension}")) {
                return $foto_id;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}
  • You can edit the question with the result of var_dump($file) within the function criarfoto() ? (to know what you’re input)

1 answer

1

Instead of using Return false when an error occurs, use continue, because the false Return will cause the loop to stop when it does not meet the criteria, and the continue will cause the loop to proceed to the next item of the array.

In case you want to identify also the elements that had error in sending, I suggest you change your return type from boolean to array.

If you need to return the Ids of the inserted images, you will need to work with a bi-dimensional array, storing the status and the return message in each case.

<?php
private function criarfoto($galeria_id, $file) {
    $extension = "";
    $valores = count($file['Foto']); // SALVA em quantidade,os indices que tem dentro do array.
    $error = array();
    for ($i = 0; $i < $valores; $i++) { // faz o loop de acordo com a quantidade de indices no array
        $extension = pathinfo($file['Foto'][$i]['name'], PATHINFO_EXTENSION);
        $allowExt = array('jpg', 'jpeg', 'png', 'gif');
        //print_r($file); 
        if (!in_array($extension, $allowExt)) {
            $error[] = "Extensão inválida para a imagem {$file['Foto'][$i]['name']}";
            continue;
        }
        $fotos_galeria = array(
            'galeria_id' => $galeria_id,
            'titulo' => $file['titulo'],
            'descricao' => $file['descricao'],
            'extension' => $extension,
        );
        $this->Foto->create();
        if ($this->Foto->save(array('Foto' => $fotos_galeria))) {
            //die(print_r($fotos_galeria));
            $foto_id = $this->Foto->id;
            if (!move_uploaded_file($file['Foto'][$i]['tmp_name'], WWW_ROOT . 'files' . DS . 'galeria' . DS . "{$foto_id}_o.{$extension}")) {
                $error[] = "Falha carregando imagem {$file['Foto'][$i]['name']} para o servidor";
            }
        } else {
             $error[] = "Falha salvando a imagem {$file['Foto'][$i]['name']} no banco de dados";
        }
    }
    return (empty($error)) ? true : $error;
}
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.