for() is showing the same array value

Asked

Viewed 48 times

0

Good afternoon, I’m trying to do a for() that shows the values for the current category. I use a function that takes the company and the category and selects the amount of records to show. Soon after I use a very similar function to show the products inside the for. but it is showing me several times the same product.

Follow the Roles:

function selectProdutos($idCategoria, $idEmpresa){
    require('admin/Connections/pennaConect.php');

    mysql_select_db($database_pennaConect, $pennaConect);
    $query_ListaProdutos = sprintf("SELECT * FROM tbl_produto 
                                    WHERE tbl_categoria_id = %s 
                                    AND tbl_empresa_id = %s 
                                    ORDER BY codigo_original", 
                                        GetSQLValueString($idCategoria, "int"),
                                        GetSQLValueString($idEmpresa, "int")
                                );
    $ListaProdutos = mysql_query($query_ListaProdutos, $pennaConect) or die(mysql_error());
    $row_ListaProdutos = mysql_fetch_assoc($ListaProdutos);
    $totalRows_ListaProdutos = mysql_num_rows($ListaProdutos);

    return $row_ListaProdutos;
}

function selectCountProdutos($idCategoria, $idEmpresa){
    require('admin/Connections/pennaConect.php');

    mysql_select_db($database_pennaConect, $pennaConect);
    $query_ListaProdutos = sprintf("SELECT * FROM tbl_produto 
                                    WHERE tbl_categoria_id = %s 
                                    AND tbl_empresa_id = %s 
                                    ORDER BY codigo_original", 
                                        GetSQLValueString($idCategoria, "int"),
                                        GetSQLValueString($idEmpresa, "int")
                                );
    $ListaProdutos = mysql_query($query_ListaProdutos, $pennaConect) or die(mysql_error());
    $row_ListaProdutos = mysql_fetch_assoc($ListaProdutos);
    $totalRows_ListaProdutos = mysql_num_rows($ListaProdutos);

    return $totalRows_ListaProdutos;
}

and in HTML:

            <?php for($i=0;$i<selectCountProdutos($row_ListaCategoria['CatId'] , $row_listaFabrica['id']);$i++){ ?>
                <div class="col-lg-3 col-md-3 col-xs-12 col-sm-6 col-lg-3-edit thumb">
                    <div class="box clearfix">

                        <!--IMAGEM PRODUTO-->
                        <a href="img/produtos/id/101.1032.png" class="thumbnail text-center thumbnail-edit thumbnail_wrapper no-border-radius pop" data-toggle="lightbox" data-title="CHICOTE PARA REPARO ALTERNADOR VW/CHICOTE P/REPARO SENSOR PRESS" data-footer="TC Chicotes">
                            <img class="img-responsive img-form-edit" src="img/produtos/id/101.1032.png" alt="#" />
                        </a>

                        <!--INFORMAÇÕES PRODUTO-->
                        <div class="product-text">
                        <?php $result = selectProdutos($row_ListaCategoria['CatId'], $row_listaFabrica['id']);?>
                            <h4><?php echo $result['nome']; ?></h4>
                            <p>
                                <strong>Código:</strong> <?php echo $result['codigo_original']; ?><br>
                                <strong>Aplicação:</strong> <?php echo $result['aplicacao']; ?><br>
                                <strong>Obs:</strong><?php echo $result['descricao']; ?>
                            </p>
                        </div>

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

Can you help me? I don’t understand what’s wrong and much less how to fix.

I thank all those who have help!

2 answers

0


His method selectProdutos returns only the first product. The mysql_fetch_assoc returns a value each time you use it, so you must store all the data in vectors.

/*...*/
while($line = mysql_fetch_assoc($ListaProdutos))
    $row_ListaProdutos[] = $line;
/*...*/
return $row_ListaProdutos;

Then all information will return within your vector.

And in your html:

<?php
$result = selectProdutos($row_ListaCategoria['CatId'], $row_listaFabrica['id']);
for($i=0;$i<selectCountProdutos($row_ListaCategoria['CatId'] , $row_listaFabrica['id']);$i++){ ?>
...
<div class="product-text">
    <h4><?php echo $result[$i]['nome']; ?></h4>
    ...
</div>
...

... represents the code snippet until the next tag

  • I really wasn’t getting it. In my idea mysql_fetch_assoc was returning me array. Thank you very much!

-1

Guy hit his eye fast, on the line of "for", I guess I’d have to stay that way:

<?php $result = selectProdutos($row_ListaCategoria[$i], $row_listaFabrica[$i]);?>

Like you’re riding that $row_ListaCategoria and $row_listaFabrica?
If it’s for mysql_fetch_assoc, try to change to mysql_fetch_array.

  • Hello! all quiet, I managed to resolve with the help of the colleague above. the error was that it was not returning an array. Your solution could help too, then I’ll try. Thank you very much for your help!

Browser other questions tagged

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