PDA

Click to See Complete Forum and Search --> : PHP parse_ini_file


PHP Freke
Dec 5th, 2009, 09:35 PM
I have a .ini file that is like this:
(file.ini)
; INI FILE

[1]
name = "asdf1"

[2]
name = "asdf2"

Here is what I have in PHP right now...
(otherfile.php)
<?php

$stuff = parse_ini_file("file.ini", true);

?>

Continuing on from that, how can I make another .php file so that if you entered either 2 or asdf2 it would do the same thing? (It needs to use the ini file too)

I do not know exactly how to explain all this, due to me not having experience with ini and php stuff, so tell me if I need to refrase that.

kows
Dec 5th, 2009, 11:48 PM
I'm not sure what you mean. you want to create an ini file based on what the user enters? or you want to parse the file depending on what they enter? what do you mean when you say, "so that if you entered either 2 or asdf2 it would do the same thing"? what are you entering this data into? what is "the same thing"?

if you're having a hard time explaining this, then the easiest way might be to give a real world example so that what you're saying isn't so vague.

PHP Freke
Dec 6th, 2009, 10:39 AM
Ok. You have the file.ini file like I said, and the otherfile.php file.

Now a have made a new file called someotherfile.php that has a require in it to require otherfile.php.

Inside someotherfile.php I have this:

<?php

require("otherfile.php");

$l = new someClass

$Variable = trim(fgets(STDIN));

$l->connect($Variable);


?>

Here is a new otherfile.php:

<?php

$stuff = parse_ini_file("file.ini", true);

class someClass {

function connect($Var){
fopen("http://www.example.com/someFolder/$Var", "r");
}
}
?>

I would like otherfile.php to use file.ini to do this:

(Example): If you enter 1 into the someotherfile.php it would connect to http://www.example.com/someFolder/$Var. I would like $Var to be the ID in th INI file. Now say the user enters asdf1 into someotherfile.php. It would use file.ini to see what ID that was from and use it to connect to the website.

kows
Dec 6th, 2009, 01:39 PM
oh, you're doing things with the command prompt. you should have said so.

anyway, this is pretty straight forward. parse_ini_file() allows you to parse sections by setting its second parameter to true (sections in an INI file are the headers with squared brackets; 1 and 2 in your case). for example, the following code:

$ini = parse_ini_file("myfile.ini", true);

produces the following array:

$ini = array (
[1] => array(
[name] => asdf1
)
[2] => array(
[name] => asdf2
)
)

so, your class could look something like:

<?php
class someClass {
const connectURL = "http://example.com/dir/";
private $ini; //cache of ini file

public function __construct($ini){
$this->ini = parse_ini_file($ini, true);
}

public function connect($section){
$path = self::connectURL . $this->ini[$section]['name'];
//produces http://example.com/dir/asdf1 if $section is 1

$file = file_get_contents($path);

//the file is now stored within $file, so you can do whatever with it.
}
}
?>

then, you could use it like:

<?php
require("someClass.class.php");

$ini = new someClass("myFile.ini");

//get the user's input
$input = trim(fgets(STDIN));

$ini->connect($input);
?>
of course, you'll have to provide your own validation. ask questions if some of it doesn't make sense to you!

by the way -- if this is for a school project, you don't happen to be going to NAIT, do you? if not, please disregard!

PHP Freke
Dec 6th, 2009, 08:23 PM
The code you posted works, but when I enter asdf1, i get an error.

PHP Notice: Undefined index: asdf1 in [LOCATION] on line 11

kows
Dec 6th, 2009, 11:24 PM
well, this is why I said you would need to add validation of your own. but, first of all.. asdf1 is a value, not the name of a section. you would need to input "1" as a value because the section's name is 1, and one of the "keys" underneath that section is "name," and name's value is asdf1.

now, you're getting an undefined index because the $this->ini array (within someClass) has the keys 1 and 2, and each of those keys are an array themselves -- each with a key named name. name's value is retrieved by calling $this->ini[section]['name'], where section is either 1 or 2. section 1 would return asdf1, and section 2 would return asdf2.

you can avoid this error by either making another method that checks whether or not a section exists (sectionExists()?), or by just checking whether $this->ini[section] is set within the connect() method before doing anything (just by calling isset() with $this->ini[section] as the parameter). sectionExists() would just do the same thing, but would be in a separate method so that you could use it in other situations or whatever.

hope that makes sense. as always, ask questions.

PHP Freke
Dec 7th, 2009, 03:33 PM
well, this is why I said you would need to add validation of your own. but, first of all.. asdf1 is a value, not the name of a section. you would need to input "1" as a value because the section's name is 1, and one of the "keys" underneath that section is "name," and name's value is asdf1.

now, you're getting an undefined index because the $this->ini array (within someClass) has the keys 1 and 2, and each of those keys are an array themselves -- each with a key named name. name's value is retrieved by calling $this->ini[section]['name'], where section is either 1 or 2. section 1 would return asdf1, and section 2 would return asdf2.

you can avoid this error by either making another method that checks whether or not a section exists (sectionExists()?), or by just checking whether $this->ini[section] is set within the connect() method before doing anything (just by calling isset() with $this->ini[section] as the parameter). sectionExists() would just do the same thing, but would be in a separate method so that you could use it in other situations or whatever.

hope that makes sense. as always, ask questions.

I don't really understand that, so I'm just going to ask something new...

I could use if(is_numeric($section)) { } to check if it is a number or not... then if it is, it will go ahead and connect. If not, it will use file.ini to make the word into the number.

That would be easier for me.

(And in case your wondering, I'm pretty good with PHP, but I have never done anything with INI stuff, so it is confusing to me right now. Thanks for the support so far.)

kows
Dec 7th, 2009, 05:04 PM
I didn't see any questions there, but if that works for you then you could go ahead and do that. even so, I'd put that logic within your connect() method -- this will ensure your script won't be cluttered with tons of validation stuff.

PHP Freke
Dec 7th, 2009, 06:26 PM
I didn't see any questions there, but if that works for you then you could go ahead and do that. even so, I'd put that logic within your connect() method -- this will ensure your script won't be cluttered with tons of validation stuff.

I meant, how can I get ID (1) if the user typed in asdf1?
(Code please?)

kows
Dec 7th, 2009, 08:50 PM
uhh. you have the ini file stored in an array already, so you would have to loop through the array and check whether or not $this->ini[index]['name'] equaled the string they entered. seems simple enough a concept for me, so I won't provide any code.

if the ini file is predictable and always has a certain format for every section, then you could use the pattern to create a regular expression. however, you will have to pursue this on your own a little before I helped; the method above would work just as well!

PHP Freke
Dec 7th, 2009, 09:28 PM
uhh. you have the ini file stored in an array already, so you would have to loop through the array and check whether or not $this->ini[index]['name'] equaled the string they entered. seems simple enough a concept for me, so I won't provide any code.

if the ini file is predictable and always has a certain format for every section, then you could use the pattern to create a regular expression. however, you will have to pursue this on your own a little before I helped; the method above would work just as well!

I have no experience with PHP and INI stuff. (PHP yes, INI no. Combined no.)

All I need is what to put in the code if the user enters asdf1 or asdf2 to make that into the ID the name is in.

kows
Dec 7th, 2009, 10:36 PM
.. you do realize that this literally has absolutely nothing to do with ini files, right..?

an ini file is just a text file. parse_ini_file() parses an ini file and places it into an array. this is all about dealing with multi-dimensional arrays. I just told you exactly what you'd need to do to get the information you wanted :/ I just didn't do it for you..

I've got some time on my hands, so I'll make a puzzle/guide for you (only being a puzzle if you aren't familiar with arrays)!

use a foreach() loop on $this->ini (keys and values = $this->ini as $key => $value). $key would store the number you're interested in (1 or 2, for example, from your previous ini file example), and $value would store an associative array. you can access the key 'name' from $value and then check it against your user input. if your validation expression returns true, you can break; out of the foreach() loop and store $key.

hope that puts you in the right direction! if not, post some code ;)

PHP Freke
Dec 9th, 2009, 10:21 AM
Ok, I have it figured out.

Now, how can I make sure that the user enters a valid name? (asdf1 OR 1 | asdf2 OR 2)