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 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'