Help with PHP Code and Javascript

Asked

Viewed 231 times

0

I need to bring only the products referring to the selected category, when I click on:

<a class="cat-link" href="javascript:;" title="<?php echo $res['nome_cat']; ?>">
    <img style="position:relative; top:50%; transform:translateY(-50%);"
    src="img_cate/<?php echo $res['img_cat']; ?>" width="100"
    title="<?php echo $res['nome_cat']; ?>" />
</a>

I’m using the PHP code below,

<?php include "conexao.php"; ?>
<div id="produtos-content">
<!-- Listando os Categorias -->
<?php
$nome_cat = $_POST['nome_cat'];
$img_cat = $_POST['img_cat'];
$query_categoria = mysql_query("SELECT * FROM categoria");
while($res = mysql_fetch_array($query_categoria)){
?>
<div style="float:left; width:112px; height:133px; padding:2px 10px;" id="cats-list">
        <a class="cat-link" href="javascript:;" title="<?php echo $res['nome_cat']; ?>">
            <img style="position:relative; top:50%; transform:translateY(-50%);"
            src="img_cate/<?php echo $res['img_cat']; ?>" width="100"
            title="<?php echo $res['nome_cat']; ?>" />
        </a>
        <div style="background:#2f2140; ; width:880px;z-index:999;border-radius:25px;" class="single-cat">
            <h1 align="center" style="width:830px; margin-top:-40px;"><?php echo $res['nome_cat']; ?></h1>
            <div style="margin-left:357px;width:50px;" class="cat-links">
                <a class="close-btn" href="javascript:;" title="Voltar">
                    <img src="img/fechar.jpg" />
                </a>
            </div>
<?php
$codigo = $_POST['codigo'];
$img_prod = $_POST['img_prod'];
$titulo = $_POST['titulo'];
$descricao = $_POST['descricao'];
$nome_cat = $_POST['nome_cat'];
$query_produtos = mysql_query("SELECT * FROM produtos WHERE nome_cat = '$nome_cat'") or die(mysql_error());
while($res = mysql_fetch_array($query_produtos)){
    ?>
    <div style="float:left; width:112px; height:133px; padding:2px 10px;" id="songs-list">
    <a class="song-link" href="javascript:;" title="">
        <img style="position:relative; top:50%; transform:translateY(-50%);" src="img_prod/<?php echo $res['img_prod']; ?>" width="100" title="<?php echo $res['titulo']; ?>" />
        </a>

        <div style=" margin-top:120px;z-index:999;border-bottom-right-radius:25px;border-top-right-radius:25px;" class="single-song">
        <div style="width:50px;margin-left:300px;" class="song-links">
            <a class="close-btn" href="javascript:;" title="Voltar">
                <img src="img/fechar.jpg" />
            </a>
                <div class="song-sides">
                    <img style="margin:40px 0 0 -142px;" src="img_prod/<?php echo $res['img_prod']; ?>" width="369" />
                </div>
            </div>
            <h1> <?php echo $res['titulo']; ?> </h1>
            <div class="entry" style="overflow-y: hidden; padding: 0px; width: 100px;">
            <p style="font-family:Verdana, Geneva, sans-serif; size:14px; font-weight:bold; color:#ccc;">
            <?php echo nl2br($res['descricao']); ?>
            </p>
            <div class="jspContainer" style="width: 100px; height: auto;">
            <div class="jspPane" style="padding: 0px; top: 0px; width: 100px;">
            </div></div>
            </div>
            <span class="song-sides left-side"></span>
        </div>
    </div>
    <?php } ?>
        </div>
    </div>
    <?php
    }
    ?>
    </div>
</div>

but when I insert the WHERE nome_cat = '$nome_cat' it does not bring me anything, but when the retreat, it brings me all the products listed in the PRODUCTS table, regardless of the selected category.

I did some research and I think I need to create an event handler, but I’m very Javascript layman.

If friends can help me out, I’d be grateful.

  • Already gave a print_f on $cat name to see if there really is something ?

  • Your <a> tag needs the attribute name and value to be available in the back-end.

  • Hello @Nevershowmyface, you can give me an example, because I’m really very lay in Javascript.

  • @Murilocabral adds the code of his complete <form> . Somewhere there must be an <input> whose attribute name be it nome_cat.

  • 1

    From what I saw in your code, you are not sending anything via POST. Do the following: In the link, where is href="javascript:;" put href="? name_cat=<? php echo $res['name_cat']; ? >" and where you declare the variable $name_cat = $_POST['name_cat'];, put $name_cat = $_GET['name_cat'];

  • Hello @Givanildo R. de Oliveira, I followed your guidance, and it almost worked. I’m posting the address (http://www.lccinformatica.com.br/) so you can check what’s going on after the change. Because when I select a category, it brings me the products related to the selected category, but quickly it goes back to the beginning. What could be causing this?

  • 1

    It goes back to the beginning because a refresh is done on the page. To solve you do the following: Leave the div . single-cat as follows: <div style="background:#2f2140; ; width:880px;z-index:999;border-Radius:25px; <?php echo $res['nome_cat']=$_GET['nome_cat'] ? 'display:block;' '; '; ? >" class="single-cat">

  • Show @Givanildo R. de Oliveira, worked the way I needed, thank you so much for your help. I will be posting the code that is working in an answer so that other users with the same problem may have the solution. Again BRAWL.

  • Check the address (http://www.lccinformatica.com.br/) how is working after the tip of friend @Givanildo R. de Oliveira, whom I thank for the help.

  • OK @Murilo Cabral, I’m happy to help. I would just like to give a hint. I believe that the best solution for what you are doing is using ajax. When you can, do a little research on how to use $.get, $.post, $.ajax from jQuery blz. See you later +

  • Thanks for this tip @Givanildo R. de Oliveira, I’ll give a study yes, as you can see I’m trying to learn and venturing into this wide world of WEB programming. Big hug and be at peace friend.

  • Here I am again @Givanildo R. de Oliveira, rsrsrsr... Next, I inserted an opening page (See at http://www.lccinformatica.com.br/) , and when I select any of the Categories, it sends me to that new opening page. Not to impose, but already abusing what I do so that it does not happen.

  • There in href where we leave so href="? name_cat=<? php echo $res['nam_cat']; ? >", add the #products after the category, thus: href="? name_cat=<? php echo $res['name_cat']? >#products"

Show 9 more comments

1 answer

1


With the help of our friend @Givanildo R. de Oliveira, we were able to solve the problem and bring only the products related to the selected category.

Down with the working code:

<?php include "conexao.php"; ?>

<div id="produtos" class="sections full-width-wrapper bg_4">
    <div class="centered-wrapper">

        <div class="section-header">
            <?php include 'menu_pags.php';?>            
        </div>

<div id="produtos-content">

    <!-- Listando os Categorias -->
    <?php
    $nome_cat = $_POST['nome_cat'];
    $img_cat = $_POST['img_cat'];
    $query_categoria = mysql_query("SELECT * FROM categoria");
    while($res = mysql_fetch_array($query_categoria)){
    ?>
    <div style="float:left; width:112px; height:133px; padding:2px 10px;" id="cats-list">
        <a class="cat-link" href="?nome_cat=<?php echo $res['nome_cat']; ?>#produtos" title="<?php echo $res['nome_cat']; ?>">
        <img style="position:relative; top:50%; transform:translateY(-50%);"
            src="img_cate/<?php echo $res['img_cat']; ?>" width="100"
            title="<?php echo $res['nome_cat']; ?>" />
        </a>
        <div style="background:#2f2140; ; width:880px;z-index:999;border-radius:25px; <?php echo $res['nome_cat']==$_GET['nome_cat'] ? 'display:block;' : ''; ?>" class="single-cat">
            <h1 align="center" style="width:830px; margin-top:-40px;"><?php echo $res['nome_cat']; ?></h1>
            <div style="margin-left:357px;width:50px;" class="cat-links">
                <a class="close-btn" href="javascript:;" title="Voltar">
                    <img src="img/fechar.jpg" />
                </a>
            </div>

<!-- Listando os produtos -->
<?php
$nome_cat = $_GET['nome_cat'];
$query_produtos = mysql_query("SELECT * FROM produtos WHERE nome_cat = '$nome_cat'") or die(mysql_error());
while($res = mysql_fetch_array($query_produtos)){

$codigo = $_POST['codigo'];
$img_prod = $_POST['img_prod'];
$titulo = $_POST['titulo'];
$descricao = $_POST['descricao'];
$nome_cat = $_POST['nome_cat'];
?>

    <div style="float:left; width:112px; height:133px; padding:2px 10px;" id="songs-list">
            <a class="song-link" href="javascript:;" title="">
            <img style="position:relative; top:50%; transform:translateY(-50%);" src="img_prod/<?php echo $res['img_prod']; ?>" width="100" title="<?php echo $res['titulo']; ?>" /></a>

        <div style=" margin-top:120px;z-index:999;border-bottom-right-radius:25px;border-top-right-radius:25px;" class="single-song">
            <div style="width:50px;margin-left:300px;" class="song-links">
            <a class="close-btn" href="javascript:;" title="Voltar">
                <img src="img/fechar.jpg" />
            </a>
                <div class="song-sides">
                    <img style="margin:40px 0 0 -142px;" src="img_prod/<?php echo $res['img_prod']; ?>" width="369" />
                </div>
            </div>

            <meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />

            <h1> <?php echo $res['titulo']; ?> </h1>
            <div class="entry" style="overflow-y: hidden; padding: 0px; width: 100px;">
            <p style="font-family:Verdana, Geneva, sans-serif; size:14px; font-weight:bold; color:#ccc;">
            <?php echo nl2br($res['descricao']); ?>
            </p>
            <div class="jspContainer" style="width: 100px; height: auto;">
            <div class="jspPane" style="padding: 0px; top: 0px; width: 100px;">
            </div></div></div>
            <span class="song-sides left-side"></span>
        </div>
    </div>
<?php
}
?>

        </div>
    </div>
    <?php
    }
    ?>
</div>

</div></div></div>

I hope I can help other users with the same problem.

Browser other questions tagged

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