dimanche 26 avril 2015

How to tabulate data present in a sqlite database with PHP

Hi I wrote down the following PHP script to show data written in sqlite database in form a HTML table. When I open the page I see nothing. I have saved it as index.php under my servers root directory.

<html>
    <head>
        <title>Switch and Status Report</title>
    </head>
    <body>
        <h1>Control Panel</h1>
        <table border="1" style="width:100%">
            <?php
                $dbhandle = sqlite_open('db/pj.db', 0666, $error);
                if (!$dbhandle) die ($error);
                $query = "SELECT * FROM state";
                $result = sqlite_query($dbhandle, $query);
                if (!$result) die("Cannot execute query.");
                $array = sqlite_fetch_all($result, SQLITE_NUM);
                echo($array);
                for ($x = 0; $x <= 6; $x++) {
                        $curarr = $array[$x];
                        $curpin = curarr[0];
                        $curname = curarr[1];
                        $curstate = curarr[2];
                        $curdep = curarr[3];
                        $textst = ""; 
                        $linkst = "";
                        $textdep = ""; 
                        $linkdep = "";
                        if($curstate==(0)){                             
                                $textst = "Turn On";
                                $linkst = "statechange.php";                        
                        }
                        elseif($curstate==(1)){
                                $textst = "Turn Off";
                                $linkst = "statechange.php";
                        }
                        if($curdep==(0)){                               
                                $textst = "Make Motion Dependent";
                                $linkst = "motionchange.php";                       
                        }
                        elseif($curstate==(1)){
                                $textst = "Make Motion Independent";
                                $linkst = "statechange.php";
                        }
                        echo("<tr>");
                        echo("<td>$curpin</td>");
                        echo("<td>$curname</td>");                      
                        echo("<td><a href='$linkst'>$textst</a></td>");             
                        echo("<td><a href='$linkdep'>$textdep</a></td>");                       
                        echo("</tr>");  
                        sqlite_close($dbhandle);                    
                } 
            ?> 
        </table>
    </body>
</html> 

Please Troubleshoot and help me. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire