0

Overview

I have created a script that attempts to parse an uploaded CSV file, loop through the rows and columns, and out putting them into a semantic HTML table. I have tried to add a condition . if statement that would only display the table rows that have a value in col 6 grater than 0.40 (i am filtering imported CSV's to remove table rows that have bounce rates lower than 40% n the column)

Excuse

I can't see the wood through the trees, so to speak here. I am a junior developer, struggling to actually comprehend debugging in PHP compared to .NET and Visual Studio. PHP is much harder to debug IMO. Anyway no debates I'm a newbie maybe I'm missing things, regardless I'd appreciate some help. And I apologize for any oversights or anything silly, I just need a bit of help pointing out where I went wrong and how I could improve?

It's a fairly simple script I'm sure :(

<?php
//error Reporting
ini_set('display_errors', 1);
error_reporting(E_ERROR);


//parse imported CSV
$csvData = file_get_contents($_FILES['minefile']['tmp_name'], 'r+');
$lines   = explode("\n", $csvData);
$array   = array();
foreach ($lines as $line) {
    $array[] = str_getcsv($line);
} //$lines as $line
//count rows & cols
$colCount = count($array[0]);
$rowCount = count($array);
$showRow = true;

//start table html
echo '<table width="100%">';
//Start looping through csv row (line) by row (line)
for ($row = 0; $row <= $rowCount; $row++) {
    // debug var row loop - echo "<h1>".$col."</h1>";
    // echo '<h1>'.$hideRow ? 'true' : 'false'.'</h1>';

    //if index is 0 it must be the header (in this case)
    if ($row == 0) {
        //output table head tag
        echo '<thead>';
    } 
    else {
        //check bounce rate column (6) for a bounce rate higher than 40%
        if ($showRow == true) {
            //show row
            echo '<tr>';

        } 
        //
        else {
            //hide row
            echo '<tr style="display:none;">';
        }

    }
    for ($col = 0; $col <= $colCount; $col++) {
        if ($row == 0) {
            //
            echo '<th>' . $array[$row][$col] . '</th>';
        } //$row == 0
        else {

            if ($array[$row][$col] >= 0.40 && $col == 5)
            {
                $showRow = true;
            }
            echo '<td>' . $array[$row][$col] . '</td>';

            
        }
        
    } //$col = 0; $col <= $colCount; $col++
    
    if ($row == 0) {
        echo '</thead>';
    } //$row == 0
    else {
        echo '</tr>';
    }

} //$row = 0; $row <= $rowCount; $row++
echo "</table>";
echo "<h1>Debugging</h1>";
echo "<h3>Col Count: " . $colCount . " </h3>";
echo '<h3>Row Count: ' . $rowCount . '</h3>';
?>
Community
  • 1
  • 1
Chris Leah
  • 79
  • 3
  • 14

1 Answers1

1

The issue is likely with the fact that you are showing the start of the row

echo '<tr>';

before you change the value of $showRow.

If you changed a small portion of the code :

for ($row = 0; $row <= $rowCount; $row++) {
    // debug var row loop - echo "<h1>".$col."</h1>";
    // echo '<h1>'.$hideRow ? 'true' : 'false'.'</h1>';

    //if index is 0 it must be the header (in this case)
    if ($row == 0) {
        //output table head tag
        echo '<thead>';
    } 
    else {
        $showRow = ($array[$row][5] >= 0.40);  //ADD THIS LINE

I think that would get you where you want.

DragonYen
  • 958
  • 9
  • 22
  • Hi I am attempting to integrate this..and understand but...I am struggling to see what to remove and where to add your suggestions? – Chris Leah Feb 10 '14 at 17:29
  • I only added the last line. The other lines are yours and there purely for reference (so you can find the right spot to place it). The issue is that you are looking at "showRow" to set the display to NONE, but you don't check the column until after you've already put out the start of the row. I would suspect this is actually causing the "NEXT" row to be hidden when the trip point is encountered. You shouldn't remove anything from your code, just add that one line. – DragonYen Feb 10 '14 at 18:02
  • Did this get you up and running? – DragonYen Feb 11 '14 at 20:26
  • Works like a charm now, thank you very much I shall approve it. Moreover though, I come from Visual Studio background, and like the VS debugging style and simplicity. Do you or can you recommend any good or the best way to debug applications in PHP? As I am still learning. Thank you again regardless Dragon. – Chris Leah Feb 11 '14 at 20:43
  • 1
    No problem. Most of my PHP work (these days) is in Drupal (a CMS). So I have a lot of functions I've created in order to show debug messages and stuff like that. Here's where someone else asked about PHP debugging and a few helpful responses made it in before the topic was closed : http://stackoverflow.com/questions/888/how-do-you-debug-php-scripts – DragonYen Feb 12 '14 at 13:56