dsfdsf21321 2017-06-03 22:50
浏览 50
已采纳

PHP循环没有抛出正确的链接[重复]

This question already has an answer here:

for my issue i'll try to be as brief as possible

what am trying to do is reference a page with a specific id in the HTML anchor link with PHP as the below

<body>
<?php  $linkName = "Second Page"; ?>
<?php $id = 5; ?>

<a href="secondPage.php?id=<?php echo $id;?>"><?php echo $linkName?></a><br>

and it works fine

now what am trying to do is to make the $id part more dynamic by making looping the number from 1 to 10 and also providing 10 links

the code is

</head>
<body>
    <?php  $linkName = "Second Page"; ?>
    <?php $id = 5; ?>

    <?php  
        for ($i=0; $i < 10 ; $i++) { 
            echo "<a href='secondPage.php?id=<?php echo $i;?'>Link1</a>";
        };

    ?>
</body>

however what i did notice as the below images indicates when i hover on the links i noticed that i refer to a strange link

Strange Link

and when i cliched on it it takes me to the following link with an id that i did not want as below

http://localhost/PHP_Course/secondPage.php?id=%3C?php%20echo%201;?

i tried researching the subject and i tried escaping the quotation but it does not seem to resolve the problem

Any help please ??

</div>
  • 写回答

4条回答 默认 最新

  • doubi7306 2017-06-03 22:57
    关注

    <?php and ?> tags indicate to the PHP preprocessor that anything inside them is code and needs to be parsed, everything outside is just text PHP doesn't touch.

    Inside the <?php tag, "<?php" string has no special meaning, so is printed. You do not need to open and close tags all the time, try this:

    </head>
    <body>
        <?php
            $linkName = "Second Page";
            $id = 5;
    
            for ($i = 0; $i < 10 ; $i++) { 
                echo "<a href='secondPage.php?id=$i;'>Link1</a>";
            };
        ?>
    </body>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal