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

时间:2019-05-24 22:15:12

标签: php mysql image zoom display

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

我尝试为所有要检索的图片分配一个ID /类别,并使用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.

1 个答案:

答案 0 :(得分:0)

如果我正确理解:

您应该替换为:

<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>