Link to home
Create AccountLog in
Avatar of doctorbill
doctorbillFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Php empty file

The attached file is working perfectly well except for one thing:

I need to get the file to ignore the GTE url if the value is empty

The GET url has 5 possible values sent from a form:
xmlfile1
xmlfile2
xmlfile3
xmlfile4
xmlfile5

If, for example, I only send xmlfile1 and xmlfile2 values from the form, I need the attached page to stop processing after the second value

I have tried using "if(!empty($_GET("xmlfile".$i));" but this does not work
xmlimport-form-loop.php
xmlimport-form-loop.php
SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of doctorbill

ASKER

Ray,
Your code on 12/13 works in terms of entering the correct data into the database but the page hangs after the url data is parsed to it and the code from lines 78 onwards does not show on the page

where exactly should the second } bracket be in your code - could you edit the page to demonstrate please
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Both your code and Ray's cause the same problem:

Code works in terms of entering the correct data into the database but the page hangs after the url data is parsed to it and the code from lines 78 onwards does not show on the page
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Sorry - your code did not paste correctly
What haappens now is that the page does not hang and the code from 78 onwards runs correctly BUT Ionly send one GET value I get 5 insertions of the same record into the database
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Sorry - your code did not paste correctly
What haappens now is that the page does not hang and the code from 78 onwards runs correctly BUT Ionly send one GET value I get 5 insertions of the same record into the database

DUMP:
1array(2) { ["xmlfile1"]=> string(30) "Purchase_Order_FI2-4098097.xml" ["xmlsubmit"]=> string(6) "Submit" } 2array(2) { ["xmlfile1"]=> string(30) "Purchase_Order_FI2-4098097.xml" ["xmlsubmit"]=> string(6) "Submit" } 3array(2) { ["xmlfile1"]=> string(30) "Purchase_Order_FI2-4098097.xml" ["xmlsubmit"]=> string(6) "Submit" } 4array(2) { ["xmlfile1"]=> string(30) "Purchase_Order_FI2-4098097.xml" ["xmlsubmit"]=> string(6) "Submit" } 5array(2) { ["xmlfile1"]=> string(30) "Purchase_Order_FI2-4098097.xml" ["xmlsubmit"]=> string(6) "Submit" }
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
<?php 

$dbh = new PDO("mysql:host=localhost;dbname=1callorders", "root", "xxxx");
		
$i = 1;

do {
   echo $i;

if($_GET['xmlfile'.$i]!=""){ // check if the value is empty

$file = $_GET['xmlfile'.$i];

// SKIP THE EMPTY FILE POSITIONS
//if (empty($file)) continue;

$filepath = "xmlimport/";
}

$xml = file_get_contents($filepath . $file);
$obj = simplexml_load_string($xml);

//echo out statements from xml
//$name = (string)$obj->header->manifest->document->name;
//$orderdesc = (string)$obj->body->ORDER->ORDERHEAD->ORDERDESC;
//$orderdesc2 = (string)$obj->body->ORDER->ORDERHEAD->FUNCDESC;

$space = "&nbsp;";
//echo "Name: " . $name . $space;
//echo "Desc: " . $orderdesc . $space;
//echo "Desc2: " . $orderdesc2 . $space;
//end statements
		
//prepare the query
$stmt = $dbh->prepare("INSERT INTO ret_orders (ret_id, ord_prodtpe, ord_prodcdedesc, ord_proddesc) VALUES (:ret_id, :name, :orderdesc, :orderdesc2)");

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

$ret_id = "DRM";
$name = (string)$obj->header->manifest->document->name;
$orderdesc = (string)$obj->body->ORDER->ORDERHEAD->ORDERDESC;
//$orderdesc2 = (string)$obj->body->ORDER->ORDERHEAD->FUNCDESC;

//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
//$stmt2 = $dbh->prepare("INSERT INTO ret_orders (ord_linetype) VALUES (:name)");
//$stmt2->bindParam('name', $name);
///$stmt2->execute();

$i++;
} while ($i <= 5);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body><br /><br />


<?php
$i = 1;
$break = "<br />";
$space = "&nbsp;";

do {
   //echo $i;
if($_GET['xmlfile'.$i]!=""){ // check if the value is empty
$filenames = $_GET['xmlfile'.$i];
}

echo "File Imported:".$space.$filenames.$break;

$i++;
} while ($i <= 5);

?>

</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Accepting one of your own comments delays the closing process, especially when your comment is not part of the solution.
In this case it would be just easier to split the points between Ray's and my first comments (as you see fit) as they were the solution and the other comments were just clarifications. (Rather than 70 points for every comment ;o)
@doctorbill: Please, please get some professional programming help, or take some time to step away from this work and learn how to use PHP.  It's not rocket science, but it is extremely detail oriented work, and it takes time and practice to get what you want.  

As an example of what's important but overlooked, you can look no further than the recommendation to change $_GET to $_POST.  I'm concerned that you did not recognize the importance of this recommendation.  We can help with many things, and if you ask more questions along the lines of "what are good learning resources," you will get better results.
solution