1

I have a php file that reads in information from a txt file and prints it on the screen into lines such as

1st line [ x ]
2nd line[ x ] etc etc etc

i am trying to add checkboxes next to all the lines of information, i managed to do a for loop that creates checkboxes depening on how many lines are read.

Now the final thing which i am stuck on is that i want the user to be able to click on any checkboxes and then click the submit button which should print out the chosen information on a new php file.

If the user ticked 1st line and submitted then it should display the text string "1st line" on the opening php file

I done some research and managed to use isset method to find out if it was checked, that worked but im still unsure how to read the information that was checked onto a new php file any help would be appreciated thank you

$filename = "file.txt";

$filepointer = fopen($filename, "r"); //open for read

$myarray = file ($filename);

// get number of elements in array with count
for ($counts = 0; $counts < count($myarray); $counts++)

{ //one line at a time
$aline = $myarray[$counts];

//$par = array();
$par = getvalue($aline);

if ($par[1] <= 200) 
{ 

print "<input type=checkbox name='test'/>"." ".$par[0]." "; 
print $par[1]." ";
print $par[2]." ";
print $par[3]." ";

}

}

1 Answer 1

2

I think you are probably wanting to create an array which identifies which lines were checked? Well, you'll want to use an array to name your checkbox inputs. You can do this with a very similar syntax to PHP, by appending [] to the input name. For this specific case, you'll also want to explicitly index the array keys, which you can do like [index]. It will be easier to demonstrate this in code:

file1.php (FIXED):

<?php

$filename = "file.txt";

// file() does not need a file pointer
//$filepointer = fopen($filename, "r"); //open for read

$myarray = file($filename);

print "<form action='file2.php' method='post'>\n";

// get number of elements in array with count
$count = 0; // Foreach with counter is probably best here
foreach ($myarray as $line) {

  $count++; // increment the counter

  $par = getvalue($line);

  if ($par[1] <= 200) {
    // Note the [] after the input name
    print "<input type='checkbox' name='test[$count]' /> ";
    print $par[0]." "; 
    print $par[1]." ";
    print $par[2]." ";
    print $par[3]."<br />\n";
  }

}

print "</form>";

file2.php:

<?php

  foreach ($_POST['test'] as $lineno) {
    print "Line $lineno was checked<br />\n";
  }

EDIT

Say you wanted file2.php to display the lines from the file that were checked:

<?php

  $filename = "file.txt";

  $myarray = file($filename);

  foreach ($_POST['test'] as $lineno) {
    // We need to subtract 1 because arrays are indexed from 0 in PHP
    print $myarray[$lineno - 1];
  }
Sign up to request clarification or add additional context in comments.

4 Comments

thanx for the help but when i run the php file it just prints out "Line on was checked" rather then printing the information from the text file
Well you would need to read the file into memory again in the file2.php. Hang on, I will edit.
it gave me a error showing Notice: Undefined offset: -1 , - further more it wasnt dispaying the count variable for some strange reason.
@Hashey100 I'm guessing you didn't check any boxes then. The only thing the edited code is designed to do is print the selected lines verbatim, it won't print the line numbers, although simply changing the print line to something like print "Line $lineno: ".$myarray[$lineno - 1]; would fix that. Make sure you are using the file1 code as it is now, I edited a couple of mistakes out after my first post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.