Web Hosting Talk







View Full Version : Create quries from file.ini


latheesan
09-19-2005, 04:48 PM
I was wondering, if this is possible

read a file called "DTunnel.ini" in the folder called /files and then attach every lines of text to a string, so that at the end of reading the file, i could insert the values on the strings to the db automatically

for e.g.

this is the content of the file "DTunnel.ini"

name=DTunnel
description=Maneuver your ship through the tunnel without crashing into the sides. The longer you last, the tunnel becomes narrower and wilder and the speed of your ship increases.
author=DBarbarian
width=400
height=250
bgcolor=FFFFFF

so read the file and assign each lines to strings like this:

$name = "DTunnel";
$description = "Maneuver your ship through the tunnel without crashing into the sides. The longer you last, the tunnel becomes narrower and wilder and the speed of your ship increases.";
$authour = "DBarbarian";
$width = "400";
$height=250
$bgcolour = "FFFFFF";

once the lines of text assigned to the strings like i showed above, i cud run this query

include ("admin/db.php");

$query = "INSERT INTO games VALUES ('$name', '$'description, '$authour ', '$height','$bgcolour')";

echo "Game Installed In The DataBase";

?>

What do you guys think, is this possible?

mfonda
09-20-2005, 12:29 AM
sure, http://us2.php.net/parse_ini_file

latheesan
09-20-2005, 02:28 AM
Originally posted by mfonda
sure, http://us2.php.net/parse_ini_file

Thanks alot mfonda, ur suggestion was something i was looking for

from that manual, i was able to get this code

<?php

$ini_array = parse_ini_file("sample.ini");
print_r($ini_array);

>?

Which Creates this as a array

Array ( [one] => 1 [five] => 5 [animal] => BIRD [path] => /usr/local/bin => [url]http://www.example.com/~username )

Now, how exactly do i work with arrays? it looks like how a cookie would have been stored, so is this how you assign it to a string?

$number = $_ARRAY['one'];

???

sea otter
09-20-2005, 03:52 AM
Actually, what you would write is:

$number = $ini_array['one'];

in your above example, $number would now == 1

It indeed looks similar to the way cookies are stored, because the cookie superglobal is an array!

latheesan
09-20-2005, 03:00 PM
Thanks alot sea otter,

your help is very much appreciated. Exactly what i was looking for.

One last question...

What if i had several file.ini in my folder and i wish to read one by one of those ini files automatically?

So i can do a bulk install of games?

sea otter
09-20-2005, 03:33 PM
We'll do this in two steps. The core foundation would be this:


<?

foreach (glob("*.ini") as $filename) {
// do something with each ini file name
}

?>


The above code loops over all the files in a directory that end with ".ini", and returns each of their names.

The question now is what to do with each file name. Obviously you want to open each file, read in the data as per all the above posts, and save it. What you'll end up with is an array of arrays. Here's the very simple code:


<?
$all_ini_files = array();

foreach (glob("*.ini") as $filename) {
$all_ini_files[$filename] = parse_ini_file($filename);
}

// now you have an array which contains the names of all your ini files, like
// game1.ini, game2.ini, etc.

// get the value for "animal" from the ini file "game1.ini"
$value = $all_ini_files['game1.ini']['animal'];

// get the value for "num_players" from "game2.ini"
$value = $all_ini_files['game2.ini']['num_players'];

// see if the value "time_limit" has been set in "game3.ini", and if not, set it to a default

if (!isset($all_ini_files['game3.ini']['time_limit']))
$all_ini_files['game3.ini']['time_limit'] = 10; // 10 minute default

// now we can access that variable like all the others:
$time_limit = $all_ini_files['game3.ini']['time_limit'];

?>


Hope this helps; let me know if you have any other questions.

latheesan
09-22-2005, 11:39 AM
Hey thanks again sea otter, sorry for the long reply, i've been experimenting with your script and finally got it to work like i want it to

once again, great suggestion, thanks alot ;)

sea otter
09-22-2005, 04:32 PM
no prob. Glad it all worked out!