Jump to content

PHP -> writing and reading from text file not working as expected


n000bie

Recommended Posts

Hello,

 

I am writing to a text file it saves the data but when I read the text files it shows

Array ( [0] => Array ( [0] => ) [1] => Array ( [0] => ) [2] => Array ( [0] => ) [3] => Array ( [0] => ) ) 111.

 

Can someone help me how to read this data in proper format.

 

this is my php code

 

<?php 


if(isset($_POST['save_value']) && ($_POST['save_value']==7) ) {

$myfile = 'test.txt';
$file = fopen($myfile, 'a+') or die('File cannot be found.....');

$index_name = $_POST['index_name'];
$curr_point = $_POST['current_pt'];
$point_change = $_POST['points_change'];
$per_change = $_POST['per_change'];

$my_data = array($index_name,$curr_point,$point_change,$per_change);
$test_data = print_r($my_data);
fwrite($file, $test_data) or die('cannot write data!');
fclose($file);

}

if(isset($_POST['show_records'])) {
$fileread = 'test.txt';
$data = file($fileread) or die ('cannot find file');

foreach($data as $array){
	print_r($array);
}

}

?>

 

and this is my html code

 

<div>
<form method="post" action="">
<div>
<span>Index</small></strong></span>
<span><strong><small>Current</small></strong></span>
<span><strong><small>Points Change</small></strong></span>
<span><strong><small>%Change</small></strong></span>
</div>
    <div class="expDiv">
   <span><input type="text" name="index_name[]" id="a1" /></span>
<span><input type="text" name="current_pt[]" id="a2" /></span>
<span><input type="text" name="points_change[]" id="a3" /></span>
<span><input type="text" name="per_change[]" id="a4" /></span>
  </div>
<div><span class="addme">Add More</span> <span class="removeme">Remove</span>
  <input type="submit" name="button" id="button" value="Submit" />
<input type="hidden" name="save_value" value="7" />

<input type="submit" name="show_records" value="Show Records" />
</div>
</form>
</div>

Link to comment
Share on other sites

Can someone help me how to read this data in proper format.

 

What exactly do you mean by proper format? Considering your using print_r to format the array, that is how your data is saved.

 

Well I save data like this

SE        545.00          0.46        0.09%

NE        135.11          0.09        0.06%

 

and I am getting Array Array Array Array

 

and in my text file there is also

Array Array Array Array

 

I have changed my php code also

 

$my_data = array($index_name,$curr_point,$point_change,$per_change);
$test_data = print_r($my_data,true);
$fp=fopen("test.txt","w+");
foreach($my_data as $key => $value){
fwrite($fp,$value."\t");
}

fclose($fp);

 

 

Have you tried using the return parameter on the print_r function that might give you what you are looking for.

$test_data = print_r($my_data,true);

 

it also prints out Array Array Array Array

:confused:

 

Link to comment
Share on other sites

My guess would be the values you are putting into the $my_data array are wrong..

Try echoing them out

when I echo , it shows array

My form fields are also in array (added by js when user clicks add more). How can I save those ?

 

Help me pls...

 

You've print_r($_POST); and it has come out correct? That is where you would access the 'array', such as $_POST['foo'][2]

Link to comment
Share on other sites

Ok, now I changed my php code, this codes write correctly to text file but when reading I am getting error

 

 

Warning: Invalid argument supplied for foreach() in C:\wamp\www\earth\index.php on line 63

 

<?php function array2string($myarray,&$output,&$parentkey){
      foreach($myarray as $key=>$value){
         if (is_array($value)) {
            $parentkey .= $key."^";
            array2string($value,$output,$parentkey);
            $parentkey = "";
         }
         else {
            $output .= $parentkey.$key."^".$value."\n";
         }
      }
   }


   function string2array($string,&$myarray){
      $lines = explode("\n",$string);
      foreach ($lines as $value){
         $items = explode("&#183;",$value);
         if (sizeof($items) == 2){
            $myarray[$items[0]] = $items[1];
         }
         else if (sizeof($items) == 3){
            $myarray[$items[0]][$items[1]] = $items[2];
         }
      }
   }


if(isset($_POST['save_value']) && ($_POST['save_value']==7) ) {

$index_name = $_POST['index_name'];
$curr_point = $_POST['current_pt'];
$point_change = $_POST['points_change'];
$per_change = $_POST['per_change'];

$mydb = array($index_name, $curr_point, $point_change, $per_change);
print_r($mydb);
// Convert the array into string
   	array2string($mydb,$output,$parent);

// Store the string in a file   
   	$f1 = fopen("test.txt","w+");
   	fwrite($f1,$output);
   	fclose($f1);
}

if(isset($_POST['show_records'])) {

// Read the file back from the disk
   $f1 = fopen("test.txt","r");
   $newString = fread($f1,filesize('test.txt'));
   fclose($f1);
   
   // Convert the content back to an array
   string2array($newString, $newArray);

   // Print out the array
   foreach ($newArray as $item) {   // this is line 63 
      print_r($item);
   }
}
?>

 

any help

 

Link to comment
Share on other sites

Change your function to....

 

function string2array($string) {
      $lines = explode("\n",$string);
      foreach ($lines as $value){
         $items = explode("&#183;",$value);
         if (sizeof($items) == 2){
            $myarray[$items[0]] = $items[1];
         }
         else if (sizeof($items) == 3){
            $myarray[$items[0]][$items[1]] = $items[2];
         }
      }
      return $myarray;
   }

 

And your calling code to....

 

$newArray = string2array($newString);

   // Print out the array
   foreach ($newArray as $item) {   // this is line 63 
      print_r($item);
   }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.