I have a .txt file which contains objects on each line.Meaning the first line is "baby",second line "toddler",next line "dog" and the line after is "cat".
I want to be able to add objects into my txt file using php but i want to prevent duplicates.Somehow my codes only work for "dog".When i try to add "dog" it will say this object already exist but when i try "cat" / "baby" /"toddler" it still adds even though it is already in the list.
CODES
$check = false;
if(isset($_POST['add'])){
if($_POST['addLbl'] == ''){
echo ' Please enter a label';
$check = true;
}
else{
$data = "
".$_POST['addLbl'];
$file_lines = file('lbls/predefined_classes.txt');
foreach($file_lines as $line){
if($_POST['addLbl'] === $line){
$check = true;
}
}
if($check === false){
$ret = file_put_contents('lbls/predefined_classes.txt',$data,FILE_APPEND | LOCK_EX);
if($ret === false){
echo ' Unable to add.An error occurred.';
}
else{
echo ' Sucessfully added!';
$_POST['addLbl'] = '';
}
}else{
echo 'The label '.$_POST['addLbl'].' already exists.';
}
}
because you add list manualy and have white space in the end of each line add trim() function solve problem
<?php
$check = false;
if(isset($_POST['add'])){
if($_POST['addLbl'] == ''){
echo ' Please enter a label';
$check = true;
}
else{
$data = "
".$_POST['addLbl'];
$file_lines = file('lbls/predefined_classes.txt');
foreach($file_lines as $line){
if($_POST['addLbl'] === trim($line)){ // trim() added here
$check = true;
}
}
if($check === false){
$ret = file_put_contents('lbls/predefined_classes.txt',$data,FILE_APPEND | LOCK_EX);
if($ret === false){
echo ' Unable to add.An error occurred.';
}
else{
echo ' Sucessfully added!';
$_POST['addLbl'] = '';
}
}else{
echo 'The label '.$_POST['addLbl'].' already exists.';
}
}
}
?>
output test for cat :
The label cat already exists.
try the following, little shorter with same logic. No need for $check
among other improvements.
if(isset($_POST['add'])){
if($_POST['addLbl'] == ''){
echo ' Please enter a label';
}
else{
$data = "
".$_POST['addLbl'];
$file_str = file_get_contents('lbls/predefined_classes.txt');
if (!strrpos ( $file_str , $data)) { // not found
$ret = file_put_contents('lbls/predefined_classes.txt',$data,FILE_APPEND | LOCK_EX);
if($ret === false){
echo ' Unable to add.An error occurred.';
}
else{
echo ' Sucessfully added!';
$_POST['addLbl'] = '';
}
}else{
echo 'The label '.$_POST['addLbl'].' already exists.';
}
}
}
You get lines with the new line character. Read the file and split it into lines.
$newline = PHP_EOL; // your editor might use another new line character
if(in_array($_POST['addLbl'], explode($newline, file_get_contents('test.txt'))))
// echo error message
else
// append to file
The entire task can be done in a few lines. Btw. check also for unset $_POST
entries.
if(isset($_POST['add']))
{
if(!isset($_POST['addLbl']) || $_POST['addLbl'] === '')
echo ' Please enter a label';
elseif(in_array($_POST['addLbl'], explode(PHP_EOL, file_get_contents('lbls/predefined_classes.txt'))))
echo "The label {$_POST['addLbl']} already exists.";
elseif(false === file_put_contents('lbls/predefined_classes.txt',PHP_EOL . $_POST['addLbl'], FILE_APPEND | LOCK_EX))
echo ' Unable to add.An error occurred.';
else
{
echo ' Sucessfully added!';
$_POST['addLbl'] = '';
}
}
The $file_lines array looks like as below
array
(
[0] =>
[1] => baby
[2] => toddler
[3] => cat
[4] => dog
)
You must remove next line escape sequence(i.e, ) from $line and then compare with $_POST['addLbl']