PHP in_array() function

Asked

Viewed 372 times

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 arrayand 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

inserir a descrição da imagem aqui

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.

  • inserir a descrição da imagem aqui



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 a function() 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 of in_array().

  • 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.

  • You can put the SELECT in a variable and in the if call again the $conn->prepare($variavel).

  • 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

2 answers

2


I believe that a do-while resolve:

$categoriasAnuncioAtual = explode(',', $rowCategoriasAnuncioAtual->anuc_cat_id);

do{
    $categoriaRandomAtual = $categoriasAnuncioAtual[array_rand($categoriasAnuncioAtual)];
    $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 = ? AND ac.anuc_cat_id
                                            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);

}while(!in_array($categoriaRandomAtual, $arraycategoriasbanco));

The code above runs the risk of going into an infinite loop.

To avoid this, I would remove the selected category from $categoriasAnuncioAtual and add the condition !empty($categoriasAnuncioAtual) within the condition of while.

  • Exactly that! Only one question, which exact function do has to complement a while, did not know this function.

  • Would there be a way for me to avoid the risk of an infinite loop? Or this way it would be without using the while and trying another logic?

  • are equal except for the moment they perform the output condition of the loop. do-while executes at the end and while at the beginning

  • @Tiagoboeing already complemented the answer

  • Thank you, exactly what I needed!

0

You can do with A function that runs the SELECT and then a while until the condition is met.

function getCategoriasBanco($conn, $rowId)
{
    $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 = ? AND ac.anuc_cat_id
                                        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);

    return $arraycategoriasbanco;
}

$arraycategoriasbanco = getCategoriasBanco($conn, $rowId);
//busca pelo array random dentro da variavel de categorias do anuncio
while(!in_array($categoriaRandomAtual, $arraycategoriasbanco))
{
    $arraycategoriasbanco = getCategoriasBanco($conn, $rowId); 
}
  • This does what it seems he is asking but I doubt it solves the problem in fact. But I don’t know, the question does not clearly indicate the purpose. XY.

  • I added a few screenshots and a few more details to try to exclaim.

  • @moustache focused only on responding to the request.

  • 1

    Thank you for your reply. I was able to resolve the while question by checking if it meets the conditions and reexecuting negative case.

Browser other questions tagged

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