在PHP中拆分表

时间:2014-03-19 12:08:41

标签: php

我有这个脚本。它查找一个文件并读取所有行并将其很好地放在一个表中..我现在需要做的是获取所有表数据并将其拆分为两个表..

例如。如果它们是100行..那么我将在一个表中获得50个数据而在另一个表中获得50个数据...

  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>";
}
?>

1 个答案:

答案 0 :(得分:0)

使用foreach而不是for和$ count来计算是否需要回显表标记。 $ count仅在插入行时递增。

<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>';
}