PHP return function

Asked

Viewed 146 times

3

Despite the variable url be returning content, as VAR_DUMP, the code below comes out by the message "URL nonexistent". Why this? Where am I sinning in the code?

public function getUrlcliente($cliente_id) {

        $parametro = "cliente_id=" . (int)$cliente_id;
        $url_externa = $this->db->query("SELECT DISTINCT query as indice, keyword as url FROM url_alias WHERE query = '" . $parametro . "'");

        if (empty($url_externa)) {
            $url = "Erro - URL amigável não cadastrada";
        }
        else {
            foreach ($url_externa as $row) {
                var_dump($row);
                if (isset($row->url)) {
                    $pos = strpos($row->url, "www");
                    if ($pos == false) {
                    }
                    else {
                        $url = $row->url;
                        break;
                    }
                }
                else {
                    $url = "Url inexistente";  ---> ele está saindo aqui
                }
            }
        }

        if ($url_externa->num_rows) {
                    return $url;
                }
        else
                {
                    return "Erro-Url inexistente";
                }
}

VAR_DUMP is returning this ---->

int(1) array(2) { ["Indice"]=> string(13) "cliente_id=42" ["url"]=> string(21) "http://www.uol.com.br" } array(1) { [0]=> array(2) { [""Indice"]=> string(13) "cliente_id=42" ["url"]=> string(21) "http://www.uol.com.br" } }

Note that the purpose of the function is to return only one string, the URL, not to return an array.

  • 3

    Have you tried to take the amount like this $row["url"] ?

  • 2

    @Augusto was that. Thank you.

1 answer

3


Follow the solution below by taking the result of the array $row

public function getUrlcliente($cliente_id) {
    $parametro = "cliente_id=" . (int)$cliente_id;
    $url_externa = $this->db->query("SELECT DISTINCT query as indice, keyword as url FROM url_alias WHERE query = '" . $parametro . "'");

    if (empty($url_externa)) {
        $url = "Erro - URL amigável não cadastrada";
    } else {
        foreach ($url_externa as $row) {
            if (isset($row['url'])) {
                $pos = strpos($row['url'], "www");
                if ($pos !== false) {
                    $url = $row['url'];
                    break;
                }
            } else {
                $url = "Url inexistente";  ---> ele está saindo aqui
            }
        }
    }

    if ($url_externa->num_rows) {
        return $url;
    } else {
        return "Erro-Url inexistente";
    }
}
  • 3

    Msergio if the Augusto solution solves your problem, you can mark the answer as accepted later, to appear as resolved in the listing of the site. When it is something that admits several answers, it is always good to wait a longer time, to choose the most suitable one, but in this case here I think that there is not much to change.

Browser other questions tagged

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