Apologies for having to ask.
In short I'm making a simple imageboard with a "like" button for each image. The number of clicks (likes) stores in 'counter.txt' file in the following format:
click-001||15
click-002||7
click-003||10
Clicking the buttons initiates a small php code via AJAX. counter.php:
<?php
$file = 'counter.txt'; // path to text file that stores counts
$fh = fopen($file, 'r+');
$id = $_REQUEST['id']; // posted from page
$lines = '';
while(!feof($fh)){
$line = explode('||', fgets($fh));
$item = trim($line[0]);
$num = trim($line[1]);
if(!empty($item)){
if($item == $id){
$num++; // increment count by 1
echo $num;
}
$lines .= "$item||$num\r\n";
}
}
file_put_contents($file, $lines);
fclose($fh);
?>
So when I run the website and testclick my buttons I get the following message:
Notice: Undefined offset: 1 in C:\wamp64\www\wogue\counter.php on line
18
I figured that the script 'counter.php' creates a whitespace on a new string in 'counter.txt' and so it fails to 'explode' and thus make a [1] index. The way I figured that is by backspacing the last empty line in .txt file and saving it. It ran without errors until I clicked a button a few times then the same error appeared.
The piece of code in index looks like this:
<?php
$clickcount = explode("\n", file_get_contents('counter.txt'));
foreach($clickcount as $line){
$tmp = explode('||', $line);
$count[trim($tmp[0])] = trim($tmp[1]);
}
?>
Any ideas?..
Trim $line and if it is not empty - do what you need:
$line = trim(fgets($fh));
if ($line) {
$line = explode('||', $line);
$item = trim($line[0]);
$num = trim($line[1]);
if(!empty($item)){
if($item == $id){
$num++; // increment count by 1
echo $num;
}
$lines .= "$item||$num\r\n";
}
}
Or check with empty this way:
$line = explode('||', fgets($fh));
if(!empty(line[0]) && !empty($line[1])){
if(line[0] == $id){
$line[1]++; // increment count by 1
echo $line[1];
}
$lines .= "{$line[0]}||{$line[1]}\r\n";
}
}
You are writing using \r\n as a line separator in counter.php and reading the same file exploding only for \n. You should be consistent.
Just removing the \n should be enough to avoid the extra "space" you're seeing.
<?php
$file = 'counter.txt'; // path to text file that stores counts
$fh = fopen($file, 'r+');
$id = $_REQUEST['id']; // posted from page
$lines = '';
while(!feof($fh)){
$line = explode('||', fgets($fh));
$item = trim($line[0]);
$num = trim($line[1]);
if(!empty($item)){
if($item == $id){
$num++; // increment count by 1
echo $num;
}
$lines .= "$item||$num\n"; //removing the \r here
}
}
file_put_contents($file, $lines);
fclose($fh);
?>
Related
I have a file that looks like this:
1||Allan||34||male||USA||||55.789.980
2||Georg||32||male||USA||||55.756.180
3||Rocky||21||male||USA||[100][200]||55.183.567
I made a function that when executed adds a given number or removes it if already present, which is $added and equals 100 for this example. This is my code:
$added = $_GET['added']; //100 for this example
$f = fopen($file, "w");
$list = file($file);
foreach ($list as $line) {
$details = explode("||", $line);
if (preg_match("~\b$details[0]\b~", 3)) {
foreach ($details as $key => $value) {
if ($key == 5) {
$newline .= str_replace("[" . $added . "]", "", $value);
} else {
$newline .= $value . "||";
}
}
$line = $newline . "\n";
}
fputs($f, $line);
}
fclose($f);
}
this code is supposed to remove the [100] from the Rocky line since its already present which it kinda does. However, upon further execution instead of adding it back it duplicates the Rocky line and messes it up so the file looks like this:
1||Allan||34||male||USA||||55.789.980
2||Georg||32||male||USA||||55.756.180
3||Rocky||21||male||USA||[100][200]55.183.567
3||Rocky||21||male||USA||[100][200]55.183.567
||
||
why is it doing this? I cant make any sense out of it...
Thank you.
First, you should read the file before you open it for output, because opening with the w mode truncates the file.
Second, you don't need to loop over the fields in $details if you just want to change one of them. Just access and assign it by index.
Then you can put the line back together with implode().
$list = file($file);
$f = fopen($file, "w");
foreach ($list as $line) {
$details = explode("||", $line);
if (preg_match("~\b$details[0]\b~", 3)) {
if (strpos("[$added]", $details[5]) === false) {
$details[5] = "[$added]" . $details[5];
} else {
$details[5] = str_replace("[$added]", "", $details[5]);
}
$line = implode('||', $details)
}
fputs($f, $line);
}
fclose($f);
the problem is simple but complicated at the same time.
feof doesn't print my last word. It take from file name city and code (Venice,A908) and should show in OUTPUT: nameCity,codeOfCity.
Let me show you an example:
City.csv
Abano Terme,A001
Abbadia Cerreto,A004
Abbadia Lariana,A005
Abbiategrasso,A010
Zubiena,M196
Zuccarello,M197
Zuclo,M198
Zungri,M204
Code:
<?php
$buffer = "";
$file = fopen("City.csv", "r");
//while (($c = fgetc($file)) != EOF )
//while (($c = fgetc($file)) != NULL )
//while (($c = fgetc($file)) !== false )
while(!feof($file))
{
$c = fgetc($file);
$buffer .= $c;
if($c == ",")
{
echo $buffer;
$buffer = "";
}
if($c == "\n")
{
echo $buffer."<br/>";
$buffer = "";
}
}
fclose($file);
?>
OUTPUT:
Abano Terme,A001
Abbadia Cerreto,A004
Abbadia Lariana,A005
Abbiategrasso,A010
Zubiena,M196
Zuccarello,M197
Zuclo,M198
Zungri,
Since it seems like you are just trying to output the file as is, with only change being to substitute HTML line breaks <br /> instead of new line characters why not simplify things?
echo nl2br(file_get_contents('City.csv'), true);
Or if you don't want to read the whole file into memory:
$file = fopen('City.csv', 'r');
while(!feof($file)) {
echo nl2br(fgets($file), true);
}
fclose($file);
In one of the comments above you mention that you want the city and city values available as variables (though your code example doesn't seem to indicate this). If that is the case, try fgetcsv() like this:
$file = fopen('City.csv', 'r');
while($values = fgetcsv($file)) {
$city = $values[0];
$city_code = $values[1];
echo $city . ',' . $city_code . '<br />';
}
fclose($file);
Your problem is, there's no newline at the end of your file, so it never hits the last "\n" check to output the buffer contents.
to fix this, you just need to put in another check on that conditional. change
if($c == "\n")
to:
if($c == "\n" || feof($file))
Here's a much cleaner and more concise version of your code if you'd like to use the correct function for parsing a csv file:
<?php
$buffer = array();
$file = fopen("City.csv", "r");
while(!feof($file) && $buffer[] = fgetcsv($file));
fclose($file);
foreach($buffer as $line){
echo join(',', $line).'<br/>';
}
?>
I have some PHP function that requires the line number of a CSV file used as database. Once it has line, it navigates to the specific value that needs to be changed, changes it and rewrites the whole files. Here is my code:
<?php
function update($file, $id, $field, $value)
{
//$id is the line number
$contents = explode("\n", file_get_contents($file));
$fh = fopen($file, "w");
$lines = array();
foreach($contents as $line)
{
if($line == "")
continue;
$fields = explode("|", $line);
if($fields[0] == $id)
{
$line = null;
for($i = 0; $i<count($fields); $i++)
{
if($i == $field)
$fields[$i] = $value;
if($i != count($fields)-1)
$line .= $fields[$i]."|";
else
$line .= $fields[$i];
}
}
$line .= "\n";
fwrite($fh, $line);
}
fclose($fh);
$contents = null;
return true;
}
$id = $_SESSION['id'];
$uid = $_GET['p'];
$myfile = "myfile.txt";
if(update($myfile, 12, 14, "somevalue"))
echo "updated!";
?>
I am unable to find the problem because whenever I run the code, it outputs "updated!" just as it should but when check the file, I find it has not been updated. I do not know why, but it always remains the same! Thanks.
Check that fwrite() is not failing.
Do something like this:
...
$writeSuccess = (fwrite($fh, $line) !== false);
}
fclose($fh);
$contents = null;
return $writeSuccess;
}
...
If it is failing, check that your filesystem permissions are correctly set. The Apache user needs to have write access to whatever file/folder you are writing the file to.
I found out what the problem was.
$id = $_SESSION['id'];
$uid = $_GET['p'];
$myfile = "myfile.txt";
if(update($myfile, 12, 14, "somevalue"))
The line number pointed to the previous line, which made it impossible to update the first line of the file. So all I had to do was
$line ++;
I am trying to search a line in a text file and then print the following three lines. For example, if the text file has
1413X
Peter
858-909-9999
123 Apple road
then my PHP file would take in an ID ("1413X") through a form, compare it to lines in the text file - essentially a mock database - and then echo the following three lines. Currently, it is echoing only the phone number (with the second half of the numbers wrong??). Thanks for your help.
<?php
include 'SearchAddrForm.html';
$file = fopen("addrbook.txt", "a+");
$status = false;
$data = '';
if (isset($_POST['UserID']))
{
$iD = $_POST['UserID'];
$contact = "";
rewind($file);
while(!feof($file))
{
if (fgets($file) == $iD)
{
$contact = fgets($file);
$contact += fgets($file);
$contact += fgets($file);
break;
}
}
echo $contact;
}
fclose($file);
?>
It is better to set some flag that you found id and some counter to count lines after it to achieve your aim.
<?php
include 'SearchAddrForm.html';
// $file = fopen("addrbook.txt", "a+");
$file = fopen("addrbook.txt", "r");
$status = false;
$data = '';
if (isset($_POST['UserID']))
{
$iD = $_POST['UserID'];
$contact = "";
rewind($file);
$found = false;
$count = 1;
while (($line = fgets($file)) !== FALSE)
{
if ($count == 3) // you read lines you needed after you found id
break;
if ($found == true)
{
$contact .= $line;
$count++
}
if (trim($line) == $iD)
{
$found = true;
$contact = $line;
}
}
echo $contact;
}
fclose($file);
?>
This kind of example how you can achieve this. And as you see in comment you should use $contact .= value, not $contact += value.
Also instead of reading you can take the whole file in array line by line using function file.
And why are opening file for writing?
What I did:
<?php
//input (string)
$file = "before\n1413X\nPeter\n858-909-9999\n123 Apple road\nafter";
//sorry for the name, couldn't find better
//we give 2 strings to the function: the text we search ($search) and the file ($string)
function returnNextThreeLines($search, $string) {
//didn't do any check to see if the variables are not empty, strings, etc
//turns the string into an array which contains each lines
$array = explode("\n", $string);
foreach ($array as $key => $value) {
//if the text of the line is the one we search
//and if the array contains 3 or more lines after the actual one
if($value == $search AND count($array) >= $key + 3) {
//we return an array containing the next 3 lines
return [
$array[$key + 1],
$array[$key + 2],
$array[$key + 3]
];
}
}
}
//we call the function and show its result
var_dump(returnNextThreeLines('1413X', $file));
I am trying to select columns in my tab separated document.
The problem i am having is i get this error....
Notice: Undefined offset: 1 in E:\xampp\htdocs\qrcode\tab.php on line 11
Notice: Undefined offset: 2 in E:\xampp\htdocs\qrcode\tab.php on line 12
Notice: Undefined offset: 3 in E:\xampp\htdocs\qrcode\tab.php on line 13
Code:
$file = "t_rac.txt";// Your Temp Uploaded file
$handle = fopen($file, "r"); // Make all conditions to avoid errors
$read = file_get_contents($file); //read
$lines = explode("\n", $read);//get
$i= 0;//initialize
$o=1;
foreach($lines as $key => $value){
$cols[$i] = explode("\t", $value);
$list=($cols[$o++][1]);
$list.=($cols[$o++][6]);
$list.=($cols[$o++][7]);
$i++;
}
echo $list;
Updated code off of data this works for ya.
I skip the 1st line, and make sure the column is not out of bounds before adding item.
$lines = explode("\n", $read); //get
$i = 0;//initialize
$list = "";
foreach($lines as $value)
{
if($i != 0)
{
$cols[$i] = explode("\t", $value);
if(isset($cols[$i][1]))
$list.=($cols[$i][1]);
if(isset($cols[$i][6]))
$list.=($cols[$i][6]);
if(isset($cols[$i][7]))
$list.=($cols[$i][7]);
$list.= "<br />";
}
$i++;
}
echo $list;
Your array keys are totally wrong:
$list=($cols[$o++][1]);
^^^^
That changes $o every time you use it. So on your first iteration of the loop, you're doing:
$list=($cols[1][1]);
$list.=($cols[2][6]);
$list.=($cols[3][7]);
Note that $o is now 4... On your second iteration you're doing
$list=($cols[4][1]);
$list.=($cols[5][6]);
$list.=($cols[6][7]);
and $o is 7 at the end of the iteration, etc... If you have 100 rows to process, $o will end up being 301, and you do NOT have that many columns in your $cols array.
May I suggest using the fgetcsv PHP function to parse character delimited files. There is no need to use explode.
Edit - I just read your other comment and understand now what you mean. Updated.
Edit - So you need to get columns 1, 6 and 7 from the same row then. Also added check for first row.
function getMeTheString($file) {
$str = "";
if (($handle = fopen($file, "r")) === FALSE) return;
$line = 0;
while (($cols = fgetcsv($handle, 1000, "\t")) !== FALSE) {
if ($line > 0) { // Ignore 1st line
$str .= $cols[1];
$str .= $cols[6];
$str .= $cols[7];
}
$line++;
}
return $str;
}
echo getMeTheString("t_rac.txt");
Thank you so much everyone! Here is the working code! Works like a charm #Demodave Thank you!!
<?php
$file = "t_rac.txt";// Your Temp Uploaded file
$handle = fopen($file, "r"); // Make all conditions to avoid errors
$read = file_get_contents($file); //read
$lines = explode("\n", $read); //get
$i = 0;//initialize
$list = "";
foreach($lines as $value)
{
if($i != 0)
{
$cols[$i] = explode("\t", $value);
if(isset($cols[$i][2]))
$list.=($cols[$i][2]);
$list.= "--";
if(isset($cols[$i][3]))
$list.=($cols[$i][3]);
$list.= "--";
if(isset($cols[$i][4]))
$list.=($cols[$i][4]);
$list.= "--";
if(isset($cols[$i][6]))
$list.=($cols[$i][6]);
$list.= "--";
if(isset($cols[$i][7]))
$list.=($cols[$i][7]);
$list.= "--";
if(isset($cols[$i][23]))
$list.=($cols[$i][23]);
$list.= "<br />";
}
$i++;
}
echo $list;
?>