Foreach with 2 conditions

Asked

Viewed 386 times

2

I’m trying to put two conditions on the foreach, but I’m not succeeding. The idea is as follows: the client accesses the site in the following URL: /room/balcony and appears only products that have in their register in DB the environment 'room' and the category 'countertop'.

The code is the one I’m using

.htaccess

RewriteRule ^produtos/(.*)$ produtos.php?id=$1&cat=$2

Consultation page

$ProdAmb=  $_GET['id'];
$ProdCat= $_GET['cat'];

$muda_path = explode('/', $ProdAmb);
$muda_path1 = explode('/', $ProdCat);

foreach($muda_path as $produto_link){
    $ProdID = $produto_link;
    $subcat = $sub;
    $prod_select_categoria = "SELECT * FROM todos_produtos INNER JOIN ambiente_moveis ON ambiente_moveis.nome = todos_produtos.ambiente_prod INNER JOIN categorias_moveis ON categorias_moveis.nome = todos_produtos.categoria_prod WHERE categorias_moveis.link_cat='$subcat' OR ambiente_moveis.link_cat='$ProdID'";
    $query = @mysql_query($prod_select_categoria) or die (mysql_error());
}

The table 'all products' contains the two terms, environment and category, while the table 'ambiente_prod' only has environments and 'categoria_moveis' only has categories, both tables(environment and category) the structure is equal, changing only the data. As you can see, the foreach has only one condition, but I’ve tried it with && or || and none worked.

1 answer

3


From what I understand:

    foreach($muda_path as $produto_link){
        $ProdID = $produto_link;
        foreach($muda_path1 as $produto_link1){
            $subcat = $produto_link1;
            $prod_select_categoria = "SELECT * FROM todos_produtos INNER JOIN ambiente_moveis ON ambiente_moveis.nome = todos_produtos.ambiente_prod INNER JOIN categorias_moveis ON categorias_moveis.nome = todos_produtos.categoria_prod WHERE categorias_moveis.link_cat='$subcat' OR ambiente_moveis.link_cat='$ProdID'";
            $query = @mysql_query($prod_select_categoria) or die (mysql_error());
        }
    }

If you do not solve your problem try to specify the question and put more data like the table structure print + the values that will go in the URL, I could not understand for sure which data will be exploded and how this SQL should return.

  • Dude, exactly. But I’m having trouble with . htaccess right now. He is not able to return me the first variable of the url, only the 2nd is returning me.

  • Try it like this: Rewriterule (.*)$ products.php? id=$1&cat=$2 [L]

Browser other questions tagged

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