简体   繁体   English

在PHP中将变量插入href不能正常工作

[英]insert variable into href in php not working

Been struggling with this for hours. 挣扎了几个小时。 Followed several similar examples I found online, none identical situation, and can't get it to pass the variable. 跟随我在网上发现的几个类似示例,情况完全不同,无法让它传递变量。 I know it's basic, but just learning. 我知道这是基本的,但只是学习而已。 Thanks in advance. 提前致谢。

<?php
        foreach($arr as $r) {
            echo "<tr>";
            echo "<td>".$r['TimeStamp']."</td>";
            echo "<td>".$r['LocationName']."</td>";
            **echo "<td><a href='details_get.php?id='$r['Post_ID']>".$r['Title']."</a></td>";**
            echo "<td>".$r['Price']."</td>";
            echo "<td>".$r['Description']."</td>";
            echo "</tr>";
        }
        ?>

I added a variable $id to make the code clearer and easier to work with, but still can't pass the value. 我添加了一个变量$ id,以使代码更清晰,更易于使用,但仍然无法传递该值。

<?php
        foreach($arr as $r) {
            $id = $r['Post_ID'];
            echo "<tr>";
            echo "<td>".$r['TimeStamp']."</td>";
            echo "<td>".$r['LocationName']."</td>";
            echo "<td><a href='details_get.php?id='.$id.'>".$r['Title']."</a></td>";
            echo "<td>".$r['Price']."</td>";
            echo "<td>".$r['Description']."</td>";
            echo "</tr>";
        }
        ?>

Finally got it to work, but I had to separate my php foreach loop and html as found in this post works . 最后得到它的工作,但我不得不在这个帖子找到我的PHP foreach循环和HTML代码中分离作品

        <?php foreach($arr as $r) : ?>                
            <tr>
                <td><?php echo $r['TimeStamp']; ?></td>
                <td><?php echo $r['LocationName']; ?></td>
                <td><a href="details_get.php?id=<?php echo $r['Post_ID']; ?>"><?php echo $r['Title']; ?></a></td>
                <td><?php echo $r['Price']; ?></td>
                <td><?php echo $r['Description']; ?></td>
            </tr>

        <?php endforeach; ?>
        **echo "<td><a href='details_get.php?id='$r['Post_ID']>".$r['Title']."</a></td>";**

this row should look like 这行应该看起来像

echo <td><a href="details_get.php?id=".$r['Post_ID'].">".$r['Title']."</a></td>";

You are missing a dot in the $r['Post_ID'] I think. 我认为您在$ r ['Post_ID']中缺少一个点。

If you share more information may be I can give you others advises 如果您分享更多信息,我可以给您其他建议

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM