Solved

Insert php call into database with loop

Posted on 2011-09-02
4
321 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

777 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