【问题标题】:Updating values in a mysql database from a PHP array从 PHP 数组更新 mysql 数据库中的值
【发布时间】:2012-03-22 20:57:40
【问题描述】:

我正在查询数据库,将结果值插入到数组中,然后对其进行操作,然后我想使用数组中的mysql_update 更新受影响行中的 每个 值,这就是我我遇到了麻烦。

这是我的代码 - 请协助:

name   sellerid quantity
-------------------------
john       12     10
joel       23     20
brian      40     10

让我们把它作为查询结果,有人订购了 25 件商品,现在程序是拿走这些商品并将它们分配给订购的人,然后从卖家那里扣除。

$cursor="SELECT itemquantity,sellerid FROM mytable WHERE  price='$price'";                     
//it is a table containing data about people selling their commodities

$foundItems = array();

// likely to be a parameter of a function...
$totalUnitsOrdered = 25;

// maps user to amount assigned from him
$assignedQuantityPerUser = array();


while ( $row = mysql_fetch_assoc( $cursor ) ) {

  // Still order Quantity left?
  if ( 0 < $totalUnitsOrdered ) {

    if ( $row[ "itemquantity" ] <= $totalUnitsOrdered ) {
      if (!isset($assignedQuantityPerUser[$row["sellerid"]])) {
        $assignedQuantityPerUser[$row["sellerid"]] = 0;
      }

      // assign all of $row[ "itemquantity" ]
      $totalUnitsOrdered  -= 0 + $row[ "itemquantity" ];
      $assignedQuantityPerUser[ $row[ "sellerid" ] ] += 0 + $row[ "itemquantity" ];

    }  else {
      // assign all the rest: $totalUnitsOrdered
      $totalUnitsOrdered   = 0;
      $assignedQuantityPerUser[ $row[ "sellerid" ] ] += $totalUnitsOrdered;
    }
  }

  $newItem[] = $row[ "sellerid" ];
  $newItem[] = $row[ "itemquantity" ];

  // Append $newItem to the end of $foundItems 
  $foundItems[] = $newItem;
}

【问题讨论】:

  • 这个问题最近已经发布:stackoverflow.com/questions/9531487/…。我认为不够清楚,因此缺乏回应
  • @lng 但显然这是一个不同的问题,与之前提出的问题不同......我尽可能地简化它。

标签: php mysql arrays sorting multidimensional-array


【解决方案1】:

有三种情况(算法) 这是伪代码而不是确切的代码我希望你能明白

while($row= mysql_fetch_assoc($rec))
{
  if(quantity for user is  > total quantity ordered)
  {
    quantity of user -=total quantity ordered 
    update tableNamse set qty =quantity for user where userId=$row['id'];
    exit while
  }
  else if(quantity for user = total quantity ordered)
  {
     quantity of user=0;
    update tableNamse set qty =0 where userId=$row['id'];
    exit while
  }
  else
  {
     total quantity ordered - = quantity of user
    update tableNamse set qty =0 where userId=$row['id'];
    continue while loop
  }
 }

-- 更新

   if($row['itemquantity'] > $totalUnitsOrdered)
   {
     $qtyUser=$row['itemquantity']-$totalUnitsOrdered;
     mysql_query("update tableName set itemQuantity=$qtyUser
                  where userId=$row['userId']" )
      break; // exit while
   } 

--更新第三种情况

  else
{
$totalUnitsOrdered-=$row['itemquantity'];
mysql_query("update tableName set itemQuantity=0
                  where userId=$row['userId']" )

}

【讨论】:

  • 。谢谢你,我明白了。你能不能帮我提供至少一个案例的代码。谢谢。
  • 我在编写第三种情况时遇到了麻烦,请协助。非常感谢。抱歉打扰您,只是我是 php 和 mysql 的新手。谢谢
  • 谢谢,但仍然在第三种情况下(当 $totalUnitsOrdered> $row['itemquantity'] 在索引 0 处,它应该转到索引 1 处的 $row['itemquantity'] 等等直到 $ totalUnitsOrdered =0),我如何包含一个while循环来循环遍历数据库直到$totalUnitsOrdered =0并更新每一行,因为目前它没有实现这一点。非常感谢。这是我在项目中的主要障碍.请再次帮助我。谢谢 Naveen
  • 代码不工作(第三种情况),$totalUnitsOrdered 减去当前用户数量,循环继续,里面先检查if($totalUnitsOrdered &gt;0) break;
  • , if($totalUnitsOrdered >0) 还是 if($totalUnitsOrdered =0) 应该中断,我认为是第二个
猜你喜欢
  • 2014-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-15
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
相关资源
最近更新 更多