Viewing source code files from the server
which support rendering for: Movies
Movies.php :
this PHP server side code creates the page
<?php error_reporting(E_ALL); ini_set('display_errors', 'On'); class MyDB extends SQLite3 { function __construct() { $this->open('movieDB.sqlite'); } } $sql3 = new MyDB(); $error = 'no error message.'; $database = $sql3; if(isset($_POST['title'])) { //print_r ($_POST); print ('<br/>'); //$title = sqlite_escape_string($_POST['title']); // should have worked, but I don't have SQLite2 working $title = trim($database->escapeString($_POST['title'])); $director = trim($database->escapeString($_POST['director'])); $year = trim($database->escapeString($_POST['year'])); $genres = trim($database->escapeString($_POST['genres'])); $user = trim($database->escapeString($_SERVER['REMOTE_ADDR'])); } // add movie if posted if(isset($_POST['add_movie'])) { if (!$title || !$year || !$genres || !$director) { echo ("<br/><b><big style='color: red;'>Movie addition was rejected!</big></b> provide something for every field (eg: '-')<br/>"); } else { $sql = "SELECT count(*) FROM Movies WHERE Title='$title' and Year='$year'"; $cnt = $database->querySingle($sql); if ($cnt==0) { $sql = 'INSERT INTO Movies (Title, Director, YEAR, Genres, User) ' . "VALUES ('$title', '$director', '$year', '$genres', '$user');"; $sql = strip_tags($sql); // ---- Prevent code injection into the DB & later going to the browser if(!$database->exec($sql)) { die($error); } } else { echo ("<i style='color: blue;'><b><big>That Movie is already in this database.</big></b></i><br/>"); } } } else if(isset($_POST['remove_movie'])) { if (!$title || !$year) { echo ("<br/><b><big style='color: red;'>Title and Year are required to Remove an entry.</big></b><br/>"); } else { $sql = "SELECT count(*) FROM Movies WHERE Title='$title' and Year='$year'"; $cnt = $database->querySingle($sql); if ($cnt==0) { echo ("<i style='color: blue;'><b><big>Such an entry was NOT Found in this database.</big></b></i> (Note: case sensitive)<br/>"); } else { $sql = "SELECT * FROM Movies WHERE Title='$title' and Year='$year'"; $entry = $database->querySingle($sql, true); if ($entry['User']!=$user && substr($entry['User'],0,4)!='192.') { // must be the same user or local ip echo ("<br/><b><big style='color: red;'>Removal Rejected! Due to 'User' miss-match.</big></b>"); } else { echo ("<i style='color: blue;'><b><big>Removed that Movie from this database.</big></b></i><br/>"); $sql = "DELETE FROM Movies WHERE Title='$title' and Year='$year'"; if(!$database->exec($sql)) { die($error);} } } } } // --------------- Fetch the table DATA $table_name = "Movies"; //echo "<br/> ------- Counting entries in the Movies table<br/>"; //$cnt = $database->querySingle("SELECT count(*) FROM Movies"); //echo ("There were $cnt records found.<br/>"); $table_data = array(); $query = "SELECT * FROM Movies"; if($result = $database->query($query)) { //var_dump($result->fetchArray()); while($row = $result->fetchArray()) { // form data for the Table aka Grid $table_data[] = $row; } } else { die($error); } //print_r($table_data); //========================================================================= $sql =''; $title = "Movie List"; // ============================ build the web page $page_title = 'SQLite3 Example'; include('std_page.inc.php'); head($title,$page_title); // generate the site wide std top section of the page echo "<div id='outer_div'>"; // ----------------------- given we have a table then build all the sections regarding it if ( !empty($table_name) ) { // --------------- form the HTML for the Table section ?> <h1 class="sectionLabel" title="<?php echo $sql; ?>" style="display:inline-block;"><?php echo $title; ?></h1> <?php if(count($table_data)){ $first_row=true; ?> <div style="margin-left:30px; margin-right:30px;"> <table id="dataTable" width="800" cellpadding="0" cellspacing="0" border="1" align="center"> <?php // ---------- Output the Table contents $col_names = array(); //$colOpts = "<option value='' ></option>"; //while($result = mysql_fetch_array($query)){ foreach ($table_data as $result) { if ($first_row) { // ---- build the table header with Column Nmaes $first_row = FALSE; echo "<thead><tr>"; foreach ($result as $col_name=>$data) { if (is_string($col_name)) { echo "<th>$col_name</th>"; $col_names[] = "'" . $col_name . "'"; //$colOpts .= "<option value='$col_name' >$col_name</option>"; // setup col options picker } } echo "</tr></thead><tbody>"; } echo "<tr>"; // ---- fill in the columns with data foreach ($result as $col_name=>$data) { if (is_string($col_name)) { if ($col_name===NULL) echo "<td>NULL</td>"; else echo "<td>$data</td>"; } else { if ($col_name===NULL) echo "<td>NULL</td>"; } } echo "</tr>"; } ?> </tbody> </table> </div> <br/> <br/> <hr/> <!---------------------------------------------------------------------------------------------> <small><font color='black'>This form allows you to add your favority movies to this SQLite DataBase.</font></small><br/> <div id='submit_feedback' class='feedback'></div> <br/> <font style='font-size: large;'> <form id="movie_form" name="movie_form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <span>Title</span><span style='margin-left:140px;'>Year</span><span style='margin-left:25px; margin-right:95px;'>Director</span>Genres<br/> <input type="text" name="title" id="title" size='30'> <input type="text" name="year" id="year" size='4'> <input type="text" name="director" id="director" > <input type="text" name="genres" id="genres" size='24'> <!-- input type="button" value="Add this Movie" onClick="addMovie()" --> <input type="submit" name="add_movie" style="font-size : 13px;" value="Add this Movie" > <input type="submit" name="remove_movie" style="font-size : 11px;" value="Remove this Movie" > </form> <input type="button" class="button" name="rest_api" style="font-size : 14px;" value="Suggestions:" onClick="restIt();" /> (based on above partial title) <div id='rest_results' class='feedback2'></div> </font> <script type="text/javascript"> $(document).ready(function() { $('#dataTable').dataTable( { "sScrollY": "180px", "bPaginate": false, "bScrollCollapse": true } ); } ); function restIt(){ // using JS Ajax to get info from REST service //$(".feedback2").css("visibility",'hidden'); $(".feedback2").html("<br/>fetching ... <img src='/images/spinner.gif' />"); var title = encodeURIComponent($("#title").val()); // without the encode the spaces get lost //alert("title="+title); // I could have used a server side .PHP to do it all (fetch, error handling & formating), // $("#rest_results").load("search_movies_ajax.php?title="+title); // .load will not work X-domain // $(".feedback2").css("visibility",''); // but doing it here with JS Ajax so that it is visible in the browser // Send Request var http = new XMLHttpRequest(); http.open("GET", "http://www.omdbapi.com/?apikey=66c2fe7d&s=" + title, false); http.send(null); // Response to JSON var omdbDataJSON = http.responseText; var returnObj = eval("(" + omdbDataJSON + ")"); var mlist; if (returnObj.Error) { mlist = "<div style='margin-left: 50px; color:red;'><b>" + returnObj.Error + "</b></div>"; } else { movies = returnObj.Search; // movies is global // Returns Movie Title mlist = ' Select from below: <table style="margin-left: 50px;">'; for (var i in movies) { if (movies[i].Type='movie') { //alert(movies[i].Title); var m = movies[i]; mlist += "<tr style=\"color: darkblue;\" onclick='setMovie(" + i + ");'>" + "<td >" + m.Title + "</td><td>" + m.Year + "</td></tr>"; } } mlist += '</table>'; } $("#rest_results").html(mlist); } function setMovie(i){ // Setup clicked on Movie into the Add movie form //alert("Set Movie to :" + movies[i].Title); // Fetch complete movie data from the REST service var http = new XMLHttpRequest(); http.open("GET", "http://www.omdbapi.com/?apikey=66c2fe7d&t=" + movies[i].Title, false); http.send(null); var movie = eval("(" + http.responseText + ")"); // get Response JSON Object $("#title").val(movies[i].Title); $("#director").val(movie.Director); $("#year").val(movies[i].Year); $("#genres").val(movie.Genre); } </script> <!--------------------------------------------------------------------> <?php } } echo ("</div>"); // end of the outter div $bottom = file_get_contents("../../inc/bottom.ssi"); $bottom = str_replace('</span>', '</span> | <a href="viewSrc.php?src=Movies"> <b> VIEW SERVER SIDE CODE </b> </a> ', $bottom); echo $bottom; ?>
MovieGrid.tpl.html :
Template file fo rforming the Movie grid
<div id=menu>{menu}</div> <table> <tr> <td> <div id="sidebar" class="sidebar">{sidebar}</div></td> <td> <div id="main-content"> {main-content} </div></td> </tr> <tr><td><div>{extras}</div></td> </tr> </table> <div id='diag'></div> <script type="text/javascript"> function loadingNotice() { $('#dataTable').html("<h3 style='text-align:center;'>Loading...     <img src='images/spinner.gif' /></h3>"); } </script>
std_page.inc.php :
some common page pieces/parts
<?php function head($title="Default", $page_title="missing page_title param") { $site_meta = file_get_contents('../../inc/site_meta.ssi'); $html = <<<HTML <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="en"> <head> <title>$title</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="author" content="Ron Miller" /> $site_meta <!-- style type="text/css" media="screen"> @import "/css/styles.css"; </style --> <!-- script type="text/javascript" src="/js/jquery.js"></script --> <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" /> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> <!--script type="text/javascript" src="/js/common.js"></script --> <script type="text/javascript" src="http://www.datatables.net/release-datatables/media/js/jquery.dataTables.js"></script> </head> <body bgcolor="#E8E8E8"> <div xheigth="80px" style="background:#10C010;"> <div style="display:inline-block;"> <center><h2 class="header" style="margin-left:60px;"> $page_title <font size=2></font></h2></center></div> </div> <div style="float:right; color:#444444;" onclick="alert('Yet To Be Done.');"><small>Hello, {user_name} | (logout)</a></small> </div> HTML; if (isset($_SESSION['user_name'])) { $html = str_replace("{user_name}",$_SESSION['user_name'],$html); } echo $html; } function foot() { echo <<<HTML <br/> <hr/> <font size="-1"> <p class="copyright footer" style="text-align:center;">© 2011 DR.Miller. All Rights Reserved. Click here for trademark and copyright information. </p> </font> <div id="debug" class="debug"> </div> </body> </html> HTML; } ?>
bottom.ssi :
provides a site standard set of links at the bottom of the page
<div style="position:fixed; bottom:0; right:30px;"> <font color=Black> <small> <p> | <a href='/'>Site Home</a> | <span class='tag' style="color:blue; cursor: pointer;" onclick='takeMessage();'><u>msg: Site Administrator</u></span> | <strong> <font color='#008080'> RonMiller </strong></font> ©1999-2021 </small> </font> <br/> </div> <div id='message_dialog' title="Message for our site administrator" style='display: none;'> <br/> From: <input type="text" size="32" name="from" id="from" ><br/> <span style="color: #444444"><small><small>Include an email addr if a response is desired.</small></small></span><br/> <textarea rows='4' style='width: 420px;' name="msgtext" id="msgtext" ></textarea> <br/><br/> <input type="button" name="saveMessage" value="Post the Message" onClick="saveMessage();" /> </div> <script type="text/javascript"> function takeMessage(){ //$('#message_dialog').css('display','inline'); $("#message_dialog").dialog({ height: 360, width: 520, modal: true }); } function saveMessage(){ var from = $('#from').val(); var msgtext = $('#msgtext').val(); //alert('Got ' + from +' message :\n' + msgtext); $.post("/post_message.php", {from:from,msg:msgtext}, function(result){ alert(result); }, "text"); $("#message_dialog").dialog( "close" ); } </script> </body> </html>