0

everyone I have the following file and I want to show lines that match on if condition and pass the other.

I have this TXT file:

Doc. number|Date|Price|Description|Name
100|11/11/2015|99|Test 1|Alex
101|11/11/2015|120|Test 2
102|11/11/2015|100|Test 3|John
102|11/11/2015|140||
103|11/11/2015|110|Test 4|

And this is my PHP code:

$file_handle = fopen("file.txt", "rb");
$i = 0;
echo "<table border='1'>";
echo "<tr><th>Doc. number</th><th>Date</th><th>Price</th><th>Description</th><th>Name</th></tr>";
while (!feof($file_handle)) {
    $line_of_text = fgets($file_handle);
    $parts = explode('|', $line_of_text);

    if($i > 1) { // Pass the first line
        echo "<tr>";
            echo "<td>" . $parts[0] . "</td>"; // Doc. number
            echo "<td>" . $parts[1] . "</td>"; // Date
            echo "<td>" . $parts[2] . "</td>"; // Price
            echo "<td>" . $parts[3] . "</td>"; // Description
            echo "<td>" . $parts[4] . "</td>"; // Name

        echo "</tr>";
    }
    $i++;
}

fclose($file_handle);
echo "</table>"

How I can check if there are no "Description" and/or "Name" in table and pass this line. I want to show(get) only line that match on if condition.

I will be very grateful if someone have idea. Thanks in advance.

1
  • You know that $parts[3] and $parts[4] have always Description and Name respectively. So in every iteration Check them. If they exist in array execute the if-body, neither pass the iteration. Commented Feb 5, 2016 at 11:50

5 Answers 5

1

As simple as

$file_handle = fopen("file.txt", "rb");
$i = 0;
echo "<table border='1'>";
echo "<tr><th>Doc. number</th><th>Date</th><th>Price</th><th>Description</th><th>Name</th></tr>";
while (!feof($file_handle)) {
    $line_of_text = fgets($file_handle);
    $parts = explode('|', $line_of_text);

    if($i > 1 && !empty($parts[3]) && !empty($parts[4])) { // Pass the first line and lines without description / name
        echo "<tr>";
            echo "<td>" . $parts[0] . "</td>"; // Doc. number
            echo "<td>" . $parts[1] . "</td>"; // Date
            echo "<td>" . $parts[2] . "</td>"; // Price
            echo "<td>" . $parts[3] . "</td>"; // Description
            echo "<td>" . $parts[4] . "</td>"; // Name

        echo "</tr>";
    }
    $i++;
}

fclose($file_handle);
echo "</table>"
Sign up to request clarification or add additional context in comments.

Comments

1

Only print table row if we have name and description:

if($i > 1 && $parts[3] && $parts[4]) { 

Comments

1

You can put condition before echo statement and if it will be false just skip "echo";

if (count($parts) === 5) {
    $error = 0;
    foreach ($parts as $part) {
       if (empty($part)) error++;
    }

    if($i > 1 && $error === 0) {
            echo "<tr>";
                echo "<td>" . $parts[0] . "</td>"; // Doc. number
                echo "<td>" . $parts[1] . "</td>"; // Date
                echo "<td>" . $parts[2] . "</td>"; // Price
                echo "<td>" . $parts[3] . "</td>"; // Description
                echo "<td>" . $parts[4] . "</td>"; // Name

            echo "</tr>";
        }
}

Comments

1

I've a solution that can help you.
But why I think you need just scape the heading line. so I changed if($i > 1) to be if($i >0)

$file_handle = fopen("file.txt", "rb");
$i = 0;
echo "<table border='1'>";
echo "<tr><th>Doc. number</th><th>Date</th><th>Price</th><th>Description</th><th>Name</th></tr>";
while (!feof($file_handle)) {
    $line_of_text = fgets($file_handle);
    $parts = explode('|', $line_of_text);
    if($i > 0) { // Pass the first line
        if ( (!empty($parts[3])) && (!empty($parts[4])) ){
            echo "<tr>";
                echo "<td>" . $parts[0] . "</td>"; // Doc. number
                echo "<td>" . $parts[1] . "</td>"; // Date
                echo "<td>" . $parts[2] . "</td>"; // Price
                echo "<td>" . $parts[3] . "</td>"; // Description
                echo "<td>" . $parts[4] . "</td>"; // Name
            echo "</tr>";
        }
    }
    $i++;
}
fclose($file_handle);
echo "</table>"

1 Comment

That will get to you: Alex & John Rows.
1

Your file has a CSV structure, pipe delimited.
So parse it as a CSV.

Note the using of array_shift to get the header of the CSV and passing the delimiter parameter to fgetcsv.

Also consider using implode instead explicitly passing each member of the array between td tags.

Here's an example using two functions, one for parsing the CSV and returning the data,
and another one for displaying the data and doing the validation.

function getData($file){

    $rows = array();

    if (($handle = fopen($file, "r")) !== FALSE) {
        while (($data = fgetcsv($handle, null, "|")) !== FALSE) {

            $rows[] = $data;
        }
        fclose($handle);
    }

    return $rows;

}

function displayData($rows){

    $header = array_shift($rows);
    $output = '<table border="1">' . PHP_EOL;
    $output .= '<tr><th>' . implode('</th><th>',$header) . '</th></tr>' . PHP_EOL;

    foreach($rows as $row){

        if (isset($row[3]) and isset($row[4]) and $row[3]!='' and $row[4]!=''){
            $output .= '<tr><td>' . implode('</td><td>',$row) . '</td></tr>' . PHP_EOL;
        }

    }

    $output .= '</table>';
    return $output;

}

$rows = getData("pipe.txt");

print displayData($rows);

This will output the following

<table border="1">
<tr><th>Doc. number</th><th>Date</th><th>Price</th><th>Description</th><th>Name</th></tr>
<tr><td>100</td><td>11/11/2015</td><td>99</td><td>Test 1</td><td>Alex</td></tr>
<tr><td>102</td><td>11/11/2015</td><td>100</td><td>Test 3</td><td>John</td></tr>
</table>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.