view php 2nd array in table

105 Views Asked by At

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..

2

There are 2 best solutions below

2
Kevin Kopf On BEST ANSWER

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.

3
AudioBubble On

$x=1; only works for for loops.

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