Jump to content


Photo

Data not displaying properly in table


  • Please log in to reply
9 replies to this topic

#1 jadeg

jadeg

    Advanced Member

  • Members
  • PipPipPip
  • 35 posts

Posted 05 May 2014 - 07:57 AM

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==8) {  // 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);                                   



#2 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,861 posts
  • LocationCanada

Posted 05 May 2014 - 09:17 AM

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.


The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#3 jadeg

jadeg

    Advanced Member

  • Members
  • PipPipPip
  • 35 posts

Posted 05 May 2014 - 09:23 AM

Here is a sample file. I'll look through the code, I have done this severally. https://www.dropbox....SG Data.xlsx?m=



#4 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,861 posts
  • LocationCanada

Posted 05 May 2014 - 10:28 AM

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>

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#5 ginerjm

ginerjm

    Advanced Member

  • Members
  • PipPipPip
  • 1,769 posts
  • LocationVoorheesville NY

Posted 05 May 2014 - 10:47 AM

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.


JG

PS - If you're posting here you should be using:
        error_reporting(E_ALL | E_NOTICE);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!


#6 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,861 posts
  • LocationCanada

Posted 05 May 2014 - 11:40 AM

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.


The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#7 mac_gyver

mac_gyver

    Advanced Member

  • Administrators
  • 2,568 posts

Posted 05 May 2014 - 11:47 AM

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, 05 May 2014 - 11:49 AM.

multi-purpose programming fool. well written source-code should be self-documenting. well written code should be self-troubleshooting. 


#8 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,861 posts
  • LocationCanada

Posted 05 May 2014 - 12:11 PM

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.


The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#9 ginerjm

ginerjm

    Advanced Member

  • Members
  • PipPipPip
  • 1,769 posts
  • LocationVoorheesville NY

Posted 05 May 2014 - 12:32 PM

Or maybe the OP could modify his own code now that someone debugged it for him?


JG

PS - If you're posting here you should be using:
        error_reporting(E_ALL | E_NOTICE);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!


#10 jadeg

jadeg

    Advanced Member

  • Members
  • PipPipPip
  • 35 posts

Posted 05 May 2014 - 01:30 PM

Thank you you very much for all the help. Will implement the suggested chnages






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com