473,395 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

how to parse this,and input in mysql ?!

Hello all!
This might be simple,but im having some problems in parsing the php code, can someone help?
i have a text file with this (for example):
--------------------------------------------------------------------
blitzztriger [****] - time : 1


hello - ( 47) 52 40
30,000 (+600) 50% 20,000 (+400) 50%

hello2 - ( 48) 102 40
35,000 (+650) 100% 50,000 (+200) 90%
--------------------------------------------------------------------
the ideia is to :
"name" --->column 1(line1)the dump will be:blitzztriger
"hello"--->column 2(line1) the dump will be:hello (hello its a name)
"( 47)"--->column 3 (line1)the dump will be:47
"( 52)"--->column 4 (line1)the dump will be:52
"30,000"--->comumn 5(line1)the dump will be: 30000 (number)
"(+600)"--->column 6(line1)the dump will be : 600
"50%"------>colum 7(line1) the dump will be: 50
and so on...

"name" --->column 1(line2)the dump will be:blitzztriger
"hello2"-->column 2(line2) the dump will be:hello2 (hello2 its a name)
"( 48)"--->column 3 (line2)the dump will be:48
"(102)"--->column 4 (line2)the dump will be:102
"35,000"--->comumn 5(line2)the dump will be: 35000 (number)
"(+650)"--->column 6(line2)the dump will be : 650
"100%"----->colum 7(line2) the dump will be: 100
and so on...

then the dumped values should enter mysql DB.
i already tried millions of things but no way!! going mad... :(
thanks in advance
Jul 17 '07 #1
6 1614
hum, if its difficult, can someone show me just one example?!
Jul 17 '07 #2
I don't quite understand what it is that you're doing, but i can give a basic example of how to put stuff into a mysql database.

Once you've created the database with all of the columns that you want (ask if you want to know how to do that, there are various ways).

get your php script to connect to your database:
[PHP]$link=mysql_connect("localhost" , "username" , "password");
mysql_select_db("name_of_your_database" , $link);[/PHP]

Create a query to insert stuff into your database:
[PHP]$query="INSERT INTO name_of_your_table (name_of_column1 , name_of_column2 , etc) VALUES ('value_to_go_in_col_1' , 'value_4_col2' , 'etc')";[/PHP]
Then execute the query:
[PHP]mysql_query($query);
if(mysql_error())
{
print "Oh, shit!<br>";
print mysql_error();
}
else
print "The stuff was put into the database.<br> Woo!!";[/PHP]

So you'd need to do that for each of the lines (ie records) that you want to add to the db.

I'm sure there's a different quicker method for adding lots of records at once, but this'll get you started.
Jul 17 '07 #3
Thanks a lot , that part i already understood. :)
Now for a better comprehension of what i want to do , heres the table :

name id pieces (A) (pieces (b) working pieces (A) production (A)
blitzztriger 47 52 40 30000 600
blitzztriger 48 102 40 35000 650

and so on...

From a .txt file like this:
blitzztriger [****] - time : 1


hello - ( 47) 52 40
30,000 (+600) 50% 20,000 (+400) 50%

hello2 - ( 48) 102 40
35,000 (+650) 100% 50,000 (+200) 90%

I know that i must regex that file..and thats the problem...or should i put it in array´s and then connect it to the db?
even with the correct regex code, where should i put it to send to the db btw?, in here?
$query="INSERT INTO name_of_your_table (name_of_column1 , name_of_column2 , etc) VALUES ('value_to_go_in_col_1' , 'value_4_col2' , 'etc')";
value_to_go_in_col_1 = regex code ?
Jul 17 '07 #4
I still don't understand!! Are you trying to split your text file into all of the separate items using regular expressions, and then put all of those items into the db?
Jul 17 '07 #5
yes!! thats it!! :D
heres some regex for this part:
30,000 (+600) 50% 20,000 (+400) 50% 1,000 (+60) 50% 0 (+30) 50% 20,000

regex: ([\d,]+) \(\+([\d,]+)\) (\d+)% ([\d,]+) \(\+([\d,]+)\) (\d+)% ([\d,]+) \(\+([\d,]+)\) (\d+)% ([\d,]+) \(\+([\d,]+)\) (\d+)%
Jul 18 '07 #6
I'm not hot on regular expressions, but my approach would be something like this:

Use fopen() or something to open the text file and stick the contents of it into a variable, $text.

Then something like this:
[PHP]while(preg_match("/hello/",$text))//while there is still at least one more record to do
{
list($junk,$text) = preg_split("/hello/",$text); //cut off stuff up to (and including) "hello"
list($junk,$text) = preg_split("/(/",$text); //cut stuff off up to the bracket (
list($value1,$text) = preg_split("/)/",$text); //so $value1 now contains the number that was between the brackets ()
list($junk,$text) = preg_split("/ /",$text);//cut off the space that comes after the closing bracket
list($value2,$text) = preg_split("/ /",$text); //$value2 now contains the number that comes before the next space (52 in the first one)
//just keep going through the bits until you've got all of the values into $variables.
//then add the dogs to the database:
$query="INSERT INTO table (column1,column2,column3,etc) VALUES ('". $value1 ."','". $value2 ."','". $value3 ."','". $etc ."')";
mysql_query($query);
}[/PHP]
I hope that helps
Jul 18 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Rob | last post by:
It's been driving me crazy...why is this returning a parse error? It says the error is on the last line, I thought I was following the syntax.... <?php $link = mysql_connect("localhost");...
3
by: josh dismukes | last post by:
/// here is the code i'm getting a parse error on the last line of the code which /// is </html> any help will be much appreciated. <?php session_start ();
3
by: Marten van Urk | last post by:
I got the following error in my page Parse error: parse error, unexpected T_ELSE in line 25 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Club</title>...
0
by: Robert Mazur | last post by:
MySQL 5.0 alpha (binary install) on Solaris 9 -or- RedHat 8.0 mysql-connector-java-3.0.8-stable ----------------------- Is there something different going on with JDBC and the alpha version...
4
by: Peter Proost | last post by:
Hi group, it's been a long time since the last time I've been here but I have a question. I'm working with timespan.parse for calculating a duration, I have to add strings which are in the...
2
by: martha70 | last post by:
I’m running PHP MySQL (zen-cart) I’m looking for info on CSV files with data sets and how I can import these into MySQL. I found a number of topics involving simple CSV files and I tried a number of...
3
by: deerhide | last post by:
Hi, I was updating my website, well trying to... and I somehow messed it up. I didnt build it, I bought it so I don't know alot about programming. I receive these errors when going to...
6
by: momogi | last post by:
I' making an database application. In one form (Input data Form), I need to fill the text field with the reference number automatically. This number must be start with date of today. For the example,...
9
by: Gilles Ganault | last post by:
Hello I googled for samples, but I'd like to make sure there's no simpler way to parse tab-separated data read from a web page, before I can save them into a database: <?php $input =...
7
by: lancewassing | last post by:
I'm hoping this question is simple. I am trying to create a login script for my already in place client manage using php. I have created a text file filled with usernames and passwords delimited by...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.