Hello everyone, I have a text file which has some values structured as below

--------------------------------------

1

2

3

4

--------------------------------------

Then i made a PHP file which can receive checkbox values (always receive 2,3 & 4) from another HTML file as below

if($_POST['Button']){

$check = $_POST['$boxname'];

foreach($check as $chk ){

 echo "$chk<br>"; // Output of this always 2,3 & 4

}
}

-------------------------------------------

I have another script which can read the text file as below

$fileopen = fopen("text.txt", "r");
if ($fileopen) {
while (!feof($fileopen)) {

$read = fgets($fileopen);

$get_string = explode(",",$read);

$values = $get_string[0]; 


}

}

How can i combine this code with the foreach loop in order to add 1 to the values of the text file if checkbox values are identical to them. I know i can use r+ to write data into a file. But i don't understand how to put them together.

Thanks everyone in advance

Member Avatar

diafol

Is this homework? Bumping after just 48 minutes is a bit extreme mate.

@ardav it is a part (final part) of my coursework. Thanks for your reply

Member Avatar

diafol

OK, you need to explain yourself better than this. You have 4 numbers in your text file 1,2,3,4. Your checkboxes have values 2,3,4.

Yoiu want to increment the file values by 1?

What file values? The 1,2,3,4?

BTW: this smacks of the old AS/A level stuff. Where VB/text files or VB/Access used to be used. No chance using MySQL to keep your data?

@ardav i have text file which has data not separated by commas but start with new line as below

1
2
3
4

checkboxes send the values 2,3 & 4 to a php file and it will compare whether these values are identical to the values in text file (obviously in this case 2,3 & 4 matches). So 2,3 & 4 matches with the text file, therefore add 1 to 2,3 & 4 in the text file values. New text file will look like below

1
3
4
5


It is compulsory for us to use a text file

Thanks for your time

Member Avatar

diafol

The mind boggles!!

OK, as this is coursework, you're going to have to do the work. Ensure that you have access to the php manual (online or local copy). The online copy is probably better as there are a number of examples of usage included.

My suggestions
1. I'd read the file using file_get_contents() and write back using file_put_contents().

2. Exploding data:
The fact that you don't separate values with a comma, just a newline isn't a problem, but it's just a pain. You have to explode on "\n" - exactly that - double quotes. If this doesn't work, perhaps you could try "\r\n", but I don't think you'll need to.

3. Do a foreach array loop. This will loop through each array item for each one check if it is equal to 4 or 3 or 2 (you can use just a single if test for this using in_array() for your checkbox values). If TRUE, add one to the value. **

4. That's it for the values. Now just implode() the array using "\n" as a delimiter.

**In part 3 you have the choice as to whether you want to overwrite your array items or to create and fill a new array. Either should be fine.

Hope that helps - the reason for using the checkbox values in their original array is that checking for multiple possible values is far easier than having to manage manual OR (||) conditional. It also scales well if you add or remove checkboxes.

@ardav Thanks a lot for your time. I'll give it a go.

Member Avatar

diafol

Ok, good luck. I've just read back my post and perhaps it wasn't as lucid as I thought. If you hit a wall or want elucidation, come back.

I'll be here for a little while, then I'm gonna have to decorate my daughter's bedroom. Teacher's half term treat! So, if I'm not back soon, don't bump. I'll be back later on tonight.

@ardav thanks again. If i get stuck I'll post it here

I made a terrible mistake by mentioning that i want to add 1 to the information if they match the contents of the text file. All i want to do is add 1 the information if they matches the position of the information.

and the check box values are going to be 0,1 & 2

Please forget about the while loop above. However text file is going to be the same format. I have managed to come up with a new for loop which can detect the array position of the information after reading the text file line by line as below

$filename = "test.txt";
 $filepointer = fopen($filename,"r"); 
 $myarray = file ($filename);
 for ($mycount = 0; $mycount < count($myarray); $mycount++ )
    {			
      $aline = $myarray[$mycount]; // $mycount is the position 
	  
	   $info = explode(" ",$aline);
	   
	   	$values = $info[0];
		
		echo "$mycount |"."$values<br>";
	}

Thanks for you time and sorry for the confusion

Member Avatar

diafol

You've completely lost me now. Add one to what? What matches the position?? Could you give an example please.

@ardav yes that is what i exactly want. Add 1 to what matches the position

Thanks for your reply

Member Avatar

diafol

What?? Lost - totally.

text file which has some values structured as below

--------------------------------------

1

2

3

4

PHP file which can receive checkbox values (always receive 0, 1 & 2) from another HTML file as below

if($_POST['Button']){
 
$check = $_POST['$boxname'];
 
foreach($check as $chk ){
 
 echo "$chk<br>"; // Output of this always 0,1 & 2
 
}
}

I have another script which can read the text file information as below.

$filename = "test.txt";
 $filepointer = fopen($filename,"r"); 
 $myarray = file ($filename);
 for ($mycount = 0; $mycount < count($myarray); $mycount++ )
    {			
      $aline = $myarray[$mycount]; // $mycount is the position 
 
	   $info = explode(" ",$aline);
 
	   	$values = $info[0];
 
		echo "$mycount |"."$values<br>";
	}

When it find the position zero which has the value 1 it will add 1 to it and so on. And at the end text file will look like below

2

3

4

4

Member Avatar

diafol

Ah, OK I see. I'll get back to you (about 2 hours). I'm watching Resident Evil / Afterlife at the moment.

Member Avatar

diafol

$vals = $_POST['boxcheck']; //I assume this is now an array containing 0,1,2
$str = file_get_contents('test.txt');
$parts = explode("\n", $str);
foreach($parts as $key=>$value){
  if(in_array($key,$vals)){
     $new[] = $value + 1;
  }else{
     $new[] = $value;
  }
}
$out = implode("\n",$new);
file_put_contents("test.txt",$out);

I think it works...

//EDIT

Yes, it does

@ardav thanks i will give it a go

Member Avatar

diafol

IF it works mark the thread solved (the link below this post).

ardav I'm extremely sorry to trouble you again. I have another question related to the PHP thread(Update existing values of a text file). Could you please suggest me solution for this?


My text file need to be as below
-----------
Jhon,1
Smith,2
Anna,3
----------

so the checkboxes will send the values 0,1&2 same as before. If the values are equal to the line number then it will update the value next to to the comma @ the end text file will look like this

-----------
Jhon,2
Smith,3
Anna,4
----------

Member Avatar

diafol

<?php
$vals = $_POST['boxcheck']; //I assume this is now an array containing 0,1,2
$str = file_get_contents('test.txt');
//break apart
$parts = explode("\n", $str);
foreach($parts as $part){
	$complete[] = explode(",",$part);
}
//rejoin and change
foreach($complete as $key=>$value){
  if(in_array($key,$vals)){
  	$new[] = implode(",",array($value[0],$value[1] + 1));//value[0] is the name, value[1] is the number
  }else{
  	$new[] = implode(",",array($value[0],$value[1]));
  }
}
$out = implode("\n",$new);
file_put_contents("test.txt",$out);
?>

I have to say, there must be an easier way than this. I think I may have overcomplicated things.

@ardav u r amazing! thanks a lot for ur support.