-2
I created a class with arrays to be populated with objects, selected the database, then made a foreach
to create a new object with each row of the select return and save these objects in the array.
But when I try to create a new object I get the following error message:
Warning illegal string offset in ['cd_anuncio'] checked in the bank and is equal.
I used the print_r
to check if there was a return, and I checked that the return is correct.
public static function getAnuncios() {
include("conexao.php");
$contador = 0;
$query = "SELECT * FROM tb_anuncio";
$result = $conexao - > query($query);
$formatResult = mysqli_fetch_assoc($result);
print_r($formatResult);
if (!isset($anuncios)) {
foreach($formatResult as $anuncio) {
$obj_anuncio = new anuncio($anuncio['cd_anuncio'],
$anuncio['nm_titulo'],
$anuncio['ds_anuncio'],
$anuncio['cd_usuario'],
$anuncio['nm_estado'],
$anuncio['nm_cidade'],
$anuncio['nm_bairro'],
$anuncio['nm_categoria']);
$anuncios[$contador] = $obj_anuncio - > getCd();
$anuncios[$contador] = $obj_anuncio - > getNmTitulo();
$anuncios[$contador] = $obj_anuncio - > getDsAnuncio();
$anuncios[$contador] = $obj_anuncio - > getCdUsuario();
$anuncios[$contador] = $obj_anuncio - > getNmEstado();
$anuncios[$contador] = $obj_anuncio - > getNmCidade();
$anuncios[$contador] = $obj_anuncio - > getBairro();
$anuncios[$contador] = $obj_anuncio - > getCategoria();
$contador++;#
code...
}
die();
}
}
Vlw by the answer, implemented and ran without error, only I have no idea how to check if the arrey was really populated you have any suggestions ?
– Reignomo
@Reignomo as so?
– Guilherme Nascimento
@Reignomo after the while, before the
die
do this:if (empty($anuncios)) { echo 'Vazio'; } else { echo 'Populado'; }
– Guilherme Nascimento
OK vlw saved me :D
– Reignomo