【问题标题】:Loop through an array to unset a known value and implode to sql循环遍历数组以取消设置已知值并内爆到 sql
【发布时间】:2011-02-17 19:45:29
【问题描述】:

我在不同的 MYSQL 表中有两组数组。这就是我想做的事情

               What I Want TO Do
   TABLE_ONE            connect to the table.
                        get the value we want from session_id
                        THEN get the array associated with the value (session_id)
                        explode the array to get individual values.
                        NOW::::: - GO TO TABLE_TWO

   TABLE_TWO            Go straight to the first value from array (TABLE_ONE)
                        Explode the array associated with it.
                        Delete the number that's equal to the session_id
                       _____________________________________________________
                       And so fort....

下面更直观的解释: session_id = 4

TABLE_ONE:

   id            array1
   1             4
   2             1
   3             2,5
   4             1,3,4,5
   5             4,5

TABLE_TWO:

   id            array2
   1             4,6,9,2
   2             3,7,8,2
   3             7,12,4,9
   4             1,5,4,8
   5             3,6,12,3,5,4

所以,因为 session_id = 4,我们转到 TABLE_ONE id 4。 id-4 的数组是 1,3,4,5。 所以现在我们知道 4 可以在id 1,3,4,5 of TABLE_TWO 中找到 我们现在应该分解 TABLE_TWO 的数组并从那里的数组中删除 4。内爆数组并将新值保存到数据库。

这就是我所做的 - 它只会从 id-3 中删除“4”并删除 id-4 中的所有值。请帮忙!!

    $SESSION = 4;
    $depSQL = mysql_query("SELECT array1 FROM TABLE_ONE WHERE id='$SESSION' LIMIT 1"); 
while($row=mysql_fetch_array($depSQL)) { $depARRAY = $row["array1"]; }
$explodedDEP = explode(",", $depARRAY);
foreach ($explodedDEP as $key1 => $value1) {

    $stSQL = mysql_query("SELECT array2 FROM TABLE_TWO WHERE id='$value1'"); 
    while($get=mysql_fetch_array($stSQL)) { $stARRAY = $get["array2"];}
    $explodedST = explode(",", $stARRAY);
        foreach ($explodedST as $key2 => $value2) {
              if ($value2 == $SESSION) {
                      unset($explodedST[$key2]);
              }
        }
         $newST = implode(",", $explodedST);
     $sql = mysql_query("UPDATE TABLE_TWO SET array2 ='$newST' WHERE id='$value2'");

}
exit();

请帮忙!!!我真的很挣扎。 我已经尝试了几个小时,但我还没有真正得到任何地方。 我认为问题在于插入数据库。 请帮忙。

【问题讨论】:

  • $SESSION = 4 应该是$SESSION['id'] = 4;

标签: mysql arrays loops foreach implode


【解决方案1】:

您可以通过在第二个 SQL 中直接使用 array1 来避免一个循环,因为它已经是一个逗号分隔的列表。

试试这个: 编辑:测试后更新代码。

    $SESSION['ID'] = 4;

$depSQL = mysql_query("SELECT array1 FROM TABLE1 WHERE id='".$SESSION['ID']."' LIMIT 1"); 

while($row=mysql_fetch_array($depSQL)) 
{ 
    $depARRAY = $row["array1"]; 
}
$stSQL = mysql_query("SELECT id, array2 FROM TABLE2 WHERE id IN ($depARRAY)") or die("Query Error"); 
while($get=mysql_fetch_array($stSQL)) { 
    $stARRAY = $get["array2"];
         $id =  $get["id"];
    $explodedST = explode(",", $stARRAY);
    foreach ($explodedST as $key2 => $value2) {
            if ($value2 == $SESSION['ID']) {
                            unset($explodedST[$key2]);
            }
    }
    $newST = implode(",", $explodedST);
    echo $id . " " . $newST . "<BR/>" ;
    $sql = mysql_query("UPDATE TABLE2 SET array2 ='$newST' WHERE id='$id'");
}
exit();

【讨论】:

  • 嗨 Cyber​​nate,它只删除了 id-3(TABLE_TWO) 中的数字 4,并删除了 id-4(TABLE_TWO) 中的所有值。我不知道为什么会这样......你知道我做错了什么吗?
  • 在一分钟前编辑了帖子..没有意识到更新使用的是 $value2。请尝试使用新帖子...
  • 嗨 Cyber​​nate,它仍然无法正常工作。我试图在unset($explodedST[$key2]); 代码之前和之后回显密钥,但它没有。它也不会更新数据库。请帮忙:(
  • 我不能再偷懒了:)。用虚拟数据尝试了代码并进行了一次更正。测试它并且它正在工作。请尝试更新后的帖子。
  • OKA CYBERNATE - 我真的爱你.. :) --- 你统治!!!!!谢谢你十亿次!!!我已经做了好几个小时了!!!只是一个小错误哈哈。我爱你,伙计!!!
【解决方案2】:

$SESSION 是一个数组,你不能像 $SESSION = 4; 这样分配值

这样赋值

$SESSION['id'] = 4;
if ($value2 == $SESSION['id']) {

【讨论】:

  • 嗨,Shakti,以上数值只是为了向您展示我的意思。谢谢你指出这一点。它有效,但仅删除 id-3 (TABLE_TWO) 中的数字“4”并删除 id-4 (TABLE_TWO) 中的所有值。请您看看并指出正确的方向。
猜你喜欢
  • 1970-01-01
  • 2018-04-05
  • 2014-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多