Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7597347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:08:07+00:00 2026-05-30T22:08:07+00:00

I am querying the database, inserting the resultant values in an array then manipulating

  • 0

I am querying the database, inserting the resultant values in an array then manipulating them and thereafter I want to update each of values in the affected rows using mysql_update from the array and that’s where I am having trouble.

This is my code – kindly assist:

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

Let’s take that as the query result and someone orders 25 items now the program is to take the items and assign them to one who ordered then deduct from the sellers.

$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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:08:08+00:00Added an answer on May 30, 2026 at 10:08 pm

    There are three cases (Algorithm )
    this is pseudo code not exact code i hope you get the idea

    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
      }
     }
    

    — Update

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

    –Update for third case

      else
    {
    $totalUnitsOrdered-=$row['itemquantity'];
    mysql_query("update tableName set itemQuantity=0
                      where userId=$row['userId']" )
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SQL server database that I am querying and I only want
Normally, when querying a database with SELECT, its common to want to find the
I am querying the database to return a simple array. IE: SELECT * FROM
I have a pulldown menu created from PHP querying a database and then using
I make an application for querying from and inserting data to the database using
Im querying a database using JPQL but I cannot retrieve the 'Report' table's rows
I am querying a database for results and trying to convert them into JSON
How Do You Implement Specification Pattern for querying database using NHibernate?(without LINQ to NHibernate).I
i'm querying a database like this: SELECT * from Log WHERE cookieId IN (select
I have just found RowSets for database querying with JDBC. They are stateless and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.