Friday, February 2, 2018

How to check every row of a table to continue my loop? - development

How to check every row of a table to continue my loop?

I’m stuck with this problem because my loop stops at only the first row of the table. Every customer has a set budget for purchasing products which resets every week to purchase items if ever the qty is not equal to 0. The admin is the one who can process the order for the customer.
Database tables

customer  
id | name | budget    

customer_product   
product_id | cus_id | qty | status  

products  
id | name | price

Controller

public function processOrder($id)  
{  
 $customers = Customer::find($id);
  foreach ($customers->products as $product) 
  {
    $status = $product->pivot->status;
    $budget = $product-budget;
    $price = $product->price;
    $qty = $product->pivot->qty;

    if ($status == 1) {

          $i = $qty;

          for ($i; $i > 0; $i--) { 
            if ($budget < 1) {
              break;
            } else {
              $budget-=$price;
            }

          }
  };
  echo "Status 0"; break;
 }
}

Routes

Route::get('processOrder/{id}', ['as' => 'processOrder',
        'uses' => 'AdminController@processOrder']);

I can process the first row of the table and when it reaches 0 qty with my for loop, it stops there and doesn’t continue to process the next row even though his budget is still enough to purchase products from the next row. Can someone please help me with this problem?



from Laravel Questions and Answers https://laravelquestions.com/php/how-to-check-every-row-of-a-table-to-continue-my-loop/
via Lzo Media

No comments:

Post a Comment