从PHP请求填充Javascript数组

时间:2019-03-30 17:52:06

标签: javascript php html

我想用我的PHP查询到数据库的结果填充JavaScript数组。我最初用变量填充它,但我想从数据库中获取这些变量。我将查询结果放在“ liste”变量中,不知道是否可以使用foreach进行填充,谢谢您的帮助。

PHP代码:

<?php
        $sql="SELECT * FROM  produits WHERE cat='Interieur'";
        $req=$db->prepare($sql);
        $req->execute();
        $liste=$req->fetchAll(PDO::FETCH_OBJ);
?>

<?php foreach ($liste as $product): ?> ````

JavaScript代码:

var arrayTitle = ["Chaise Skandinave", "Chaise2", "Chaise3","Table1","Table2","Table3","Fauteuil1","Fauteuil2","Fauteuil3"];
var arrayImage = ["images/ChaiseSkandinave.jpg", "images/Chaise2.jpg", "images/Chaise3.jpg","images/Table1.jpg","images/Table2.jpg","images/Table3.jpg","images/Fauteuil1.jpg","images/Fauteuil2.jpg","images/Fauteuil3.jpg"];
var arrayDesc = ["a", "b", "c","d","e","f","g","h","i"];
var modal = document.getElementById('myModal');

function openModal(i)
{
    modal.style.display = "block";

    $(document).ready(function()
    {
            document.getElementsByClassName("modalimg")[0].src = arrayImage[i];
            document.getElementsByClassName("modaltitle")[0].innerHTML = arrayTitle[i];
            document.getElementsByClassName("modaldesc")[0].innerHTML = arrayDesc[i];
    });
}

HTML代码:

        <div class="container">
            <div class="row animate-box">
                <div class="col-md-8 col-md-offset-2 text-center fh5co-heading">
                    <span>Meubles Interieurs</span>
                    <h2>Produits</h2>
                    <p></p>
                </div>
            </div>
            <div class="row" id="row1">
            </div>
            <?php
            $sql="SELECT * FROM  produits WHERE cat='Interieur'";
            $req=$db->prepare($sql);
            $req->execute();
            $liste=$req->fetchAll(PDO::FETCH_OBJ);
            ?>
            <?php foreach ($liste as $product): ?>

                <div class="col-md-4 text-center animate-box">
                <div class="product">
                    <div class="product-grid" style="background-image:url(images/<?php echo $product->num; ?>.jpg)">
                        <div class="inner">
                            <p>
                                <a href="single.html" class="icon"><i class="icon-shopping-cart"></i></a>
                                <a href="javascript:openModal()" class="icon"><i class="icon-eye"></i></a>
                            </p>
                        </div>
                    </div>
                    <div class="desc">
                        <h3><a href="single.html"><?php echo $product->nom; ?> </a></h3>
                        <span class="price"><?php echo number_format($product->prix,2,',',''); ?> TND</span>
                    </div>
                </div>
                </div>

            <?php endforeach ?>
        </div>
    </div>

0 个答案:

没有答案