Jump to content

Data not displaying properly in table


jadeg

Recommended Posts

I am trying to get a csv file from a server, parse it and display the table. However It is exhibiting some strange behaviours. It displays the content of column 1 where it  should be column 8 and vice versa. How whenI use print_r() is prints the right value. Any Idea why this is so. Below is my code.



​<?php 
session_start(); 
  
$ftp_server="server"; 
$ftp_user_name="user"; 
$ftp_user_pass="password"; 
    $conn_id = ftp_connect($ftp_server) or die ("Couldn't connect to $ftp_server"); 
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
  
    if($login_result) { 
        echo '<h1>Last 10 second reading for Bridgeford T1</h1>'; 
        ftp_pasv($conn_id, true);    
          
          
        if(($handle = fopen('ftp://user:%pass@server/1hdata.csv', "r")) !== false) {  //pointer to begining of file 
            $table4 = $table5 = $table6 ='<table border="1">'; //table tag 
            $row=0; 
            $x=3591;  //rows with data 
            $y=3600; 
              
              
            while (($data = fgetcsv($handle, 0, ",")) !== false) {  //store data in an array 
                $table4Add = $table5Add = $table6Add= false; 
                if($row >=$x && $row <=$y) {   //select range of rows to display values 
                    $table4Add=$table5Add=$table6Add=true; 
                 } 
                  
                if($row == 0) {   //table headings 
                    $table4 .= '<thead><tr>'; 
                    $table5 .= '<thead><tr>'; 
                    $table6 .= '<thead><tr>'; 
                      
                    $table4 .= '<th>TimeStamp</th>';  
                    $table4 .= '<th>Voltage A</th>'; 
                    $table4 .= '<th>Current A</th>'; 
                    $table4 .= '<th>Phase A</th>';                  
                    $table4 .= '</tr></thead><tbody>';  
                
                      
                } 
                    
                else { 
                    if($table4Add) $table4 .= '<tr>'; 
                    
                        for($c = 0; $c <= 10; $c++) { //obtain values form each field 
                            $value = $data[$c];  
                          
                            if($c==0) {                //displaytime                 
                                if($table4Add) $table4 .= '<td><p>'.$value.'</p></td>'; 
                            } 
                              
                      if ($c== {  // display phase a voltage and implement colour code
                      if($value <=216.2 || $value >=253.0) { 
        if ($table4Add) $table4 .='<td><p style="color:red";>'.round($value,2).'</p></td>';                       } 
                       else { 
                           if ($table4Add) $table4 .='<td><p>'.round($value,2).'</p></td>';
                            } 
                      } 
                              
                      if ($c==5) { //display phase a current and implement colour code 
                      if(($value*1250) >= 1500.0) {   
 if ($table4Add) $table4 .='<td><p style="color:red";>'.round(($value*1250),2).'</p></td>';
                      } 
                       else { 
                    if ($table4Add) $table4 .='<td><p>'.round(($value*1250),2).'</p></td>';                       } 
                      } 
                              
                      if ($c==1) { // display phase a pf and implement colour code 
                      if($value <=0.0) { 
       if ($table4Add) $table4 .='<td><p style="color:red";>'.round($value,2).'</p></td>'; 
                        } 
                        else { 
                         if ($table4Add) $table4 .='<td><p>'.round($value,2).'</p></td>'; 
                             } 
                       } 
              } 
                          
                        if($table4Add) $table4 .= '</tr>'; 
                       
                } 
                $row++; 
            } 
  
    $table4 .= '</tbody></table>';  
    
    fclose($handle);   
     
    echo $table4; // display tables 
   }
}
ftp_close($conn_id);                                   

Link to comment
Share on other sites

A few comments first:

 

1. In my experience, these type of issues with tables usually occurs doe to improper opening/closing of the tags

2. Can you provide a sample file that you are using? It is difficult to easily identify the errors in the code and having a sample input and seeing the actual output would help

3. You should really create your output in a way that the source HTML would be readable. From the code you've provided there are no line breaks in the HTML output. I suggest using double quotes in delineating the strings and putting \n at the end of an HTML line so there will be a line break in the output.

Link to comment
Share on other sites

Ok, I've used the test file and have used it with the code you provided to generate output. I'm not seeing anything wrong. But, I will say you went if/else crazy in your script. It is much more complicated than it should be - which is making this harder to solve than it should be.

 

I am seeing some incorrect HTML syntax. Those could cause different presentations based on your browser. Some browsers are more forgiving of HTML errors than others. For example

 

if ($table4Add) $table4 .='<td><p style="color:red";>'.round($value,2).'</p></td>';

The semicolon after the style parameter is outside the quote mark

 

Anyway, here is a complete rewrite that should produce the same output format using much simpler logic. I don't know that it will sove your problem, but I did clean up some of the invalid HTML as well. I didn't see that the code for table5 and table6 was doing anything, so I removed it. If you need it, please explain what it is supposed to do and I can help with that too. One other change I would probably make to this that I didn't do is that I would probably read the entire file and copy it to the local server and then process it from there rather than read it line by line from the FTP location.

 

 

<?php 
 
session_start(); 
 
//Define configuration variables
$ftp_server = "server";
$ftp_user_name = "user";
$ftp_user_pass = "password";
$dataFile = "1hdata.csv";
$startRow = 3592; //First row with data
 
//connect to FTP server
$conn_id = ftp_connect($ftp_server) or die ("Couldn't connect to $ftp_server"); 
if(!ftp_login($conn_id, $ftp_user_name, $ftp_user_pass))
{
    die("Unable to log into FTP server");
}
ftp_pasv($conn_id, true);  
 
//Get data file
$handle = fopen("ftp://user:%pass@server/{$dataFile}", "r");
if($handle===false)
{
    die("Unable to access file {$dataFile}");
}
 
//Process the file
$row = 0;
$table4Body = false;
while (($data = fgetcsv($handle)) !== false)
{
    //Increment row count
    $row++;
 
    //Skip rows before the data we want
    if($row < $startRow) { continue; }
 
    //define values from the data row
    $time          = $data[0];
    $voltage       = round($data[8], 2);
    $voltageStyle  = ($voltage <=216.2 || $voltage >=253.0) ? 'red' : 'black';
    $currentA      = round($data[5] * 1250, 2);
    $currentAStyle = ($currentA >= 1500.0) ? 'red' : 'black';
    $phaseA        = round($data[1], 2);
    $phaseAStyle   = ($phaseA <= 0) ? 'red' : 'black';
 
    //Create the HTML output
    $table4Body .= "    <tr>\n";
    $table4Body .= "        <td>$time</td>\n";
    $table4Body .= "        <td style='color:{$voltageStyle};'>{$voltage}</p></td>\n";
    $table4Body .= "        <td style='color:{$currentAStyle};'>{$currentA}</p></td>\n";
    $table4Body .= "        <td style='color:{$phaseAStyle};'>{$phaseA}</p></td>\n";
    $table4Body .= "</tr>\n";
 
} 
 
//Close file handle and FTP connection
fclose($handle);
ftp_close($conn_id);
 
//Define the output
$table4 = '';
if(!empty($table4Body))
{
    $table4 .= "<table border='1'>\n";
    $table4 .= "<thead>\n";
    $table4 .= "    <tr>\n";
    $table4 .= "        <th>TimeStamp</th>\n";
    $table4 .= "        <th>Voltage A</th>\n";
    $table4 .= "        <th>Current A</th>\n";
    $table4 .= "        <th>Phase A</th>\n";
    $table4 .= "    </tr>\n";
    $table4 .= "</thead>\n";
    $table4 .= "<tbody>\n";
    $table4 .= $table4Body;
    $table4 .= "</tbody>\n";
    $table4 .= "</table>\n";
}
 
?>
<html>
<head></head>
<body>
    <h1>Last 10 second reading for Bridgeford T1</h1>
    <?php echo $table4; ?>
</body>
</html>
Link to comment
Share on other sites

Could not the problems be the existence of <p>...</p> tags inside of table elements?  Even more likely, the lines that include ONLY a </p> tag in a <td> element could be a problem.

 

What is the point in using a p tag inside a td one?  Seems like a lot of needless html, but of course the specs probably say it is much needed and that still confuses me.

Link to comment
Share on other sites

Could not the problems be the existence of <p>...</p> tags inside of table elements?  Even more likely, the lines that include ONLY a </p> tag in a <td> element could be a problem.

 

What is the point in using a p tag inside a td one?  Seems like a lot of needless html, but of course the specs probably say it is much needed and that still confuses me.

 

Yeah, I noticed that too and removed the <p> tags from the updated code I provided. I don't think that would cause a problem, but best to leave out things unless they are needed.

Link to comment
Share on other sites

the main reason the op's result doesn't match the expected is the array index/offsets he used in the code don't match the data.

 

offsets:

0 = time

1 = phase a voltage

2 = phase b voltage

3 = phase c voltage

4 = phase a current

5 = phase b current

6 = phase c current

7 = phase a pf

8 = phase b pf

9 = phase c pf

10 = freq

 

for the values the code claims to be displaying, the offsets used would be 0, 1, 4, and 7

Edited by mac_gyver
Link to comment
Share on other sites

Good catch mac_gyver. With the modified code it should be very simple to fix that type of error. There were so many if/else combinations that it made my head spin. And, now that you identified that, now I'm guessing that tables 5 and 6 were to display similar results for phases B and C. If the OP can confirm that, i can easily modify the code to support that.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.