2
\$\begingroup\$

How can I code this in a better way, in one file? Output must be more than one (if there is more that one status_update with the given word "online or performancedegradation"), like:

#online
System is operating at peak performance.

#directonly
Traffic is bypassing Smart CDN system, and going directly to websites.
<?php
$filename = "export_file_system_status_output";   
$status_update = "online";  
$handle = fopen($filename, "r");  
$file = fread($handle,filesize($filename));  
if (preg_match('#' . $status_update . '#', $file, $match)) {  
echo "<div id=badge_subpage_system>
                                        <h5>#online</h5>
                                        <p>System is operating at peak performance.</p>
                                    </div>";
}  
?>

<?php
$filename = "export_file_system_status_output";   
$status_update = "performancedegradation";  
$handle = fopen($filename, "r");  
$file = fread($handle,filesize($filename));  
if (preg_match('#' . $status_update . '#', $file, $match)) {  
echo "<div id=badge_subpage_system>
                                        <h5>#performancedegradation</h5>
                                        <p>Performance is slower than normal.</p>
                                    </div>";
}
?>
                                    
<?php
$filename = "export_file_system_status_output";   
$status_update = "directonly";  
$handle = fopen($filename, "r");  
$file = fread($handle,filesize($filename));  
if (preg_match('#' . $status_update . '#', $file, $match)) {  
echo "<div id=badge_subpage_system>
                                        <h5>#directonly</h5>
                                        <p>Traffic is bypassing Smart CDN system, and going directly to websites.</p>
                                     </div>";
}
?> 

It must sort by first status_update found:

  1. online

    #online
    
    System is operating at peak performance.
    
  2. then performancedegradation

    #directonly
    
    Traffic is bypassing Smart CDN system, and going directly to websites.
    
\$\endgroup\$
2
  • \$\begingroup\$ use conditions, if there are more then use a switch case. File name ends with type of file. Your code is not having full file name \$\endgroup\$
    – user1162286
    Jan 31, 2012 at 15:04
  • \$\begingroup\$ It must sort by first status_update found: Online => then #online System is operating at peak performance. performancedegradation => #directonly Traffic is bypassing Smart CDN system, and going directly to websites. \$\endgroup\$ Jan 31, 2012 at 15:05

2 Answers 2

1
\$\begingroup\$

I'll give you a hint. Try using a foreach loop: http://www.php.net/manual/en/control-structures.foreach.php

You can store all your files and associated information in an associative array, such as:

$files = array(
  0 => array(
    'filename' => "export_file_system_status_output";   
    'status_update' => "online";
  ),
  1 => array(
    'filename' => "another_file";   
    'status_update' => "offline";
  )
);

You should really try this on your own :) If you don't get it, I'll give you a more complete answer :)

On a side note, your HTML is not valid. Use classes instead of ids when possible, and use quotes:

<div class="badge_subpage_system">
  <h5>#performancedegradation</h5>
  <p>Performance is slower than normal.</p>
</div>

Hope this helps :)

EDIT:

Here's what your final code will look like, with the array I posted above:

foreach($files as $file)
{
  $handle = fopen($file['filename'], "r");
  $file = fread($handle,filesize($file['filename']));
  if (preg_match('#' . $file['status_update'] . '#', $file, $match))
  {
    echo '<div id="badge_subpage_system"><h5>#performancedegradation</h5><p>Performance is slower than normal.</p></div>';
  }
}
\$\endgroup\$
4
  • \$\begingroup\$ like this: $arr = array("0", "1", "2"); reset($arr); while (list(, $value) = each($arr)) { echo "Value: $value<br />\n"; } foreach ($arr as $value) { echo "Value: $value<br />\n"; } \$\endgroup\$ Jan 31, 2012 at 16:05
  • \$\begingroup\$ The second line seems right, yes :) The first line is just messy :s Always think about the fact that if others read your code, they should be able to understand easily what your code does. \$\endgroup\$ Jan 31, 2012 at 16:22
  • \$\begingroup\$ Can't get it fixed :( Can you help me on this one? \$\endgroup\$ Feb 1, 2012 at 15:59
  • \$\begingroup\$ Sorry for being so late :s anyway, in case you still need help, I've updated my answer aboce :) \$\endgroup\$ Feb 5, 2012 at 3:45
1
\$\begingroup\$
  1. Write D.R.Y. code. Do not ask php to open the same unchanged file 3 times, just open it once.
  2. Be precise about where in your text you expect to find your specified keywords.
  3. Use a lookup to generate the regex and to quickly determine the desired text to present for each occurrence.
  4. HTML markup must have unique id attributes in the document. Since your output is repeating, you should use a class instead of id. Your attribute values should be quoted as well.
  5. Though I typically advise against using single-use variables, in this case it serves to separate and describe specific pieces of data / operations.

Code: (Demo)

$lookup = [
    '#online' => 'System is operating at peak performance.',
    '#performancedegradation' => 'Performance is slower than normal.',
    '#directonly' => 'Traffic is bypassing Smart CDN system, and going directly to websites.'
];
$regex = '/^(?:' . implode('|', array_keys($lookup)) . ')(?=\s)/m';

$contents = file_get_contents("export_file_system_status_output.txt");
$matches = preg_match_all($regex, $contents, $m) ? $m[0] : [];
foreach ($matches as $keyword) {
    printf(
        '<div class="badge_subpage_system"><h5>%s</h5><p>%s</p></div>',
        $keyword,
        $lookup[$keyword]
   );
}

Output:

<div class="badge_subpage_system"><h5>#online</h5><p>System is operating at peak performance.</p></div>
<div class="badge_subpage_system"><h5>#directonly</h5><p>Traffic is bypassing Smart CDN system, and going directly to websites.</p></div>
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.