I have a php page that takes 2.8 seconds to render. The page contains a script that reads a txt file via file()
line by line (~5000 lines) via a foreach
loop. This works perfectly and allows me to wrap each line in a <div>
. This all looks something like this.
$text_file = 'path/to/my/text/file.txt';
$lines = file($text_file);
$output = '';
foreach($lines as $line_num => $line){
$output .= '<div id="'.$line_num.'" class="line">'.htmlspecialchars($line).'</div>'."
";
}
echo $output;
The problem is I need to query if a line number is in the database and if it is give it an extra class highlight
. This is what is making the page render so slowly. Each line (~5000) is querying the database within the loop. This looks something like this.
foreach($lines as $line_num => $line){
// codeigniter is being used here
$line_exists = $this->line_model->lookup_line($line_num);
// $line_exists checks the database if the $line_num exists it will return true / false
if($line_exists){
$lines_output .= '<div id="'.$line_num.'" class="line highlight">'.htmlspecialchars($line).'</div>'."
";
}else{
$lines_output .= '<div id="'.$line_num.'" class="line">'.htmlspecialchars($line).'</div>'."
";
}
}
My question is: Is there a more efficient / faster way to do this?