如何使用获取通过id选择的项


how to use the get the item choosen through the id

如何将$_get方法添加到此代码中,并使其显示为"这是另一页中的苹果"

<div class="col-md-3 col-sm-6 post">
<a href="fruit.php?id=1"> <img src="image/apple.jpg"></a> <h4> apple</h4>
 </div>

如何将$_get方法添加到该代码中,并使其显示为"这是另一页中的橙色"

<div class="col-md-3 col-sm-6 post">
                <a href="fruit.php?id=2">
                    <img src="image/orange.jpg"></a>
                <h4>orange</h4>
        </div>

fruit.php页面中,您可以执行以下操作:

<?php
if(isset($_GET["id"]))
{
    if($_GET["id"]==1)
    {
        echo "This is an apple in another page";
    }
    else if($_GET["id"]==2)
    {
        echo "This is an orange in another page";
    }
}
?>

尝试:

<?php
if(isset($_GET["id"]))
{
    if($_GET["id"]==1)
    {
        echo "This is an apple";
    }
    else if($_GET["id"]==2)
    {
        echo "This is an orange";
    }
}
?>

您可以使用查询字符串(fruit.php?id=1&fruit_name=applefruit.php?id=1&fruit_name=orange

<div class="col-md-3 col-sm-6 post">
  <a href="fruit.php?id=1&fruit_name=apple"><img src="image/apple.jpg"></a><h4> apple</h4>
</div>
<div class="col-md-3 col-sm-6 post">
  <a href="fruit.php?id=2&fruit_name=orange"><img src="image/orange.jpg"></a><h4> Orange</h4>
</div>

fruit.php页面中,您可以获得如下的水果名称

<?php if( isset($_GET['fruit_name']) ){ echo 'This is an '.$_GET['fruit_name']; } ?>