Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Php empty file

Posted on 2014-01-05
16
Medium Priority
?
309 Views
Last Modified: 2014-01-10
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
Comment
Question by:doctorbill
  • 7
  • 7
  • 2
16 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 1712 total points
ID: 39757639
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
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 288 total points
ID: 39757666
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
 

Author Comment

by:doctorbill
ID: 39758030
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 58

Assisted Solution

by:Gary
Gary earned 1712 total points
ID: 39758041
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
 

Author Comment

by:doctorbill
ID: 39758059
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
 
LVL 58

Assisted Solution

by:Gary
Gary earned 1712 total points
ID: 39758085
I don't see how unless the prior code is failing - have you got error messages turned on?
0
 

Author Comment

by:doctorbill
ID: 39758100
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
 
LVL 58

Assisted Solution

by:Gary
Gary earned 1712 total points
ID: 39758107
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
 

Author Comment

by:doctorbill
ID: 39758118
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
 
LVL 58

Assisted Solution

by:Gary
Gary earned 1712 total points
ID: 39758134
I think you need to paste your code as you have it, you seem to be looping when you shouldn't be.
0
 

Author Comment

by:doctorbill
ID: 39758146
<?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
 
LVL 58

Accepted Solution

by:
Gary earned 1712 total points
ID: 39758151
<?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
 

Assisted Solution

by:doctorbill
doctorbill earned 0 total points
ID: 39758175
Perfect - thanks very much
0
 
LVL 58

Expert Comment

by:Gary
ID: 39758200
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39758222
@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
 

Author Closing Comment

by:doctorbill
ID: 39770581
solution
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

782 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