0

i have a problem with my 2nd array to display it in the table.. can anyone help me out..

<?php
    $logdate = "20140918";
    $fileloc = $logdate."TPL.log";

    if (file_exists($fileloc)) {
        $result = array();
        $file = explode("\n", file_get_contents($fileloc));
        $rowFile = count($file);

?>
            <table cellpadding="5" cellspacing="0" width="100%" border="1">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Transaction ID</th>
                        <th>X</th>
                    </tr>
                </thead>
                <tbody>

<?php
        $x=1;
        foreach ( $file as $content ) {         
            $result[] = array_filter(array_map("trim", explode(";", $content)));            
?>
                    <tr>
                        <td><?=$x?></td>
                        <td><?=$result[$x][0]?></td>
                        <td><?=$result[$x][9]?></td>
                    </tr>

<?php
                $x++;
        }
?>
                </tbody>
            </table>
<?php
    } else {
        echo "File x exists";
    }
?> 

actually i want to insert the record into database.. but i want it to view in the table first. how i want to view the explode result in column..

achimet
  • 75
  • 6
  • You need to add more information. Give us a small sample of what the data looks like in the log file. What's the current output? What's the expected output? By the way, take a look at [file()](http://php.net/manual/en/function.file.php). I think it might be shorter to use than `$file = explode("\n", file_get_contents($fileloc));` – Mikey Dec 03 '14 at 04:45
  • tq bro @Mikey.. its solved.. – achimet Dec 03 '14 at 05:47

2 Answers2

1

Your code has major problems. For once, you never close the first if. Or, indexes in the array start with 0, not 1, so you do not need $x. And I also do not understand what <?=$x?> has to be or what you mean with this. Try this:

<?php
    $logdate = "20140918";
    $fileloc = $logdate."TPL.log";

    if (file_exists($fileloc)) {
        $result = array();
        $file = explode("\n", file_get_contents($fileloc));
        $rowFile = count($file);

            $output = '<table cellpadding="5" cellspacing="0" width="100%" border="1">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Transaction ID</th>
                        <th>X</th>
                    </tr>
                </thead>
                <tbody>';

        foreach ( $file as $key => $content ) {         
            $result[] = array_filter(array_map("trim", explode(";", $content)));

            $output .= '<tr>
                        <td>'.($key+1).'</td>
                        <td>'.$result[$key][0].'</td>
                        <td>'.$result[$key][9].'</td>
                    </tr>';
        }

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

        } else {
            echo "File x exists";
        }
    }

echo $output;
?>

If you spend more time maintaining your own code, that would probably be a good thing.

Mikey
  • 6,728
  • 4
  • 22
  • 45
Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
-1

$x=1; only works for for loops.

I believe that next($file); will do what you want.

  • which part do i need to change it? $x=1 actually i want to view increment numbers in first column of the table – achimet Dec 03 '14 at 04:30
  • @achimet Then in that case, you'll want to change your `foreach` loop into a `for` loop. It'll look something like this `for($x=1; $x –  Dec 03 '14 at 04:32
  • i tried your solution too.. its work.. tq @Mister Dood – achimet Dec 03 '14 at 05:46