0

i got this script. it looks into a file and read all the lines and put it nicely in a table.. what i need to do now is to take all the table data and split it into two tables ..

eg. if their is 100 rows.. then instead of one long list, i will get 50 data in one table and the other 50 in the other table...

  enter code here<?php
    if(isset ($_GET['type'])) 
    {
        $otype = $_GET['type'];
    } 
    else 
    {
        $otype = 'm';
    }

    $statusFile = "d:\\CLIENTS\\status.txt";

    $file_handler = fopen($statusFile, "r"); 

    // read the contents 
    $contents = fread($file_handler, filesize($statusFile)); 

    // close the file 
    fclose($file_handler); 

    if (strcasecmp($otype, "m") == 0)
    {
        echo $contents;  
    } 
    else 
    {
        $lines = explode("\n",$contents);
        $frow = explode(",", $lines[0]);

        if (strcmp($frow[1],"1") == 0) 
        {
            echo "Update Status: <b>Complete</b>";
            //to count total lines in txt file
            $statusFile = "d:\\CLIENTS\\status.txt";
            $line = count(file($statusFile)); 
            echo "There are".$line."lines in"; 
        } 
        else 
        {
            echo "Update Status: <b>Incomplete</b>";
        }

        echo "<table border=\"1\">";
        for ($count = 1; $count < sizeof($lines); $count++) 
        {
            $fields = explode(",",$lines[$count]);
            $sz = sizeof($fields);
            if ($sz > 1) 
            {
                $str = "OK";
                echo "<tr>";
                echo "<td>" . $fields[0] . "</td>";
                echo "<td>" . $fields[1] . "</td>";
                //echo "<td>" . $fields[2] . "</td>";

                if (strpos($fields[2],'OK') !== false) 
                {
                    echo "<td><font color='green'>". $fields[2] ."</font></td>";

                    //echo "<td style='background-color: #00FF00;'>". $fields[2] ."</td>"; 
                } 
                else 
                {
                    //echo "<td><font color='red'>". $fields[2] ."</font></td>";
                    echo "<td style='background-color: #FF0000;'>". $fields[2] ."</td>"; 
                }


            echo "</tr>";

        }
    }
        echo "</table>";
}
?>
NoobEditor
  • 15,563
  • 19
  • 81
  • 112

1 Answers1

0

Use a foreach instead of a for and $count to calculate if it is needed to echo a table tag. $count is only incremented when a row was inserted.

<style>
div.container {
    width: 100%; /* Change the width to the desired width */
    padding: 0;
}
div.container table {
    width: 50%; /* Change the width to half of the width of the div */
    margin: 0;
    float: left;
}
</style>
<?php
if(isset ($_GET['type'])) 
{
    $otype = $_GET['type'];
} 
else 
{
    $otype = 'm';
}

$statusFile = "d:\\CLIENTS\\status.txt";

$file_handler = fopen($statusFile, "r"); 

// read the contents 
$contents = fread($file_handler, filesize($statusFile)); 

// close the file 
fclose($file_handler); 

if (strcasecmp($otype, "m") == 0)
{
    echo $contents;  
} 
else 
{
    $lines = explode("\n",$contents);
    $frow = explode(",", $lines[0]);

    if (strcmp($frow[1],"1") == 0) 
    {
        echo "Update Status: <b>Complete</b>";
        //to count total lines in txt file
        $statusFile = "d:\\CLIENTS\\status.txt";
        $line = count(file($statusFile)); 
        echo "There are".$line."lines in"; 
    } 
    else 
    {
        echo "Update Status: <b>Incomplete</b>";
    }

    echo '<div class="conatiner">';
    $count = 0;
    foreach ($lines as $currLine) 
    {

        $fields = explode(",",$currLine);

        $sz = sizeof($fields);
        if ($sz > 1) 
        {
            if ($count == 0) {
                echo "<table border=\"1\">";
            }
            elseif ($count % 50 == 0) {
                echo "</table>";
                echo "<table border=\"1\">";

            }
            $str = "OK";
            echo "<tr>";
            echo "<td>" . $fields[0] . "</td>";
            echo "<td>" . $fields[1] . "</td>";
            //echo "<td>" . $fields[2] . "</td>";

            if (strpos($fields[2],'OK') !== false) 
            {
                 echo "<td><font color='green'>". $fields[2] ."</font></td>";

                 //echo "<td style='background-color: #00FF00;'>". $fields[2] ."</td>"; 
            } 
            else 
            {
                 //echo "<td><font color='red'>". $fields[2] ."</font></td>";
                 echo "<td style='background-color: #FF0000;'>". $fields[2] ."</td>"; 
             }


            echo "</tr>";
            $count++;

        }
    }
    echo "</table>";
    echo '</div>';
}
jazZRo
  • 1,598
  • 1
  • 14
  • 18
  • It's just an example based on a part of your code. But if you replace the for loop with this foreach it should work. Can you tell me what you tried to implement it? What is going wrong? – jazZRo Mar 19 '14 at 14:05
  • I noticed that you skip the first line. Was that intended? I will edit that including a possible conflicting variable `$line`. I don't know what you mean exactly with irregular but does it have something to do with the first line? – jazZRo Mar 19 '14 at 14:19
  • actually i posted whole complete code. and my code fetching rows from text file....and displayed in tabular format on web..but it display like long list..so i want to split data(rows from text file) in 2 tables.. and in table there are 3 columns..so after 50 rows create new table side by side and display another remaning 50 rows – user3392977 Mar 19 '14 at 14:23
  • and it is easy if you post whole code...so there will be less confusion – user3392977 Mar 19 '14 at 14:25
  • I see what went wrong, a stupid mistake by me. The table close tag had to be above the opening tag. I posted the complete code – jazZRo Mar 19 '14 at 14:42
  • code works correct...but tables created in sequence..not side by side.... the separete tables created for every 50 records.... – user3392977 Mar 19 '14 at 14:47
  • code works correct...but tables created in sequence..not side by side.... the separete tables created for every 50 records.... but i want it to display tables side by side... – user3392977 Mar 19 '14 at 14:47
  • That goes beyond your original question. But it can be done with css. You can use the `float: left` property for that. See also [this question](http://stackoverflow.com/questions/8917235/html-two-tables-horizontally-side-by-side). – jazZRo Mar 19 '14 at 14:55
  • Since it answered your posted question, could you mark this answer as correct please? For an answer on how to position the tables I would post a new question since that is something for the CSS experts out there. – jazZRo Mar 19 '14 at 15:34
  • actully i used your code....it gives 90% result correct..i.e. two table created for each 50 records....but the problem is 2nd table is not created to beside 1st table...it is created under 1st table... so could you plz suggest any solution for above problem....so it will be perfect answer – user3392977 Mar 20 '14 at 09:15
  • I added an example of my suggestion to use float: left in a previous comment. Hope that clarifies it a bit. – jazZRo Mar 20 '14 at 09:44
  • Parse error: syntax error, unexpected 'float' (T_STRING), expecting ',' or ';' in C:\wamp\www\trystatus.php on line 54 it throws an error – user3392977 Mar 20 '14 at 09:52
  • i used - echo "";
    – user3392977 Mar 20 '14 at 09:53
  • plz do smthing wid it...i am already sick of this thing....mind stop working...smbdy plz helpppp – user3392977 Mar 20 '14 at 09:53
  • You are missing the \ in `echo "";`. If you use my example you can remove the `style=\"float: left;\"` part.
    – jazZRo Mar 20 '14 at 10:00
  • for example- add password to above code...if entered password is correct thn it display table – user3392977 Mar 20 '14 at 10:13
  • There are many options for that. You can use a web form and check with PHP or use `.htpasswd` if you have access to that. [See also this](http://weavervsworld.com/docs/other/passprotect.html) – jazZRo Mar 20 '14 at 10:17
  • i am not using apache server..i am using wamp server...so plz u suggest some solution for put a password to above php page code... – user3392977 Mar 20 '14 at 10:42
  • WAMP stands for Windows, Apache, Mysql, PHP so it's definitely Apache. I suggest doing some research because that would save you a lot of time. This would be a good way to start: [http://stackoverflow.com/questions/4115719/easy-way-to-password-protect-php-page](http://stackoverflow.com/questions/4115719/easy-way-to-password-protect-php-page) – jazZRo Mar 20 '14 at 11:01