How to write response into label box - php

I have a flat file TestFile.txt that has about 40 lines of data, each item on a separate row, so 40 lines. I have a PHP code that finds the row containing a string I want to find using using $Search_String. Then displays only the row containing $Search_String. This works exactly as I want. However; it displays the result in text area. How do I display the result into a label box?
Here is part of my flat file, filename is TestFile.txt:
RXFrequency=432675000
TXFrequency=432675000
RXOffset=260
TXOffset=120
Network=mnet.hopto.org
Password=8Yg81xrqK0313zt
Latitude=34.657783
Longitude=-3.784595
Port=62021
Here is my PHP:
<?php
// Place text to look for in string $Search_String.
// The $Search_String will remain hard coded in my production
// code. The users will not be able to select $Search_String.
$Search_String = "Lon";
// Identify Text File, open File and Read the File.
$MyFile = fopen("TestFile.txt", "r") or die("Unable to open file!");
$found= "False";
// Create the while loop. Test each line with the if statement,
// looking for $Search_String, and place the result into string $line.
// Next, echo string $line which containes the found line in the
// flat text file. It will return the entire line even from a
// partial $Search_String, which is what I want.
while ( $line = fgets( $MyFile ) )
{
if ( str_contains( $line, $Search_String ) )
{
echo $line;
break;
}
}
// If the string $Search_String was not found, show a message
if(!$found)
{
echo 'No match found';
}
// Properly close the text file.
fclose($MyFile);
?>
The PHP code above returns:
Longitude=-3.784595
Just what I want, but I need it displayed in text box, or label box.

You need to print the answer as html
<?php
// Place text to look for in string $Search_String.
// The $Search_String will remain hard coded in my production
// code. The users will not be able to select $Search_String.
$Search_String = "Lon";
// Identify Text File, open File and Read the File.
$MyFile = fopen("TestFile.txt", "r") or die("Unable to open file!");
$found= "False";
// Create the while loop. Test each line with the if statement,
// looking for $Search_String, and place the result into string $line.
// Next, echo string $line which containes the found line in the
// flat text file. It will return the entire line even from a
// partial $Search_String, which is what I want.
while ( $line = fgets( $MyFile ) )
{
if ( str_contains( $line, $Search_String ) )
{
echo "<div style="
border: double;
">".$line."</div>";
break;
}
}
// If the string $Search_String was not found, show a message
if(!$found)
{
echo 'No match found';
}
// Properly close the text file.
fclose($MyFile);
?>

Related

How to show php string in html input

I have a flat file TestFile.txt that has about 40 lines of data, each item on a separate row, so 40 lines. I have a PHP code that finds the row containing a string I want to find using using $Search_String. Then displays only the row containing $Search_String. This works exactly as I want. However; it displays the result in text area. How do I display the result into a label box? The php reads the flat just as I would expect. I have 2 html input fields, but the string $line only contains data inside the if statement. Any help with this would be great!
Here is part of my flat file, filename is TestFile.txt:
RXFrequency=432675000
TXFrequency=432675000
RXOffset=260
TXOffset=120
Network=mnet.hopto.org
Password=8Yg81xrqK0313zt
Latitude=34.657783
Longitude=-3.784595
Port=62021
Here is my PHP:
<!DOCTYPE html>
<html>
<body>
<?php
$line = '';
?>
<input type="text" ID="message" value="<?php echo $line;?>"/>
<?php
// Place text to look for in string $Search_String.
// The $Search_String will remain hard coded in my production
// code. The users will not be able to select $Search_String.
$Search_String = "RXF";
// Identify Text File, open File and Read the File.
$MyFile = fopen("TestFile.txt", "r") or die("Unable to open file!");
$found= "False";
// Create the while loop. Test each line with the if statement,
// looking for $Search_String, and place the result into string $line.
// Next, echo string $line which containes the found line in the
// flat text file. It will return the entire line even from a
// partial $Search_String, which is what I want.
while ( $line = fgets( $MyFile ) )
{
if ( str_contains( $line, $Search_String ) )
{
echo $line;
echo $Search_String;
}
}
// Properly close the text file.
fclose($MyFile);
?>
<input type="text" id="message" value="<?php echo $line;?>"/>
</body>
</html>
This code does properly open the flat text file, it does the search and finds
the line containing the data I am trying to retrieve. I see the correct gata using echo in the if statement. Now I need to place the found data into the html input text box. The string $line seems to contain no data outside the if block of code. Can I make the variable $line static or global so the data is available throughout the code? Or am I using the html input text improperly? I think I do need to use input text, because I want to be able to modify the data and then write the line back to the flat file.
Thanks for any suggestions,
Mitch
This is a small grasp of your code including the proposed solution to loop through and show all the lines matching the criteria from your file:
<?php
/*...*/
$lines = [];
while ( $line = fgets( $MyFile ) ) {
if ( str_contains( $line, $Search_String ) ) {
//here you are keeping track of each line matching criteria
$lines[] = $line;
//echo $line;
//echo $Search_String;
}
}
?>
<?php foreach($lines as $line): ?>
<input type="text" value="<?= $line; ?>"/>
<?php endforeach; ?>

PHP write to flat text file. New to PHP, learning. I have the read working. Need to write back updates

I have a flat file, TestFile.txt, that contains about 200 lines. Each item is a separate row. I show a partial of the contents of the TestFile.txt file below. I have PHP code working that reads TestFile.txt exactly as I need. The PHP read code searches the TestFile.txt, locates the line I wish to read, and places the result into an html input box. It parses the text after the = in the line, and only displays the data found after the =. Just as I need. Now I need to change the data in the html input box, and write the change back to TestFile.txt, and only update the text after the =. I show the PHP read code below. I have not a clue how to do what I need. I am a little over a week studying PHP. Any help with writing is much appreciated.
Thanks,
Mitch
Partial TestFile.txt:
RXFrequency=432675000
TXFrequency=432675000
RXOffset=260
TXOffset=120
Network=mnet.hopto.org
Password=9Yg81prqL0363zt
Latitude=34.657783
Longitude=-3.784595
Port=62021
Part of the PHP:
<!DOCTYPE html>
<html>
<body>
<?php
// Place text to look for in string $Search_String.
// The $Search_String will remain hard coded in my production
// code. The users will not be able to select $Search_String.
$Search_String_1 = "RXOff";
// Identify Text File, open File and Read the File.
$MyFile = fopen("TestFile.txt", "r") or die("Unable to open file!");
$found= "False";
// Create the while loop. Test each line with the if statement,
// looking for $Search_String, and place the result into string $line.
// Next, echo string $line which containes the found line in the
// flat text file. It will return the entire line even from a
// partial $Search_String, which is what I want.
/*...*/
// Next, let us build the array.
$lines = [];
while ( $line = fgets( $MyFile ) ) {
if ( str_contains( $line, $Search_String_1 ) ) {
//here you are keeping track of each line matching the criteria.
$lines[] = $line;
// This explode function will split the string contained
// in the $line variable at the =. Text left of the = is
// placed into the $key variable. Text right of the = is
// placed into the $value variable.
[$key, $value] = explode("=", "$line");
// echo $key; // RXOffset;
// echo $value; // 260;
//echo $line;
//echo $Search_String_1;
}
}
?>
<?php foreach($lines as $line): ?>
<?php endforeach;
// Properly close the text file.
fclose($MyFile);
// Get string $value from the explode code above.
?>
<label>RXOffset: <input type="text" id="message" value="<?php echo $value;?>"/></label>
<?php
</body>
<html>
Hope this gives enough information. Feel free to comment questions.
Thanks,
Mitch
This is what appears on the browser when I execute this PHP:
RXOffset: 269
Label Data

PHP Read Line From Text File, Only Selected Line

I am trying to search a text file for a string, and then display only the one line that was found. Would like to display the found line in a label box. My PHP is really weak. I got the following code working which will display the first line in the text area. Below is the contents of the text file: test.txt
First line
Second Line
Third Line
Fourth Line
My code is as follows:
<!DOCTYPE html>
<html>
<body>
<?php
$myfile = fopen("test.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>
</body>
</html>
This code returns First Line into the text area, so how can I return First Line to
text box, label?
Any pointer toward the right direction?
Thanks
Mitch
You'd want to continue fgets($file) in a loop, and checking if the search string is in the result, then when the search string is found, echo the line. Something like this maybe...
while ( $line = fgets( $myfile ) ) {
if ( str_contains( $line, $search_string ) ) {
echo $line;
break;
}
}
Note that str_contains is php 8 and up only. There are other methods for lower versions.
Also consider that perhaps a database would be better for this type of operation rather than using a flat file...

How Do You Print A Specific Line Of A Text File?

I have a program that searches a text file to see if a certain string is in that file, which I have gotten to work fine. What I need to know is how to print a particular line of that text file. For example, if the file lists three street names each on its own line, and the program searches for one of them, I would want it to print out only the line that has that street name on it.
If the file looked like this: and the word being searched for was Rose Road,
I want it to only print out 6784 Rose Road
4543 Drock Drive
1254 HeadHill Road
6784 Rose Road
This is what I have so far, which checks if it's in the file, but I am just unsure how to print out a particular line.
$roadName = "Rose";
$handle = fopen("streets.txt", "r");
if(strpos(file_get_contents("streets.txt"),$roadName) !== false) //Checks to make sure its in the file.
{
echo fgets($handle); //This is what I was trying, but it only prints the 1st line.
}
file_get_contents and strpos have no effect on the file handle, so fgets just read from the beginning of the file.
You can read each line with fgets, test if it matches the string, and then print it.
while ($line = fgets($handler)) {
if (strpos($line, $roadName) !== false) {
echo $line;
break;
}
}
If the file is very large, this is better than the solution that uses file_get_contents, because it doesn't have to read the entire file into memory and then create a huge array of all the lines.
I would explode the lines into an array and the check every line:
$roadName = "Rose";
$file=file_get_contents("streets.txt")
if(strpos($file,$roadName) !== false) //Checks to make sure its in the file.
{
$lines = explode(PHP_EOL,$file);
foreach($lines as $line) {
if (strpos($line,$roadName) !== false) echo($line);
}

How to go to first line of the file in php?

I am reading the file and getting the particular line if there is a match for the searched string. There are bunch of strings to be searched which are stored in a array. I cant be opening the file every time when i loop through the array to get the string. But want to go to the first line of the file and start searching again. The file contains around 15k lines. If i open the file every time(inside the loop) its working fine. but if the open the file outside the loop. Only the first matched string line is returned.
$scheme_code =
array("106212","112422","114239","104685","100122","118191","131666");
foreach($scheme_code as $searchthis) {
$handle = #fopen("myfile", "r");
//DONT WANT TO DO THE ABOVE LINE FOR EVERY ITERATION
if ($handle)
{
//echo "handle open"."<br>";
while (!feof($handle))
{
$buffer = fgets($handle,4096);
if(strpos($buffer, $searchthis) !== FALSE){
$matches[] = $buffer;
}
}
}
}
But want to do something like this
$handle = #fopen("Myfile", "r");
foreach(){
// inside foreach
//go to the first line of the file
}
fclose($handle);
EDIT - I tried rewind(). I got the notice "rewind(): stream does not support seeking"
Here you can use file() function which will give you complete array of lines and after that you can match line by line without using your IO resource everytime by fopen.
<?php
$linesArray = file("/path/to/your/file.txt");
foreach($linesArray as $line){
// do the stuff or matching you want to perform line by line on $line
}

Categories