PDA

View Full Version : Is there any auto add PHP script?



sartajc
10-09-2006, 09:32 PM
NOTE: sorry im double posting in the javascript and php forum. Im not sure which code language will be able to do what i need.

I have a HUGE SQL file with 1000s of queries, but i need to convert that to xml. It is the Quran (huge holy book) and i need to covert it to xml for a module i am making for Joomla.

Here is an example of one line

INSERT INTO quran VALUES (1, 'The Opening', 1, 1, '1 . In the name of Allah , the Beneficent , the Merciful');
There are thousands of lines like this one.


Bascially <query> before every INSERT INTO quran it detects? And insert a </query> after every )' it detects?

If thats not possible, is there any code that will insert a </query><query> after every enter it detects?

Or is this kind of job not possible with Javascript? Maybe PHP?

Twey
10-09-2006, 09:42 PM
Try preg_replace() (http://www.php.net/preg-replace).

AbelaJohnB
10-10-2006, 12:46 AM
sartajc,

Can you please post a real example of BOTH what the present format is, and the format of what you want.

I've done this dozens of time for different versions of the Bible... I just need to know exactly what format you want your .xml structor.

sartajc
10-10-2006, 08:09 PM
O ok. Heres an example of the line:

Now it is:


INSERT INTO quran VALUES (1, 'The Opening', 1, 1, '1 . In the name of Allah , the Beneficent , the Merciful');
INSERT INTO quran VALUES (2, 'The Opening', 1, 2, '2 . Praise be to Allah , Lord of the Worlds ,');
INSERT INTO quran VALUES (3, 'The Opening', 1, 3, '3 . The Beneficent , the Merciful .');

I need this:


<query>INSERT INTO quran VALUES (1, 'The Opening', 1, 1, '1 . In the name of Allah , the Beneficent , the Merciful');</query>
<query>INSERT INTO quran VALUES (2, 'The Opening', 1, 2, '2 . Praise be to Allah , Lord of the Worlds ,');</query>
<query>INSERT INTO quran VALUES (3, 'The Opening', 1, 3, '3 . The Beneficent , the Merciful .');</query>

There is obviously more than 3 lines. It goes on for 1000s of lines.

DimX
10-10-2006, 08:49 PM
You can use some text editor which supports RegEx search and replace operations. (PSPad ...)

AbelaJohnB
10-10-2006, 11:20 PM
Uhh, how is that example you posted "converting it to xml"?

Proper XML for this would be (something like):



<item>
<title>The Opening</title>
<chapter>1</chapter>
<verse>1</verse>
<description>
<![CDATA[1 . In the name of Allah , the Beneficent , the Merciful]]>
</description>
</item>
<item>
<title>The Opening</title>
<chapter>1</chapter>
<verse>2</verse>
<description>
<![CDATA[2 . Praise be to Allah , Lord of the Worlds ,]]>
</description>
</item>
<item>
<title>The Opening</title>
<chapter>1</chapter>
<verse>3</verse>
<description>
<![CDATA[3 . The Beneficent , the Merciful .]]>
</description>
</item>


That's what you really asked for...

But what it sounds like what you've meant is to simply (a/pre)pend something to every line of your .sql file.

In which case, just do what DimX suggested... replace on "INSERT INTO" and ");" ... and you should be done in about 20 seconds.

djr33
10-11-2006, 01:22 AM
If you want the above, just have php output all the rows, and getting the data from each row.

so....
echo "stuff".$row['data1']."morestuff".$row......;



http://php-mysql-tutorial.com

sartajc
10-11-2006, 04:44 AM
If you want the above, just have php output all the rows, and getting the data from each row.

so....
echo "stuff".$row['data1']."morestuff".$row......;



http://php-mysql-tutorial.com


ehh...i have no diea what you mean. im not PHP fluent. can you be more specific?

sartajc
10-11-2006, 04:46 AM
Uhh, how is that example you posted "converting it to xml"?

Proper XML for this would be (something like):



<item>
<title>The Opening</title>
<chapter>1</chapter>
<verse>1</verse>
<description>
<![CDATA[1 . In the name of Allah , the Beneficent , the Merciful]]>
</description>
</item>
<item>
<title>The Opening</title>
<chapter>1</chapter>
<verse>2</verse>
<description>
<![CDATA[2 . Praise be to Allah , Lord of the Worlds ,]]>
</description>
</item>
<item>
<title>The Opening</title>
<chapter>1</chapter>
<verse>3</verse>
<description>
<![CDATA[3 . The Beneficent , the Merciful .]]>
</description>
</item>


That's what you really asked for...

But what it sounds like what you've meant is to simply (a/pre)pend something to every line of your .sql file.

In which case, just do what DimX suggested... replace on "INSERT INTO" and ");" ... and you should be done in about 20 seconds.


No, thats not what i want at all. Im making an XML file for an extension im making for Joomla, and when someone uploads the xml file onto their joomla it gets executed. And in an XML install file, <query></query> will run a MySQL query. thats why i need <query></query> surrounding all the 1000s of queries i need to run.

sartajc
10-11-2006, 04:48 AM
Try preg_replace() (http://www.php.net/preg-replace).

Help... i have no idea what that means.

djr33
10-11-2006, 04:59 AM
EDIT:
Just saw your post.
Ok... you're making this WAY too complex.
You don't need php. Forget about it.
You just need notepad/word and do a find/replace to put that in there. couldn't be easier. Really.
FIND: INSERT INTO
REPLACE WITH: <query>INSERT INTO
FIND: ');
REPLACE WITH: ');</query>

You're done. Be happy.


//EDIT



mysql results are retrieved using mysql queries.... a query is a type of request/database command that results in... well... results.

(for this example, I'll assume we're using a database for a forum such as this one)


mysql_query("SELECT `name` FROM `members` WHERE `id`='1'")

That would result in the membername of the first person to get an ID, likely the admin, etc.

SELECT means... select/grab/get/find/retrieve. `name` means it will select the `name`... not the ID, not the IP address, etc.
FROM specifies the table in the database... members? topics? private messages? etc...
WHERE is a condition and optional. You can specify that if you want to find a particular item, or sets of items, like WHERE `group` = "admin".

Here's what to do with the result:

$result = mysql_query('somethinghere');
while ($row = mysql_fetch_assoc($result)) {
echo $row['name']."\n";
}This means...
The first part sets $result to the value of what is returned from the query. It's some strange form of data that can't be used directly.
We use the function mysql_fetch_assoc, just a function, don't be alarmed, to make that into an array.
$row = mysql_fetch_assoc($result) sets the value of ONE row of the result from the query to the $row array.
If you had more than one result from the mysql query, you would be able to access all of those values in it.
$row['fieldname'] is the general syntax for a value in the array at tha point, in this case, name... $row['name']
So... to recap, so you're not lost: 1. ask the database for results that match certain requirements. 2. Take those results and place them into a variable.
The next step is to use a while loop. This will keep looping/repeating until the rows are over. It's not "while this is equal to this" but literally "while this is set to this".
Since there are a limited number of rows returned (even if that is a thousand, like you'd be dealing with), then it would fail after the rows are used up. therefore, it loops through all of the rows returned from the database.

echo is a general command meaning print/output/write to the outputted html.
echo $row['name']."\n"; means...
1. print the value of the name from the database, then 2. add a line break (
\n is a special line break character in php, long story).

and that's it.

You may be a bit confused, but it'll make sense.

That's why I gave you a link.

Click. Read. Try. It'll work out. that's how I learned. Very easy to follow step by step guide. Heck, it's almost boring in parts it's so easy ;)



Also, a general question... you may not even need to do this. do you have a text document containing all this information? It looks to me like you have a text backup, which is basically a set of mysql commands to recreate the database, with INSERT INTO, and UPDATE and such.
If that's the case, then FORGET everything about php and all that.
You just need to use php to interpret the data, or even use another language. Find/replace in notepad/word/etc. might be able to help.

I'm still not quite sure what you're doing.

sartajc
10-11-2006, 05:28 AM
EDIT:
Just saw your post.
Ok... you're making this WAY too complex.
You don't need php. Forget about it.
You just need notepad/word and do a find/replace to put that in there. couldn't be easier. Really.
FIND: INSERT INTO
REPLACE WITH: <query>INSERT INTO
FIND: ');
REPLACE WITH: ');</query>

You're done. Be happy.


:eek: WOW....it was that easy :D . oops. I guess i forgot to learn the father of all computers, the NOTEPAD :)


I'm still not quite sure what you're doing.

http://famousmuslims.muslimonline.org. if you see the Quran there, i made that module for Joomla. I want other people with joomla sites to be able to install and thus need an XML file for those people who dont understand MySQL commands


Thanks alot djr33. This forum has helped me out in so many problem i have had in my coding life, and unlike others, i actually get help that works. Thanks alot everyone :cool: .

djr33
10-11-2006, 05:40 AM
Sure. Just sorry I missed that.
You obviously had a text backup of a database, not an active database you could access with PHP. Should've noticed sooner. :p

Glad it works :)


EDIT: Additionally, I'm not sure how all this works, but I think you can actually have multi-item queries. You might not need a tag between each item... just one at the start and end... and a semi-colon between? Or something. I think there might be a way. But if that works, great.

sartajc
10-11-2006, 05:57 AM
Heres how it works.
<install>
<queries>
<query> query here </query>
<query> query here </query>
</queries>
</install>

If there is a way to multi do it, i wouldnt do it cause it would be too much on the server. im talking about 13000 queries here. no joke. having them all done at once can freeze the upload.

djr33
10-11-2006, 06:08 AM
sure, that's fine.
I just mean that I think mysql might support many queries as one... with a semi colon or something, but i'm not sure. so you'd only need the one <query></query> pair. But I don't reallly know.