获取第二个数组中每个数组元素的ID

时间:2013-06-04 23:47:29

标签: php mysql arrays

我需要获取第二个数组中每个数组元素的唯一ID。该ID已存在于表中,但我无法单独获取它们。我现在获得的网址如下所示:http://page.com/index.php?p=view&m=area&id=173id=552id=768id=36id=217id= 我只需要一个ID,如果先使用第二个,依此类推。 我知道我应该使用mysqli或PDO和规范化的表格,但是后来我需要帮助。

这是代码:

$res= mysql_query("SELECT * FROM area WHERE user='$user' ORDER BY date") or     die("Error: " . mysql_error());
while($row = mysql_fetch_assoc($res))
{
   $id = $row['id'];
   $x = array();
   $parent = array();
   foreach($row as $value)
   {
      if ($value == $id) continue;

      else if ($value == $user) continue;

      $result = explode(",", $value);

      foreach($result as $newvalue)
      {
         $query = "SELECT x,firm FROM list where list.x='$newvalue'";
         $result = mysql_query($query);
         $r = mysql_fetch_assoc($result);
         $x[] = $r['x'];
         $xx = implode("id=",$x);
         $parent[] = $r['firm'];
         $list = implode("<a href='index.php?p=view&m=area&$xx'>", $parent)."</a>";
      }
    }
echo "<td><span>" . $list . "</span>/td>";

}

谢谢

2 个答案:

答案 0 :(得分:1)

首先

$list = implode("<a h

应该是

$list .= implode("<a h

答案 1 :(得分:0)

该URL语法不是将值数组传递给PHP脚本的正确方法。它应该使用PHP数组语法作为参数名称。此外,您需要使用&符号(&)分隔您的参数:

 http://page.com/index.php?p=view&m=area&id[]=173&id[]=552&id[]=76&8id[]=36&id[]=217

然后你可以使用

获得第二个
$second_id = $_GET['id'][1]; // 552

仅供参考,you shouldn't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial