tags:

views:

34

answers:

2

I have a html file that has invoice details

I would like to know is there a way that I can retrieve only the invoice numbers and store it separately in my sql database using php?

<p>Invoice ID: 0201</p>
<p>MID : Q987</p>
<p>Desciption: Solid Concrete Blocks</p>
<p>Qty: 7478 Blocks </p>
<p>&nbsp;</p>
<p>Invoice ID: 0324</p>
<p>MID : Q443</p>
<p>Desciption: Window Slides with Chrome </p>
<p>Qty: 33 Units </p>
+1  A: 

You can try something like this to extract all the invoice numbers:

<?php

$input = "<body> 
<p>Invoice ID: 0201</p> 
<p>MID : Q987</p> 
<p>Desciption: Solid Concrete Blocks</p> 
<p>Qty: 7478 Blocks </p> 

<p> </p> 
<p>Invoice ID: 0324</p> 
<p>MID : Q443</p> 
<p>Desciption: Window Slides with Chrome </p> 
<p>Qty: 33 Units </p> 
</body>";

$invoice_ids = array();
if(preg_match_all('{<p>Invoice ID:\s*(\d+)</p>}',$input,$matches)) {
    $invoice_ids = $matches[1];
}

var_dump($invoice_ids);

?>

Output:

array(2) {
  [0]=>
  string(4) "0201"
  [1]=>
  string(4) "0324"
}

Once you have extracted all the invoice numbers you can insert them in a database table using something like:

<?php
$con = mysql_connect("localhost","USRNAME","PASSWD");
if (!$con) {
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("DB_NAME", $con);

foreach($invoice_ids as $id) {

    $query = "INSERT INTO YOUR_TABLE_NAME(invoice_num) VALUES $id";

    if(!mysql_query($query,$con)) {
        die('Error: ' . mysql_error());
    }

}
mysql_close($con)
?>
codaddict
Thanks.. its was quite helpful
LiveEn
A: 

There are a few steps here:

1: create a database schema that matches your data. A schema is the layout of a database table.

2: parse the html doc and get your data using the method suggested by codaddict

3: loop through your data one record at a time and use INSERT to put it into your table.

Without any more specific info about this there's not much more we can tell you.

Travis