Solved

Insert php call into database with loop

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 …

739 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