Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2014
    Location
    Washington Township, Gloucester County, New Jersey, United States
    Posts
    11
    Plugin Contributions
    0

    help question Need some help building a looping SQL query to fill an array.

    Building a custom mod that I plan on releasing later this month. Basically, I need to do the following 6 times. I know that repeating this code 6 times with different variables is wrong. I'm just not sure how to proceed. I apologize in advance for my noobness.

    $sql = "SELECT products_quantity
    FROM " . TABLE_PRODUCTS . "
    WHERE products_id='" . (int)$p_id_1 . "'";
    $p_stock = $db->Execute($sql);
    $item_1_stock = $p_stock->fields['products_quantity'];


    So let's say I start wtih this array :

    $_product_id_array = array ($p_id_1, $p_id_2, $p_id_3, $p_id_4, $p_id_5, $p_id_6);


    I need an array that would output these keys and values:

    item_stock_array(6) {
    ["product_id_1"]=>
    int($item_1_stock) // <-- Actual value from the query above, not the variable $item_1_stock
    ["product_id_2] => // and so on for all 6 values
    ...
    }

  2. #2
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    13,872
    Plugin Contributions
    96

    Default Re: Need some help building a looping SQL query to fill an array.

    Here you go:
    Code:
    $result = $db->Execute ("SELECT products_id, products_quantity FROM " . TABLE_PRODUCTS . " WHERE products_id IN (" . implode (',', $_product_id_array) . ")");
    $item_stock_array = array ();
    while (!$result->EOF) {
      $item_stock_array[$result->fields['products_id']] = $result->fields['products_quantity'];
      $result->MoveNext ();
    
    }

  3. #3
    Join Date
    Aug 2014
    Location
    Washington Township, Gloucester County, New Jersey, United States
    Posts
    11
    Plugin Contributions
    0

    Default Re: Need some help building a looping SQL query to fill an array.

    Thank you! I had already figured out how to use the IN operator in my sql query and the implode function on my $product_id_array. I appreciate you filling in the rest. I actually understand it now :)

 

 

Similar Threads

  1. Need help creating SQL query...
    By DigitalShadow in forum General Questions
    Replies: 2
    Last Post: 22 Jun 2011, 02:05 PM
  2. I'm making major progress in building my cart but I need some help with my layout
    By strugglingnovice in forum Templates, Stylesheets, Page Layout
    Replies: 5
    Last Post: 1 Jul 2010, 10:44 AM
  3. Need help with SQL query. Want to make membership that expires.
    By TecBrat in forum Managing Customers and Orders
    Replies: 1
    Last Post: 18 Jun 2010, 03:43 PM
  4. Need help w/ SQL query in phpMyAdmin
    By audradh in forum General Questions
    Replies: 17
    Last Post: 16 Nov 2009, 01:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg