Results 1 to 9 of 9
  1. #1
    Join Date
    May 2006
    Location
    Gardiner, Maine
    Posts
    2,296
    Plugin Contributions
    22

    Default php arrays for inserting into one database field

    I'm pretty sure this is more a php question than a zen cart question, but I've been wrestling with this for hours now.

    I've created an array with this:
    PHP Code:
    while(!$productsMagic->EOF ) {
        
    $productsMagicArray = array();
    $productMagicArray[] = array('products_model'=>$productsMagic->fields['products_model'],
                                                            
    );

    $productsMagic->MoveNext();

    Then I want to take the results and separate them with commas to insert into another table.

    The array is:

    Array
    (
    [0] => Array
    (
    [products_model] => 51064
    )

    [1] => Array
    (
    [products_model] => 11111
    )

    )

    I want to have 51064,11111 to insert in order to be able to send that to an API.

    I suspect it's going to be obvious once I see it but right now I'm just missing it....

    Thanks in advance for any assistance.
    The full-time Zen Cart Guru. WizTech4ZC.com

  2. #2
    Join Date
    May 2006
    Location
    Gardiner, Maine
    Posts
    2,296
    Plugin Contributions
    22

    Default Re: php arrays for inserting into one database field

    A little more information might be needed.

    I need to send order information to an API (this is not a payment module by the way).

    Once the order is created, I get the products_model numbers out of the order - and create an array as above. There will more more info to send to the api, but their requirement is to have the model numbers as one value with each product model separated by commas.

    Sorry if my terminology is a little off. I learned php from Zen Cart and don't always know what things are supposed to be called - one of the reasons for my research failure on this one.
    The full-time Zen Cart Guru. WizTech4ZC.com

  3. #3
    Join Date
    Feb 2012
    Location
    mostly harmless
    Posts
    1,809
    Plugin Contributions
    8

    Default Re: php arrays for inserting into one database field

    If you are only capturing the model numbers in the while loop...
    Code:
    $productsModelArray = array();
    while(!$productsQueryResult->EOF ) {
      $productsModelArray[] = $productsQueryResult->fields['products_model'];
    
      $productsQueryResult->MoveNext();
    }
    $csvModelList = implode(',',$productsModelArray);
    Otherwise alot depends upon the format you need to the data to be in for the 3rd party API... Depending upon their API (and any functions / methods you may need to call), you'll probably want to adjust how you are capturing the data in the while loop to avoid needing to "loop" through the data multiple times...
    Last edited by lhungil; 11 Sep 2013 at 02:00 PM. Reason: changed variable names to be a little clearer
    The glass is not half full. The glass is not half empty. The glass is simply too big!
    Where are the Zen Cart Debug Logs? Where are the HTTP 500 / Server Error Logs?
    Zen Cart related projects maintained by lhûngîl : Plugin / Module Tracker

  4. #4

    Default Re: php arrays for inserting into one database field

    Use the following code:

    Code:
    $separate_with_commas = implode(',', $productMagicArray);

  5. #5
    Join Date
    May 2006
    Location
    Gardiner, Maine
    Posts
    2,296
    Plugin Contributions
    22

    Default Re: php arrays for inserting into one database field

    Yup, and I've tried that - instead of the product model numbers it does Array,Array

    Implode the first thing I tried.

    Now I did have the $productsMagicArray = array(); inside the while statement but moving it outside made no difference.
    The full-time Zen Cart Guru. WizTech4ZC.com

  6. #6
    Join Date
    Feb 2012
    Location
    mostly harmless
    Posts
    1,809
    Plugin Contributions
    8

    Default Re: php arrays for inserting into one database field

    Quote Originally Posted by delia View Post
    Yup, and I've tried that - instead of the product model numbers it does Array,Array
    implode operates on a single dimensional array, such as in my example ;)

    Knowing the data set you need (and how it will be used / sent) would be useful... That way the data can be manipulated in the while loop to be in a complimentary format to the later use of the data...

    For example if you needed to send a csv of just model numbers... but wanted access to all fields later (and assuming model numbers are unique)...
    Code:
    $productsModelArray = array();
    while(!$productsQueryResult->EOF ) {
      $productsModelArray[$productsQueryResult->fields['products_model']] = $productsQueryResult->fields;
    
      $productsQueryResult->MoveNext();
    }
    $csvModelList = implode(',',array_keys($productsModelArray));
    Last edited by lhungil; 11 Sep 2013 at 02:40 PM.
    The glass is not half full. The glass is not half empty. The glass is simply too big!
    Where are the Zen Cart Debug Logs? Where are the HTTP 500 / Server Error Logs?
    Zen Cart related projects maintained by lhûngîl : Plugin / Module Tracker

  7. #7
    Join Date
    May 2006
    Location
    Gardiner, Maine
    Posts
    2,296
    Plugin Contributions
    22

    Default Re: php arrays for inserting into one database field

    The final result will be a array sent using JSON

    PHP Code:
    $saleArray[] =  array('EmailAddress'=>$selectSend->fields['EmailAddress'],
                                    
    'FirstName'=>$selectSend->fields['FirstName'],
                                    
    'LastName'=>$selectSend->fields['LastName'],            
                                
    'ProductIDs'=>$selectSend->fields['ProductIds']        
            ); 
    and the ProductIDs is to be a comma separated list of the model numbers in the order.

    Is there another way to pull that array to start with besides that while statement? I just need the list of model numbers from the order. (not that it matters but the query only pulls the ordered products that are in one category - not all)
    The full-time Zen Cart Guru. WizTech4ZC.com

  8. #8
    Join Date
    May 2006
    Location
    Gardiner, Maine
    Posts
    2,296
    Plugin Contributions
    22

    Default Re: php arrays for inserting into one database field

    Oh, wait, I missed one thing when I tried your last suggestion and now it works. The array looks like this now and does implode. Thank you, thank you, thank you. I never ever would have come up with that on my own!
    Array
    (
    [51064] => Array
    (
    [products_id] => 538
    [products_model] => 51064
    [master_categories_id] => 4
    )

    [11111] => Array
    (
    [products_id] => 539
    [products_model] => 11111
    [master_categories_id] => 4
    )

    )
    The full-time Zen Cart Guru. WizTech4ZC.com

  9. #9
    Join Date
    Feb 2012
    Location
    mostly harmless
    Posts
    1,809
    Plugin Contributions
    8

    Default Re: php arrays for inserting into one database field

    Quote Originally Posted by delia View Post
    Oh, wait, I missed one thing when I tried your last suggestion and now it works. ... Thank you, thank you, thank you. ...
    No problem. There are other ways to do the same thing, but glad the quick mock up was useful :)
    The glass is not half full. The glass is not half empty. The glass is simply too big!
    Where are the Zen Cart Debug Logs? Where are the HTTP 500 / Server Error Logs?
    Zen Cart related projects maintained by lhûngîl : Plugin / Module Tracker

 

 

Similar Threads

  1. v139b Database inserting into upgrade
    By Philibel in forum Upgrading to 1.5.x
    Replies: 2
    Last Post: 23 Feb 2013, 10:37 PM
  2. Inserting Referral field into invoice
    By blarney in forum Managing Customers and Orders
    Replies: 7
    Last Post: 4 Feb 2011, 09:18 AM
  3. Inserting a string into PHP
    By bhensarl in forum Templates, Stylesheets, Page Layout
    Replies: 0
    Last Post: 5 Nov 2010, 11:46 AM
  4. date problems inserting amazonpayments orders into database
    By AmazonPayments in forum Contribution-Writing Guidelines
    Replies: 8
    Last Post: 27 Aug 2010, 10:11 AM
  5. Inserting Code into tpl_header.php
    By drymetal in forum Templates, Stylesheets, Page Layout
    Replies: 37
    Last Post: 21 Jan 2010, 10:07 AM

Bookmarks

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
Zen-Cart, Internet Selling Services, Klamath Falls, OR