Hello,

I hope somebody can help me out here ...
I'm having a problem with error code while searching a word in a flat file.
My flat file

|~|20091105213652|~|05 november 2011 - 21:36|~|John|~|18 cm|~|text 1|~|text2|~|text3|~|text4|~|text5|~|text6|~|No|~|John.jpg|~|xxx.xxxs.xxx.xxx|~|extra|~|
|~|20091105213929|~|05 november 2011 - 21:39|~|Jeff|~|45 cm|~|text 1|~|text2|~|text3|~|text4|~|text5|~|text6|~|No|~|Jeff.jpg|~|xxx.xxxs.xxx.xxx|~|extra|~|
|~|20091105220916|~|05 november 2011 - 22:09|~|Paul|~|15 cm|~|text 1|~|text2|~|text3|~|text4|~|text5|~|text6|~|No|~|Paul.jpg|~|xxx.xxxs.xxx.xxx|~|extra|~|
|~|20091105221959|~|05 november 2011 - 22:19|~|Anna|~|30 cm|~|text 1|~|text2|~|text3|~|text4|~|text5|~|text6|~|No|~|Anna.jpg|~|xxx.xxxs.xxx.xxx|~|extra|~|

My php code

<?php
	$database = "path/to/database/text.txt";
	
	$record = file($database);
	rsort($record);
	$amount = count($record);
	$file = 'John.jpg';
	$newname = 'Johnny.jpg';
	$nomrec = 0;
         for ($i=0; $i<$amount; $i++) {
	  	$nomrec++;
		$no++;
		$recno = $nomrec-1;
		    if (isset($record[$recno])) {
				$row = explode("|~|",$record[$recno]);
					if ($row[12] == $file) {
						$row[12] = $newname; 
						$id = $row[1];
						
						echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
							<html xmlns=\"">
							<meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />
							<body >
							<div align=\"left\"> 
							<form method=\"post\" name=\"rename\" id=\"rename\" action=\"link_to_action_file.php\" enctype=\"multipart/form-data\">
								<input type=\"text\" name=\"vfilename\" size=\"30\" value=\"$row[12]\" /><br />
								<input type=\"text\" name=\"vname\" size=\"30\" value=\"$row[3]\" /><br />
								<input type=\"text\" name=\"vline1\" size=\"30\" value=\"$row[4]\" /><br />
								<textarea name=\"vline2\" cols=\"40\" rows=\"3\">".htmlspecialchars_decode(htmlentities($row[5]))."</textarea><br />
								<textarea name=\"vline3\" cols=\"40\" rows=\"3\">".htmlspecialchars_decode(htmlentities($row[6]))."</textarea><br />
								<textarea name=\"vline4\" cols=\"40\" rows=\"3\">".htmlspecialchars_decode(htmlentities($row[7]))."</textarea><br />
								<textarea name=\"vline5\" cols=\"40\" rows=\"3\">".htmlspecialchars_decode(htmlentities($row[8]))."</textarea><br />
								<input type=\"text\" name=\"vline6\" value=\"$row[9]\"><br />
								<input type=\"text\" name=\"vline7\" size=\"30\" value=\"$row[10]\" /><br />
								<input type=\"text\" name=\"vline8\" value=\"$row[11]\"><br />
								<input type=\"text\" name=\"vextra\" size=\"30\" maxlength=\"100\" value=\"$row[14]\"><br />";
								
					} else echo "doesn't exist";
			} 
	} 
?>

My output shows good, it only shows the row with the name John.jpg but I also get 3 times "doesn't exist" too which is normal because of the loop.
How can I solve this problem so that I get only "doesn't exist" if the file really doesn't exists.

Thanks in advance.
Kind regards,
Coderunner

Dani AI

Generated

A simple reason for the repeated "doesn't exist" messages is that the non-match message is printed inside the loop for every record that does not match. 's approach (collect matches first) fixes that; below is a more robust, production-ready pattern that (1) normalizes the split tokens so field indexes are predictable, (2) only emits the "doesn't exist" message once, and (3) shows safer checks for missing fields.

<?php
$database = 'path/to/database/text.txt';
$targetFile = 'John.jpg';
$lines = file($database, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$lines = array_reverse($lines); // keep newest-first without reindex problems

function parse_record($line) {
    // split on the delimiter, drop empty tokens and reindex
    $tokens = preg_split('/\|~\|/', $line);
    $tokens = array_values(array_filter($tokens, 'strlen'));
    return [
        'id'        => $tokens[0]  ?? '',
        'date'      => $tokens[1]  ?? '',
        'name'      => $tokens[2]  ?? '',
        'size'      => $tokens[3]  ?? '',
        'line1'     => $tokens[4]  ?? '',
        'line2'     => $tokens[5]  ?? '',
        'line3'     => $tokens[6]  ?? '',
        'line4'     => $tokens[7]  ?? '',
        'line5'     => $tokens[8]  ?? '',
        'line6'     => $tokens[9]  ?? '',
        'line7'     => $tokens[10] ?? '',
        'filename'  => $tokens[11] ?? '',
        'ip'        => $tokens[12] ?? '',
        'extra'     => $tokens[13] ?? '',
    ];
}

$found = false;
foreach ($lines as $line) {
    $rec = parse_record($line);
    if ($rec['filename'] !== '' && trim($rec['filename']) === $targetFile) {
        $found = true;
        // render the form using $rec[...] or collect for later output
        // break; // uncomment to stop after the first match
    }
}

if (! $found) {
    echo "doesn't exist";
}
?>

Notes and troubleshooting

  • Use FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES to prevent newline artefacts.
  • Use strict comparison (===) and trim() to avoid whitespace mismatches.
  • Confirm token index (here filename is mapped to index 11 after dropping empty tokens); use var_dump(parse_record($line)) if unsure.
  • For very large files, stream with fopen/fgets to avoid memory spikes.
  • If records will grow or need updates, consider CSV or a small database for safer, faster operations.

Recommended Answers

All 4 Replies

Member Avatar for Member #120589
$record = file($database);
foreach($record as $r){
  $pos = strstr($r,"John.jpg");
  if($pos !== false){
    $hold[] = $r;
  }
}
if(isset($hold))print_r($hold);

This should find them

Hello ardav,

Thank you very much for your reply.
I'll check it out and I'll keep you informed.

Regards,
Coderunner

Hello ardav,

Your given code solved my problem.

Thank you very much for your reply!

Regards,
Coderunner

Member Avatar for Member #120589

OK mark the thread as solved (click link above the edit box).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.