dongye6377 2017-07-25 03:56
浏览 29
已采纳

单击图像并在其他页面中显示

I need to click the image and link to another page based on the product that is clicked, my page always display the same product even when i click other products. Below is the code for the main page

        <?php  
    $query = "SELECT * FROM tbl_product ORDER BY id ASC"; //order by ID ascending order  
    $result = mysqli_query($connect, $query);  
    while($row = mysqli_fetch_array($result))  
    {  
    ?>
<div id="products" class="productsContainer">
    <div class="responsive">   <!-- OUTER BOX OF PRODUCT -->
        <div class="gallery"> <!-- INNER BOX FOR PRODUCT -->
                <a href="box.php?id=".$row['id']><img src="images/<?php echo $row["image"]; ?>" class="img-responsive" /></a><br /> <!-- IMAGE OF PRODUCTS -->
                <h4 class="text-info"><?php echo $row["name"]; ?></h4>  <!-- NAME OF PRODUCT -->
                <h4 class="text-danger">$ <?php echo $row["price"]; ?></h4>  <!-- PRODUCT PRICE -->
                <input type="text" name="quantity" id="quantity<?php echo $row["id"]; ?>" class="form-control" value="1" />  <!-- QUANTITY PRODUCT -->
                <input type="hidden" name="hidden_name" id="name<?php echo $row["id"]; ?>" value="<?php echo $row["name"]; ?>" />  <!-- NOT SHOWN -->
                <input type="hidden" name="hidden_price" id="price<?php echo $row["id"]; ?>" value="<?php echo $row["price"]; ?>" />  <!-- NOT SHOWN -->
                <input type="button" name="add_to_cart" id="<?php echo $row["id"]; ?>" style="margin-top:5px;" class="btn btn-warning form-control add_to_cart" value="Add to Cart" />  <!-- ADD TO CART BUTTON -->

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

Here is another page code(when clicked into the product)

    <div class="containertype1">
                <div>
                <img id="mainImage" style="border:3px solid grey" 
     src="  images/005.jpg" height="500px" width="540x"/>
<br />

<div id="divId" onclick="changeImageOnClick(event)">
<?php

    echo "<img class='imgStyle' src='images/001.jpg' />";
    echo "<img class='imgStyle' src='images/002.jpg' />";
    echo "<img class='imgStyle' src='images/003.jpg' />";
    echo "<img class='imgStyle' src='images/004.jpg' />";
    echo "<img class='imgStyle' src='images/005.jpg' />";
?>
</div>
   <script type="text/javascript">

    var images = document.getElementById("divId")
                         .getElementsByTagName("img");

    for (var i = 0; i < images.length; i++)
    {
        images[i].onmouseover = function ()
        {
            this.style.cursor = 'hand';
            this.style.borderColor = 'red';
        }
        images[i].onmouseout = function ()
        {
            this.style.cursor = 'pointer';
            this.style.borderColor = 'grey';
        }
    }


    function changeImageOnClick(event)
    {
        event = event || window.event;
        var targetElement = event.target || event.srcElement;

        if (targetElement.tagName == "IMG")
        {
            mainImage.src = targetElement.getAttribute("src");
        }
    }

</script>
        </div>
</div>
  • 写回答

1条回答 默认 最新

  • doulai1910 2017-07-25 04:04
    关注

    The problem is that you forgot to use your opening PHP tag before attempting to write a PHP variable ($row['id']).

    Consider the following:

    <a href="box.php?id=".$row['id']>
        <img src="images/<?php echo $row["image"]; ?>" class="img-responsive" />
    </a>
    

    In the above, your link points to box.php?id=. You never actually open your PHP tag to reference $row['id']. When your HTML reaches the dot, it gets confused, as it is expecting an attribute for the <a> tag (like href).

    To correct this, simply make sure to open (and close) the PHP tags, and echo out the $row['id'] in the following format:

    <a href="box.php?id=<?php echo $row['id']; ?>">
        <img src="images/<?php echo $row["image"]; ?>" class="img-responsive" />
    </a>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器