· 9 years ago · Jan 20, 2017, 11:58 AM
1CREATE TABLE IF NOT EXISTS `php_interview_questions` (
2`id` int(8) NOT NULL,
3 `question` text NOT NULL,
4 `answer` text NOT NULL,
5 `row_order` int(8) NOT NULL
6)
7
8INSERT INTO `php_interview_questions` (`id`, `question`, `answer`, `row_order`) VALUES
9(1, 'PHP array functions example', 'is_array(), in_array(), array_keys(),array_values()', 3),
10(2, 'How to redirect using PHP', 'Using header() function', 4),
11(3, 'Differentiate PHP size() and count():', 'Same. But count() is preferable.', 1),
12(4, 'What is PHP?', 'A server side scripting language.', 0),
13(5, 'What is php.ini?', 'PHP configuration file.', 2);
14
15<?php
16class DBController {
17 private $host = "localhost";
18 private $user = "root";
19 private $password = "";
20 private $database = "blog_examples";
21
22 function __construct() {
23 $conn = $this->connectDB();
24 if(!empty($conn)) {
25 $this->selectDB($conn);
26 }
27 }
28
29 function connectDB() {
30 $conn = mysql_connect($this->host,$this->user,$this->password);
31 return $conn;
32 }
33
34 function selectDB($conn) {
35 mysql_select_db($this->database,$conn);
36 }
37
38 function runQuery($query) {
39 $result = mysql_query($query);
40 while($row=mysql_fetch_assoc($result)) {
41 $resultset[] = $row;
42 }
43 if(!empty($resultset))
44 return $resultset;
45 }
46
47 function numRows($query) {
48 $result = mysql_query($query);
49 $rowcount = mysql_num_rows($result);
50 return $rowcount;
51 }
52}
53?>
54
55<?php
56require_once("dbcontroller.php");
57$db_handle = new DBController();
58$sql = "SELECT * from php_interview_questions";
59$faq = $db_handle->runQuery($sql);
60?>
61<html>
62 <head>
63 <title>PHP MySQL Inline Editing using jQuery Ajax</title>
64 <style>
65 body{width:610px;}
66 .current-row{background-color:#B24926;color:#FFF;}
67 .current-col{background-color:#1b1b1b;color:#FFF;}
68 .tbl-qa{width: 100%;font-size:0.9em;background-color: #f5f5f5;}
69 .tbl-qa th.table-header {padding: 5px;text-align: left;padding:10px;}
70 .tbl-qa .table-row td {padding:10px;background-color: #FDFDFD;}
71 </style>
72 <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
73 <script>
74 function showEdit(editableObj) {
75 $(editableObj).css("background","#FFF");
76 }
77
78 function saveToDatabase(editableObj,column,id) {
79 $(editableObj).css("background","#FFF url(loaderIcon.gif) no-repeat right");
80 $.ajax({
81 url: "saveedit.php",
82 type: "POST",
83 data:'column='+column+'&editval='+editableObj.innerHTML+'&id='+id,
84 success: function(data){
85 $(editableObj).css("background","#FDFDFD");
86 }
87 });
88 }
89 </script>
90 </head>
91 <body>
92 <table class="tbl-qa">
93 <thead>
94 <tr>
95 <th class="table-header" width="10%">Q.No.</th>
96 <th class="table-header">Question</th>
97 <th class="table-header">Answer</th>
98 </tr>
99 </thead>
100 <tbody>
101 <?php
102 foreach($faq as $k=>$v) {
103 ?>
104 <tr class="table-row">
105 <td><?php echo $k+1; ?></td>
106 <td contenteditable="true" onBlur="saveToDatabase(this,'question','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["question"]; ?></td>
107 <td contenteditable="true" onBlur="saveToDatabase(this,'answer','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["answer"]; ?></td>
108 </tr>
109 <?php
110 }
111 ?>
112 </tbody>
113 </table>
114 </body>
115</html>
116
117<?php
118require_once("dbcontroller.php");
119$db_handle = new DBController();
120$result = mysql_query("UPDATE php_interview_questions set " . $_POST["column"] . " = '".$_POST["editval"]."' WHERE id=".$_POST["id"]);
121?>