1
I’m developing a system that takes the ID’s from categories that are stored in the database, separated by ,
, am using explode
, turning them into a array
and soon after I’m using the function array_rand()
to draw only one value from this array. Everything works perfectly, but now I need to get other records that contain the same category of this drawn. If the value returns false
(empty), I need to draw again until I find a record with this category, so that it does not occur that the sidebar is without records being displayed.
Viewing an ad
Note for example in this ad the categories are Food and Attendant
I wish in my sidebar to search for other ads that are of the same category and with ORDER BY rand()
, see an example of the sidebar.
Categories are stored in the database as follows:
anuc_cat_id = 1,7,2,9 | ID's das categorias
anuc_anu_id = 2 | Indica a qual anúncio corresponde
Separation of categories and drawing
$categoriasAnuncioAtual = explode(',', $rowCategoriasAnuncioAtual->anuc_cat_id);
$categoriaRandomAtual = $categoriasAnuncioAtual[array_rand($categoriasAnuncioAtual)];
Database query and verification
$selParecidos = $conn->prepare('SELECT * FROM anuncios a
INNER JOIN anuncios_categorias ac ON ac.anuc_anu_id = a.anu_id
INNER JOIN anuncios_fotos af ON af.anuf_anu_id = a.anu_id AND af.anuf_posicao = ?
INNER JOIN usuarios u ON a.anu_usu_id = u.usu_id
WHERE a.anu_url <> ? AND a.anu_status = ? AND a.anu_status_adm = ?
GROUP BY anu_id
ORDER BY rand()');
$selParecidos->execute(array(0, $rowId->anu_url, 1, 1));
$rowParecidos = $selParecidos->fetch(PDO::FETCH_OBJ);
$arraycategoriasbanco = explode(',', $rowParecidos->anuc_cat_id);
//busca pelo array random dentro da variavel de categorias do anuncio
if(in_array($categoriaRandomAtual, $arraycategoriasbanco)){
echo "existe";
}
On the last lines of the code where it follows if(in_array())
I need that if it does not exist it make a new SELECT
until you find a record that matches this if()
.
How can I proceed?
I have no errors in SELECT or too, I just need a way to re-enter select case
if(in_array())
is empty. I thought of making afunction()
but I’m not getting it.
Are you sure you want to keep repeating the
SELECT
? I don’t know if I understand the problem but changing the order won’t change the result ofin_array()
.– Maniero
In case my system shows ads, when I am on the page of a particular ad I have details about it, but I want to display in the sidebar similar ads of the same category as the one I am viewing. I don’t know if repeating select is a good way, but I need it in a way that when the value in the field
anuc_cat_id
does not match the ones in the array it does a new query or something like to search for advertisements that match these parameters.– Tiago Boeing
You can put the
SELECT
in a variable and in the if call again the$conn->prepare($variavel)
.– MeuChapeu
I liked the logic, I came very close to a similar result, it seems to me that Bruno Calza who answered below presented something similar. Thank you
– Tiago Boeing