• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 334
  • Last Modified:

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
0
doctorbill
Asked:
doctorbill
  • 7
  • 7
  • 2
8 Solutions
 
GaryCommented:
You've attached the same page twice so I am guessing a bit here.

do {
   echo $i;

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

$file = $_GET['xmlfile'.$i];
$filepath = "xmlimport/";
...
...

}
0
 
Ray PaseurCommented:
See lines 12/13.  Some things to consider...

The GET request is appropriate for information-only queries.  But this script changes the data model.  This script should be changed to use POST.

You might also want to do some error checking and visualize error conditions.  What if the XML did not create an object?  Would you still want to update the data base?  I probably would not want that.

<?php 

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

do {
   echo $i;

$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;

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


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

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

</body>
</html>

Open in new window

0
 
doctorbillTechAuthor Commented:
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
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
GaryCommented:
If you are talking to me then it is just before

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


But my code is no different than Rays's, just written slightly different.
0
 
doctorbillTechAuthor Commented:
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
0
 
GaryCommented:
I don't see how unless the prior code is failing - have you got error messages turned on?
0
 
doctorbillTechAuthor Commented:
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
0
 
GaryCommented:
Do a
var_dump($_GET);

and post the result here.

You will need to amend the last block of code

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);

Open in new window

0
 
doctorbillTechAuthor Commented:
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" }
0
 
GaryCommented:
I think you need to paste your code as you have it, you seem to be looping when you shouldn't be.
0
 
doctorbillTechAuthor Commented:
<?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

0
 
GaryCommented:
<?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

0
 
doctorbillTechAuthor Commented:
Perfect - thanks very much
0
 
GaryCommented:
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)
0
 
Ray PaseurCommented:
@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.
0
 
doctorbillTechAuthor Commented:
solution
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 7
  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now