Jump to content


Photo

Reading data from a file into a database


  • Please log in to reply
17 replies to this topic

#1 VTS

VTS
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationTennessee

Posted 22 June 2006 - 03:02 PM

How would I go about reading data into a database from a text tile? The format would be similar to this:

23 343.03 23
24 43.56 19


Where the first column would be an id number, the second would be a total price, and the third would be how many of that item were sold. I am pretty new to php and I am not quite sure how to go about this.

Anyone have any ideas on how to do this or where I can look to learn about it?

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 22 June 2006 - 03:22 PM

Okay to do this you'll need to use the following functions:
fopen, fread and fclose to read the contents of the file

Next you'll need to use a function called explode, to get each row out of the text file.

And finally you'll want to use a while loop to insert each row into the database.

Hope that helps. If you need help with coding it thnen first consult the fine manual over at [a href=\"http://www.php.net\" target=\"_blank\"]http://www.php.net[/a] and sue the search box to search for each function I have mentioned in order to understand how to use them.

If you're still stuck I'll provide some code.

#3 VTS

VTS
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationTennessee

Posted 22 June 2006 - 03:45 PM

Thanks alot wildteen, that helps out alot. I should be able to figure it out from here.

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 22 June 2006 - 04:06 PM

Okay no problem! I have missed out a little detail in my last post.

You will need to use explode again in your whle loop in order for you to put the the id, price and quanty into their own sepreat columns in the database.

#5 VTS

VTS
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationTennessee

Posted 22 June 2006 - 04:07 PM

Ok, thanks again!!

#6 VTS

VTS
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationTennessee

Posted 23 June 2006 - 03:36 PM

Ok, I have looked up how the functions work but I am still a little confused on how I am going to get the data stored into an array.

If the data looks like this:

23 42.00 24
29 20.00 3
34 78.00 5

Column 1 being the item id, column2 being the price, and column 3 being the quantity.


How will I get this data stored as such? Sorry, this is the first time I have ever tried to read data from a file into a database so I am still having a rough time with it.

#7 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 23 June 2006 - 03:39 PM

Have a read of the comments here:
<?php

// connect to your database here


// replace prices.txt with your file that stores the info
$file = "prices.txt";

// open the file
$handle = fopen ($file, 'r');

// get the contents of the file
$contents = fread ($handle, 9999);

// close file
fclose ($handle);

// extract each row from the file:
$row = explode ("\n", $contents);

// get total number of rows
$rows = count ($row);

// now loop through each row and put each column into the database
for($i = 0; $i <= $rows-1; $i++)
{
    // this gets each column from the row!
    list ($id, $price, $qty) = explode (" ", $row[$i]);

    // now insert each column into the database!
    $query = "INSERT INTO table_name (id, price, qty) VALUES ('$id', '$price', '$qty')";
    $result = @mysql_query ($query) or ($error[] = "row{$i} failed to insert<br /><code>{$query}</code><br />" . mysql_error() . "<br /><br />");
}

if(isset($error))
{
    foreach ($error as $k => $v)
    {
        echo $error[$k];
    }
}
else
{
    echo "Data has been inserted successfully!";
}

?>
Also change where it says table_name (id, price, qty) in the code above to the table that stores the prices in the database and id, price and qty to the correct field names

#8 VTS

VTS
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationTennessee

Posted 23 June 2006 - 03:43 PM

Wow!! That is great! Thanks alot for all the help and the quick replies. I just want to say that this is by far the best forum for php that I have ever come across.

Thanks again,
VTS

#9 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 23 June 2006 - 03:50 PM

HA! No problem. If you have any problems post them here I'll try to help.

The script should catch a few errors that may arise.

#10 VTS

VTS
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationTennessee

Posted 23 June 2006 - 08:31 PM

Well I got it all written now but for some reason the data that gets entered into the database is all zeroes except for one field. Would the spacing in the file I am reading from have anything to do with this? Some of the fields have 5 spaces between them, others have more or less. Also, there are about 5-8 spaces between the leftmost column and the left margin in the file.

I opened up the file it was reading from just to make sure it has data in it, and it does so I am not quite sure what would cause the date that gets read in to be all zeroes.

Also, is there a size limit on how big the file can be that I am trying to read from? The file is 31kb.

#11 VTS

VTS
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationTennessee

Posted 26 June 2006 - 12:25 PM

Well I found out that it won't read the data in correctly when there is more than one space between the rows in the file that I am reading.

So my next question is how can I go about fixing this without having to have to manually delete spaces.

#12 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 26 June 2006 - 04:36 PM

Sorry I didn't reply to your message the otherday.

Is the spaces at the start of each line before the first column? (the id coloumn). If it is you can use a function called trim within the for loop which will get rid of any space before and after a line. So change this:
list ($id, $price, $qty) = explode (" ", $row[$i]);
to:
list ($id, $price, $qty) = explode (" ", trim($row[$i]));


#13 VTS

VTS
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationTennessee

Posted 26 June 2006 - 04:42 PM

Yup they are before the first column and also the spaces between the columns are different (anywhere from 1-9). Thanks again for the help!! I will try that out now and see how that works.

#14 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 26 June 2006 - 04:49 PM

If there are spaces between each column then it may require a bit of regex in order to convert mulktiple spaces into one. Trim only rmoved space form the start and end of a string.

#15 VTS

VTS
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationTennessee

Posted 26 June 2006 - 04:52 PM

Thanks for not giving up on me here.....once I get this figured out, I should be good to go. By the way, what is regex?

#16 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 26 June 2006 - 05:19 PM

Okay change this:
list ($id, $price, $qty) = explode (" ", trim($row[$i]));

to the following:
// swap mulitple spaces with just one space
$row[$i] = preg_replace("/(\s+)/", " ", $row[$i]);

// this gets each column from the row!
list ($id, $price, $qty) = explode (" ", trim($row[$i]));


#17 VTS

VTS
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationTennessee

Posted 26 June 2006 - 05:25 PM

Thanks alot wildteen!!! I will get right on this after lunch. This stuff would take me forever to learn...I just looked up some stuff on regex and it seems to be something that would take awhile. Thanks again for all the help!!

#18 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 26 June 2006 - 05:35 PM

No problem! Any problems please post back. Regex is quite hard to get around but is fun to use and learn. Regex is very powerful. It is used in a lot of applications, and programming lanaguages.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users