12 Replies - 1251 Views - Last Post: 07 May 2012 - 05:31 AM Rate Topic: -----

#1 OKIEWARDOYO   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 03-April 12

[HELP] How to create a file and how to read a file?

Posted 05 May 2012 - 10:20 PM

Question no1: How to create a file?

Can somebody here tell me?
i have an array like below
$myarray = array(
	'name' => 'Okie',
	'age' => '24',
	'language' => 'indonesian'
);


now, i want to create a file, for example php file that named 'okie_data.php', which contains an array above. How to do that?

For now, i have a code below, but i think, it's not an elegant code.
	
    $ret = '';
    $ret .='<?';
     $ret .="\r\n$data_of_okie" = array(";
    //
    foreach($myarray as $cache_key => $cache_value){
        $ret .="\r\n\t'".$cache_key."'=> '".$cache_value."',";
    }
    //
    $ret .="\r\n);\r\n";
    $ret .='?>';
    
    //let's create a file
    $fp = fopen('okie_data.php', 'w');
    if ($fp) {
            fputs($fp, $ret);
            fclose($fp);
            chmod($cache_filename, 0666);
    }



Question no2: How to read a file to an array?
After i create a file from code above, i will have a php file named okie_data.php that contains below code
$data_of_okie = array(
	'name' => 'Okie',
	'age' => '24',
	'language' => 'indonesian'

);


How to read this file and return $data_of_okie array?

Is This A Good Question/Topic? 0
  • +

Replies To: [HELP] How to create a file and how to read a file?

#2 noname_clark   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 75
  • Joined: 22-October 08

Re: [HELP] How to create a file and how to read a file?

Posted 05 May 2012 - 10:34 PM

View PostOKIEWARDOYO, on 05 May 2012 - 11:20 PM, said:

Question no2: How to read a file to an array?
After i create a file from code above, i will have a php file named okie_data.php that contains below code
$data_of_okie = array(
	'name' => 'Okie',
	'age' => '24',
	'language' => 'indonesian'

);


How to read this file and return $data_of_okie array?


you will do it the same way that you wrote to the file. Instead of using the "w" command, use the "w+" when you open the file. e.g.
$fp = fopen('okie_data.php', 'w+');
That way you can read and write to the file. then to read from it, just use this:
$contents = fread($fp, filesize($fp));


This will return you a string though. You'll have to extract the data you need and assign variables.
You can also try putting the data in the file one line at a time and when you grab the data, put each line into a separate row of an array.

View PostOKIEWARDOYO, on 05 May 2012 - 11:20 PM, said:

$myarray = array(
	'name' => 'Okie',
	'age' => '24',
	'language' => 'indonesian'
);



This looks like it would be easier to do with a database. You might want to look into MySQL instead, it can handle things like this a lot better and more 'simple'.
Was This Post Helpful? 0
  • +
  • -

#3 OKIEWARDOYO   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 03-April 12

Re: [HELP] How to create a file and how to read a file?

Posted 05 May 2012 - 11:27 PM

Thanks for reply,
actually, i create that php file from database,
i catch thousands of data from my mysql, then create an array, then create a php file that contains the data.

so, for the next processing, my function will not execute database again, but just read from a file.

i think it will be more save source than using database. Imagine if the site containts thousands of data and accessing hundreds of users.

HI, WAIT, is my opinion here right???
Was This Post Helpful? 0
  • +
  • -

#4 hiddenghost   User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: [HELP] How to create a file and how to read a file?

Posted 05 May 2012 - 11:41 PM

Probably wouldn't be good to use a file to replace database storage.

Visit this link
http://www.php.net/m....pdo-sqlite.php
you can use a file for a database with sqlite.

That would still be slower than a regular database, but it's an idea.

Alternatively caching may be an option if your trying to get speed.
Was This Post Helpful? 0
  • +
  • -

#5 OKIEWARDOYO   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 03-April 12

Re: [HELP] How to create a file and how to read a file?

Posted 06 May 2012 - 12:26 AM

SOLVED FOR NOW
Thanks for reply agan,
For Question number 1: may i will used that crap code for the moment to create a php file.

Fow question number 2: i have got, i can use @include(okie_data.php)

and thanks for suggesting sqlite
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3839
  • View blog
  • Posts: 14,004
  • Joined: 08-August 08

Re: [HELP] How to create a file and how to read a file?

Posted 06 May 2012 - 06:09 AM

View PostOKIEWARDOYO, on 06 May 2012 - 02:27 AM, said:

HI, WAIT, is my opinion here right???

No, it's not. The database will be faster and use fewer resources than doing it in PHP. Databases are designed to do this and PHP is not.
Was This Post Helpful? 0
  • +
  • -

#7 OKIEWARDOYO   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 03-April 12

Re: [HELP] How to create a file and how to read a file?

Posted 06 May 2012 - 06:57 AM

@CTphpnwb:
so it's not?
some times ago i read an article that said loading data from array is much faster than executing in database queries,
so i create an array here.

am i right?
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3839
  • View blog
  • Posts: 14,004
  • Joined: 08-August 08

Re: [HELP] How to create a file and how to read a file?

Posted 06 May 2012 - 07:00 AM

Sure, it's faster from an array, but it's slower from a text file. That's especially true if you end up loading a large file text file to get to a small portion of it, as you likely would in this case.
Was This Post Helpful? 0
  • +
  • -

#9 OKIEWARDOYO   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 03-April 12

Re: [HELP] How to create a file and how to read a file?

Posted 06 May 2012 - 07:12 AM

Aha, that is it,
sorry may be i was wrong on writing my question above.
so here like this:

i have amount of data in the database, i want store is in array and this array are stored in an php file called data_okie.php.

so on the other file i can include it, not read using fopen or fread. just include it

@include("okie_data.php");
return $okie_array; //this array are stored in okie_data.php




Thanks for pay attention, i appreciate it
Was This Post Helpful? 0
  • +
  • -

#10 noname_clark   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 75
  • Joined: 22-October 08

Re: [HELP] How to create a file and how to read a file?

Posted 06 May 2012 - 08:25 AM

Depending what you need to do with the data and how long it needs to be kept, you might want to look into using sessions or cookies to store the data on the users computer or on the server in a specific place. e.g. they log in or fill out the form and the data get's put in the database, and then you read it from the users computer/your server in a cookie/session instead of making another database call.
That might be a little bit faster (I'm not sure what the cookie vs. session vs. database speeds are, but if it's a massive database, I'm sure a session would be faster).
Was This Post Helpful? 1
  • +
  • -

#11 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3839
  • View blog
  • Posts: 14,004
  • Joined: 08-August 08

Re: [HELP] How to create a file and how to read a file?

Posted 06 May 2012 - 12:55 PM

If it's in the database then at some point before you store it anywhere else you need to access the database to get the information, and doing so will put that information into an array. Why not stop there? If you're really worried about access speed then you might pay close attention to how your database tables are set up and what your query looks like, but storing it in a file and then accessing that file is adding work for the processor, not reducing it.
Was This Post Helpful? 0
  • +
  • -

#12 OKIEWARDOYO   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 03-April 12

Re: [HELP] How to create a file and how to read a file?

Posted 07 May 2012 - 01:54 AM

View Postnoname_clark, on 06 May 2012 - 08:25 AM, said:

Depending what you need to do with the data and how long it needs to be kept, you might want to look into using sessions or cookies to store the data on the users computer or on the server in a specific place. e.g. they log in or fill out the form and the data get's put in the database, and then you read it from the users computer/your server in a cookie/session instead of making another database call.


I don't know is that will better for my situation, what i need is better speed, and avoid calling database every function calling. So i need a better method by creating it in array for the 'first time database calling'. After this, this array will not updated and stay static php page until i update by using administration panel.

So the structure will like below
function get_data_countries(){
    //if no array page found let's create an array page from database
    here we calling database;
    then create an array pag;
    then close calling;

    //if file array exist, then jusg include it
    @include("my_array_page.php");
    //then just return it
    return $array();
}



What do you think???

View PostCTphpnwb, on 06 May 2012 - 12:55 PM, said:

If it's in the database then at some point before you store it anywhere else you need to access the database to get the information, and doing so will put that information into an array. Why not stop there?

so, by the way , what is the best method???



View PostCTphpnwb, on 06 May 2012 - 12:55 PM, said:

If you're really worried about access speed then you might pay close attention to how your database tables are set up and what your query looks like, but storing it in a file and then accessing that file is adding work for the processor, not reducing it.

what do you think about INCLUDING this file? is it will adding extra works for processor ?
Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3839
  • View blog
  • Posts: 14,004
  • Joined: 08-August 08

Re: [HELP] How to create a file and how to read a file?

Posted 07 May 2012 - 05:31 AM

The best method involves using prepared statements in your database queries. Including files does add some overhead since the system needs to read another file, but that is trivial. The real problem is that the data can't be changed easily, so that only works for small data sets that rarely change. It's common practice to include a file that has the information for connecting to a database for example, because that information rarely changes and because it is usually different on your test platform and live one.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1