如何在php中的循环中获取id

问题描述 投票:0回答:1

想要得到2个值id和名称,但我很困惑如何得到它,我想在链接中显示id。下面是示例代码。

 echo "<table width=\"100%\" border=\"1\" cellpadding=\"5\" cellspacing=\"2\" bordercolor=\"#FFFFFF\">";

            $count = 1;
            $id=$_GET['id'];
            $col1 = $col2 = array();
            $rowcount = round(mysqli_num_rows($nquery) / 2);
            while($crow = mysqli_fetch_assoc($nquery)) { 
                if($count > $rowcount) $col2[] = $crow['title'];
                else $col1[] = $crow['title'];
                $count++;
            }
            $counter = 0; // Arrays start with 0
            foreach($col1 as $crow) { // $col1 will always be >= $col2
                $row2 = (isset($col2[$counter])) ? $col2[$counter] : "";
                echo "<tr><td><a href='index.php?page=".$id."'>" . $crow . "</td><td>" . $row2 . "</td></tr>"; 
                $counter++;
            }
            echo "</table>";
          ?>`

id不会显示在链接上。希望有人能提供帮助。谢谢

php mysql
1个回答
0
投票

您正试图从查询字符串中获取$ id值(这是$ _GET超全局的值),而不是从数据库中获取。

如果两个元素(ID和名称)都来自同一个数据库查询,您可以尝试这样的方法:

$crow = mysqli_fetch_assoc($nquery);

foreach ($crow as $row)
{
    echo $row['id'].' is the ID and '.$row['title'].' is the title';
}

如果你仍然在foreach循环中,你可以使用$ row ['id']和$ row ['title']来构建你想要获得的表元素和链接。

© www.soinside.com 2019 - 2024. All rights reserved.