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.';
}
}