通过回显自动从 mysql 中检索许多图像并在点击时放大它们

Retrieving many images from mysql automatically by an echo and enlarging them on click

我有一个 PhP 汽车配件购物车,我需要能够在单击它时放大检索到的图像。问题是我无法手动为每张图片分配 ID,因为我正在从数据库中回显它们。

我试过将一张 ID/class 分配给所有检索到的图片,并使用 JS 函数在单击时放大它们,但无法正常工作。

    $items_array = $db_controller->runQuery("SELECT * FROM mystuff ORDER BY ID ASC");
    if (!empty($items_array)) { 
        foreach($items_array as $key=>$value){
    ?>

            <div class="DisplayCartItems">

            <form method="post" action="?action=add&mscode=<?php echo $product_array[$key]["mscode"]; ?>">
            <img src="<?php echo $product_array[$key]["msimage"]; ?>" class="imgcartdisplay" align="alignnone"/> /* Right here I need some help */
            <div class="lif"><strong>Category:</strong> <?php echo $product_array[$key]["mscategory"]; ?></div>
            <div class="lif"><strong>Category №:</strong> <?php echo $product_array[$key]["mscatnum"]; ?></div>
            <div class="lif"><strong>Stock №:</strong> <?php echo $product_array[$key]["msnomer"]; ?></div> 
            <div class="lif"><strong>Mark:</strong> <?php echo $product_array[$key]["msmark"]; ?></div>
            <div class="lif"><strong>Model:</strong> <?php echo $product_array[$key]["msmodel"]; ?></div>
            <div class="lif"><strong>Year:</strong> <?php echo $product_array[$key]["msyear"]; ?></div>
            <div class="lif"><strong>Price:</strong> <?php echo "$".$product_array[$key]["msprice"]; ?></div>
            <div style="text-align:center"><input type="number" name="oap" placeholder="Offer a price:"></div>
            <br class="">

            <div class="cartan">
                Quantity: <input type="text" class="product-quantity" name="quantity" value="1" size="2" />

                <br class="">
                <input type="submit" onclick="AddCartAlert()" value="Add to cart" /></div>

    </form>
                </div>
    <?php
        }
    }
    ?>```

I think if I can somehow assign an ID or class name for every picture that get displayed from the MySql and onclick change it's width/height, that'd do the job.

如果我理解正确的话:

你应该替换这个:

<img src="<?php echo $product_array[$key]["msimage"]; ?>" class="imgcartdisplay" align="alignnone"/> /* Right here I need some help */

与:

<a target="_blank" href="<?php echo $product_array[$key]["msimage"]; ?>"><img src="<?php echo $product_array[$key]["msimage"]; ?>" class="imgcartdisplay" align="alignnone"/></a>

用于在另一个选项卡中打开图像。

或者...如果你的意思是这样的https://lokeshdhakar.com/projects/lightbox2/#examples你必须使用javascript(例如:<a href="images/image-1.jpg" data-lightbox="image-1" data-title="My caption">Image #1</a>

在你的情况下是这样的:(当然你需要添加灯箱脚本:https://lokeshdhakar.com/projects/lightbox2/#getting-started

<a data-lightbox="image-1" data-title="My caption" href="<?php echo $product_array[$key]["msimage"]; ?>"><img src="<?php echo $product_array[$key]["msimage"]; ?>" class="imgcartdisplay" align="alignnone"/></a>