The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.
PhP coding - A question which is likely obvious to anyone who has done this a lot
So, I'm doing some coding for a website I maintain at work, and usually it's just simple stuff but I want to add a new feature. It requires me to be able to take the list of numbers which look like this in an external txt file...
Any thoughts? I'm sure it should be simple, it would certainly be trivial in C or Fortran.
edit - Oh, and the numbers in the external file are seperated by tabs, at least for now. Primarily I just need a way that I can have a place to save some variables in a logical place which are created during a session using the website, and then load them back in when you come back to it.
I would use file_get_contents() to read the file. Then I would use explode() with a newline (\n) as a delimiter to create an array whose elements are each of the lines of the file. Finally, I would use a combination of list() and explode() within a foreach block to separate the individual items and use the resulting variables to populate your array.
I can write the code for you, but I'm hesitant to in case this is homework or something.
*Edit, reading further, I see this is for a website you're working on and not homework so here you go:
<?php
// Path to file
$filename = '/path/to/file';
// Open file and save contents
$contents = file_get_contents($filename) or die('Cannot open file!');
// Split file contents into array based on newline
$array = explode("\n", $contents);
// Loop over array elements
foreach ($array as $k => $v)
{
// Assign variables split on tabs
list($one, $two, $three) = explode("\t", $v);
// Assign data to big array
$newarray[$one][$two] = $three;
}
?>
Posts
http://php.net/manual/en/function.explode.php
http://php.net/manual/en/function.fgets.php
Basically, read the file line by line. Split each line into its own little array. Use that array to populate each row of your final array.
Or you could use sqlite to store that info between sessions.
I can write the code for you, but I'm hesitant to in case this is homework or something.
*Edit, reading further, I see this is for a website you're working on and not homework so here you go: