简体   繁体   English

如何在PHP循环中获取ID

[英]How to get id while in a loop in php

Wanted to get 2 values id and name but im confuse how get it, i wanted to show the id in the link. 想要获取2个值的ID和名称,但我将其混淆,我想在链接中显示ID。 heres the sample code. 这是示例代码。

 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 wont show up on the link. ID不会显示在链接上。 Hope someone can help. 希望有人能帮忙。 Thanks 谢谢

You're trying to get $id values from the query string (that's what the $_GET superglobal is) and not from the database. 您尝试从查询字符串($ _GET超全局变量就是这个)而不是从数据库获取$ id值。

If both elements (ID and name) are coming from the same database query, you may try something like this: 如果两个元素(ID和名称)都来自同一数据库查询,则可以尝试如下操作:

$crow = mysqli_fetch_assoc($nquery);

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

if you remain within the foreach loop, you can use $row['id'] and $row['title'] to build the table element and link you're trying to obtain. 如果您仍处于foreach循环中,则可以使用$ row ['id']和$ row ['title']来构建表元素和您尝试获取的链接。

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

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