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=\"http://www.w3.org/1999/xhtml\">
							<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

Recommended Answers

$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

Jump to Post

All 4 Replies

Member Avatar
$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

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 learning and sharing knowledge.