· 9 years ago · Jan 20, 2017, 06:24 PM
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 <script>
75 function showEdit(editableObj) {
76 $(editableObj).css("background","#F0E68C");
77 }
78
79 function saveToDatabase(editableObj,column,id) {
80 $(editableObj).css("background","#FFF url(loaderIcon.gif) no-repeat right");
81 $.ajax({
82 url: "saveedit.php",
83 type: "POST",
84 data:'column='+column+'&editval='+editableObj.innerHTML+'&id='+id,
85 success: function(data){
86 $(editableObj).css("background","#FDFDFD");
87 }
88 });
89 }
90 function checkEnter(e, editableObj, column, id){
91 if (e.keyCode == 13 && e.shiftKey == false) {
92 saveToDatabase(editableObj, column, id);
93 e.preventDefault();
94 }
95 }
96
97 </script>
98 </head>
99 <body>
100 <table class="tbl-qa">
101 <thead>
102 <tr>
103 <th class="table-header" width="10%">Q.No.</th>
104 <th class="table-header">Question</th>
105 <th class="table-header">Answer</th>
106 </tr>
107 </thead>
108 <tbody>
109 <?php
110 foreach($faq as $k=>$v) {
111 ?>
112 <tr class="table-row">
113 <td><?php echo $k+1; ?></td>
114 <td contenteditable="true"
115 onBlur="saveToDatabase(this,'answer2','<?php echo $faq[$k]["id"]; ?>')"
116 onKeyDown="checkEnter(event, this,'answer2','<?php echo $faq[$k]["id"]; ?>')"
117 onClick="showEdit(this);"><?php echo $faq[$k]["question"]; ?></td>
118 <td contenteditable="true" onBlur="saveToDatabase(this,'answer','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["answer"]; ?></td>
119 </tr>
120 <?php
121 }
122 ?>
123 </tbody>
124 </table>
125 </body>
126</html>
127
128<?php
129require_once("dbcontroller.php");
130$db_handle = new DBController();
131$result = mysql_query("UPDATE php_interview_questions set " . $_POST["column"] . " = '".$_POST["editval"]."' WHERE id=".$_POST["id"]);
132?>