Solved

Insert php call into database with loop

Posted on 2011-09-02
4
329 Views
Last Modified: 2012-06-27
I am making php calls to gather product information from a vendor's API.  The data being returned looks like the example below.  They are dumped into the variable $result

object(stdClass)#2 (2) { ["success"]=> bool(true) ["data"]=> array(2400) { [0]=> object(stdClass)#3 (6) { ["groupName"]=> string(14) "FIRESTIK FLAGS" ["groupIndex"]=> string(5) "47654" ["tabName"]=> string(11) "AIR FILTERS" ["tabIndex"]=> string(6) "AT0100" ["catName"]=> string(15) "ATV ACCESSORIES" ["catPrefix"]=> string(3) "ATV" }

There are many additional items in this list.  I need to insert each row into my database

INSERT INTO groups (groupname,groupindex,tabname,tabindex,catname,catprefix)
VALUES(not sure what goes here?)

I believe I need to use a foreach or a while loop to insert each individual row as there are hundreds but I am not sure what the php code should look like?
0
Comment
Question by:cmaddrix
  • 3
4 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 36474841
To give a tested example, we would need to see the object or XML string returned from the API.

It looks like $result->data is an array of objects so you would iterate over that array with something like this, copying the fields into local variables

foreach ($result->data as $element)
{
    $groupname = $element->groupName;
    $gouupindex = $element->groupIndex;
    // etc, etc...

And once the copies are complete, you can use the local variables in your INSERT query.

There may be a tidier way to do this, using some kind of inner iterator on the $element.  Maybe something like the code snippet.  HTH, ~Ray
foreach ($result->data as $element)
{
        // PROCESS THE DATA INTO PARTS OF AN INSERT QUERY
        $cols = NULL;
        $dats = NULL;
        foreach ($element as $key => $val)
        {
            $val = mysql_real_escape_string($val);
            $cols .= "`" . $key . "`,";
            $dats .= "'" . $val . "',";
        }

        // COMPLETE THE QUERY
        $cols = rtrim($cols, ',');
        $dats = rtrim($dats, ',');
        $str  = "INSERT INTO myTable ( $cols ) VALUES ( $dats )";

        // RUN THE QUERY HERE
        var_dump($str);
}

Open in new window

0
 

Author Comment

by:cmaddrix
ID: 36488474
Ray I tried your suggestion but still not inserting into the database.  I attached the code you can see the output at http://www.idiotcycles.com/wps/apitest.php

Thanks
$groupResult = $wps->api(array(
					"method"=>"catalogs.getGroup",	//(required) The name of the method to call
					"responseType"=>"PHP_ARRAY", 	//(optional) could be PHP_ARRAY (default), HTML_OPTIONS, XML
					"catalog"=>"ATV",   			//(optional) catalogs to bring back.  Could be ALL (default), or comma delimited specific like DT,WC etc.
					//"tab"=>"AT0170",   			//(optional) tabs to bring back.  Could be ALL (default), or comma delimited specific like AT101, AT102.
					//"group"=>"25375",   			//(optional) groups to bring back.  Could be ALL (default), or comma delimited specific like 55, 556.
					//"selectedGroup"=>"25375",   	//(optional) The index of the group to be selected.
					//"catalogSort"=>"ALPHA_ASC",   //(optional) could be ALPHA_ASC, ALPHA_DESC, DEFAULT (if not provided DEFAULT)
					//"tabSort"=>"ALPHA_ASC",   	//(optional) could be ALPHA_ASC, ALPHA_DESC, DEFAULT (if not provided DEFAULT)
					//"groupSort"=>"ALPHA_ASC",   	//(optional) could be ALPHA_ASC, ALPHA_DESC, DEFAULT (if not provided DEFAULT)
				));
				
var_dump($groupResult);

require("config.php");

foreach ($groupResult->data as $element)
{
        // PROCESS THE DATA INTO PARTS OF AN INSERT QUERY
        $cols = NULL;
        $dats = NULL;
        foreach ($element as $key => $val)
        {
            $val = mysql_real_escape_string($val);
            $cols .= "`" . $key . "`,";
            $dats .= "'" . $val . "',";
        }

        // COMPLETE THE QUERY
        $cols = rtrim($cols, ',');
        $dats = rtrim($dats, ',');
        $str  = mysql_query("INSERT INTO wps_groups ( $cols ) VALUES ( $dats )",$dbC);

        // RUN THE QUERY HERE
        var_dump($str);
		
}
mysql_close($dbC);

Open in new window

0
 

Author Comment

by:cmaddrix
ID: 36489794
I've requested that this question be deleted for the following reason:

stupid
0
 

Author Comment

by:cmaddrix
ID: 36489795
I would like to accept Ray_Paseur post as solution and award points before deleting
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question