如何获取页面的id
?
我有30个链接的列表,它们看起来像:
http://test.com/composition.php?=9
这是我的代码:
(index.php)
<?
$q = array();
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$q[$row['id']]=$row['header'];
}
}
?>
<?
foreach($q as $href => $text)
{
echo '<a href="http://test.com/composition.php?=' . $href . '">' .'<li>'. $text .'</li>' .'</a>';
}
?>
当我点击链接时,如何在composition.php
页面获得$href
?
我尝试了$_SESSION[href]=$href;
,但它总是显示所有链接的最后一个id(=30),我需要我点击过的那个。
很抱歉没有问题,我是php的新手,不知道如何解决这个问题。
您需要为$href
值创建一个密钥,以便使用$_GET
数组访问它:
foreach($q as $href => $text) {
echo '<a href="http://test.com/composition.php?id=' . $href . '">' .'<li>'. $text .'</li>' .'</a>';
}
然后在composition.php:
$href = isset($_GET['id']) ? $_GET['id'] : null;
查询值(传递到?
后面的url中的值)是使用$_REQUEST
超全局或$_GET
超全局访问的键值对
更改
<a href="http://test.com/composition.php?=' . $href . '">
至
<a href="http://test.com/composition.php?get=' . $href . '">
并在php文件上获取id
$id = $_GET['get'];
欢迎