Solved

php pdo loops

Posted on 2013-11-01
8
626 Views
Last Modified: 2013-11-01
Attached file:

This objct is defined in the xml file being imported:
$orderdesc2 = (string)$obj->body->ORDER->ORDERHEAD->FUNCDESC;

I have just noted that the data  in the xml file is repeated but the insert command I have only inserts once. Please can someone show me how to insert a loop for this variable
such that it inserts the total number of records for that variable from the xml file into the database

Insert statement:

$stmt = $dbh->prepare("INSERT INTO retailers (ret_name, ret_add, ret_add2) VALUES (:name, :orderdesc, :orderdesc2)");


$stmt->bindParam('name', $name);
$stmt->bindParam('orderdesc', $orderdesc);
$stmt->bindParam('orderdesc2', $orderdesc2);

//execute the query
$stmt->execute();
xmlimport-form.php
0
Comment
Question by:doctorbill
  • 4
  • 2
8 Comments
 
LVL 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 167 total points
Comment Utility
can we see the xml file? Or at least a part of it that shows the structure and how items repeat?
0
 

Author Comment

by:doctorbill
Comment Utility
Sorry - my mistake. I gace the incorrect repeat region:
The repeat region is: body-ORDER-ORDEDRLINE-PRODUCT
If I could just see the mechanism of how to enter the repeat

xml attached
Purchase-Order-FI2-4098098.xml
0
 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 333 total points
Comment Utility
You would need to loop through the node in your XML and run the query->execute() in there:

$dbh = new PDO("mysql:host=localhost;dbname=1callorders", "user", "pass");
		
$xml = file_get_contents('data.xml');
$obj = simplexml_load_string($xml);

//prepare the query
$stmt = $dbh->prepare("INSERT INTO retailers (ret_name, ret_add, ret_add2) VALUES (:name, :orderdesc, :orderdesc2)");

//bind the named parameter to PHP variables
$stmt->bindParam('name', $name);
$stmt->bindParam('orderdesc', $orderdesc);
$stmt->bindParam('orderdesc2', $orderdesc2);

$name = (string)$obj->header->manifest->document->name;
$orderdesc = (string)$obj->body->ORDER->ORDERHEAD->ORDERDESC;

//loop through the orderline to get each product description
foreach ($obj->body->ORDER->ORDERLINE as $orderline):
	$orderdesc2 = $orderline->PRODUCT->DESCRIPTION;	

	//execute the query
	$stmt->execute();	
endforeach;

Open in new window

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:doctorbill
Comment Utility
How does this fit into the xmlimport-form.php file?

ie what does the xmlimport-form.php look like with this code inserted ?
0
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 333 total points
Comment Utility
The only difference is the loop. Other than that your code stays the same (although for some reason you seem to be echoing out some information before the opening DOCTYPE!). Here's the PHP part in full:

<?php 
$dbh = new PDO("mysql:host=localhost;dbname=retailers", "user", "pass");
		
$file = $_POST['xmlfile'];
$filepath = "xmlimport/";

$xml = file_get_contents($filepath . $file);
$obj = simplexml_load_string($xml);
		
//prepare the query
$stmt = $dbh->prepare("INSERT INTO q_28282860 (ret_name, ret_add, ret_add2) VALUES (:name, :orderdesc, :orderdesc2)");

//bind the named parameter to a PHP variable (called $name and $orderdesc etc )
$stmt->bindParam('name', $name);
$stmt->bindParam('orderdesc', $orderdesc);
$stmt->bindParam('orderdesc2', $orderdesc2);

$name = (string)$obj->header->manifest->document->name;
$orderdesc = (string)$obj->body->ORDER->ORDERHEAD->ORDERDESC;

//loop through the orderline to get each product description
foreach ($obj->body->ORDER->ORDERLINE as $orderline):
	$orderdesc2 = $orderline->PRODUCT->DESCRIPTION;	

	//execute the query
	$stmt->execute();	
endforeach;

//run the second query
$stmt = $dbh->prepare("INSERT INTO ret_orders (ord_linetype) VALUES (:name)");
$stmt->bindParam('name', $name);
$stmt->execute();
?>

Open in new window

0
 

Author Comment

by:doctorbill
Comment Utility
Thanks very much - working
It amazes me how you guys do this in seconds when it takes me ages to see the wood for the trees !!
0
 

Author Closing Comment

by:doctorbill
Comment Utility
solution
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

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

11 Experts available now in Live!

Get 1:1 Help Now