Solved

Insert php call into database with loop

Posted on 2011-09-02
4
349 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 110

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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

630 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