Solved

Insert php call into database with loop

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

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
I've requested that this question be deleted for the following reason:

stupid
0
 

Author Comment

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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now