Guest User

Untitled

a guest
Apr 11th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.57 KB | None | 0 0
  1. <?php
  2. $hostname='server67.0hosting.me';
  3. $username='u8462856_test';
  4. $password='koloiko555';
  5. $database='u8462856_test';
  6.  
  7. $con=mysqli_connect($hostname,$username,$password,$database) or die(mysqli_error());
  8. mysql_query("SET NAMES 'utf8");
  9. mysql_query("SET CHARACTER SET 'utf8'");
  10. ?>
  11.  
  12. <!DOCTYPE html>
  13. <html>
  14. <head>
  15. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  16. <title>A fancy add,edit,delete ajax paginated mysql table</title>
  17. <link rel="stylesheet" type="text/css" href="http://triass.ru/view/style.css" />
  18. <script src="http://triass.ru/view/jquery.js"></script>
  19. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
  20. <script type="text/javascript" src="pagination.js"></script>
  21. <link rel="stylesheet" type="text/css" href="http://triass.ru/view/css/normalize.css" />
  22. <link rel="stylesheet" type="text/css" href="http://triass.ru/view/css/component.css" />
  23. <link rel="stylesheet" type="text/css" href="http://triass.ru/view/css/content.css" />
  24. <script src="http://triass.ru/view/js/modernizr.custom.js"></script>
  25.  
  26. </head>
  27.  
  28. <body>
  29. <div id="formDiv">
  30. <form id="formSearch" >
  31. Search by Name <input type="text" id="fieldSearch" name="search_text" >
  32. <input type="submit" value="Search">
  33. </form>
  34. <div id="divLoading"></div>
  35. <div id="selectDiv">
  36. <small>
  37. <select id="pageRecord">
  38. <option value="1">1</option>
  39. <option value="2">2</option>
  40. <option selected value="3">3</option>
  41. <option value="4">4</option>
  42. </select><i> Record per Page</i>
  43. </small>
  44. </div>
  45. </div>
  46.  
  47. <div id="divPageData"></div>
  48. </body>
  49. </html>
  50.  
  51. <?php
  52.  
  53.  
  54. /*
  55. Developed by Reneesh T.K
  56. You can use it with out any worries...It is free for you..It will display the out put like:
  57. First | Previous | 3 | 4 | 5 | 6 | 7| 8 | 9 | 10 | Next | Last
  58. Page : 7 Of 10 . Total Records Found: 20
  59. */
  60. class Pagination_class{
  61. var $result;
  62. var $anchors;
  63. var $total;
  64. function Pagination_class($qry,$starting,$recpage)
  65. {
  66. include('connect.php');
  67. $rst = mysqli_query($con,$qry) or die(mysqli_error());
  68. $numrows = mysqli_num_rows($rst);
  69. $qry .= " limit $starting, $recpage";
  70. $this->result = mysqli_query($con,$qry) or die(mysqli_error());
  71. $next = $starting+$recpage;
  72. $var = ((intval($numrows/$recpage))-1)*$recpage;
  73. $page_showing = intval($starting/$recpage)+1;
  74. $total_page = ceil($numrows/$recpage);
  75.  
  76. if($numrows % $recpage != 0){
  77. $last = ((intval($numrows/$recpage)))*$recpage;
  78. }else{
  79. $last = ((intval($numrows/$recpage))-1)*$recpage;
  80. }
  81. $previous = $starting-$recpage;
  82. $anc = "<ul id='pagination-flickr'>";
  83. if($previous < 0){
  84. $anc .= "<li class='previous-off'>First</li>";
  85. $anc .= "<li class='previous-off'>Previous</li>";
  86. }else{
  87. $anc .= "<li class='next'><a href='javascript:pagination(0);'>First </a></li>";
  88. $anc .= "<li class='next'><a href='javascript:pagination($previous);'>Previous </a></li>";
  89. }
  90.  
  91. ################If you dont want the numbers just comment this block###############
  92. $norepeat = 4;//no of pages showing in the left and right side of the current page in the anchors
  93. $j = 1;
  94. $anch = "";
  95. for($i=$page_showing; $i>1; $i--){
  96. $fpreviousPage = $i-1;
  97. $page = ceil($fpreviousPage*$recpage)-$recpage;
  98. $anch = "<li><a href='javascript:pagination($page);'>$fpreviousPage </a></li>".$anch;
  99. if($j == $norepeat) break;
  100. $j++;
  101. }
  102. $anc .= $anch;
  103. $anc .= "<li class='active'>".$page_showing."</li>";
  104. $j = 1;
  105. for($i=$page_showing; $i<$total_page; $i++){
  106. $fnextPage = $i+1;
  107. $page = ceil($fnextPage*$recpage)-$recpage;
  108. $anc .= "<li><a href='javascript:pagination($page);'>$fnextPage</a></li>";
  109. if($j==$norepeat) break;
  110. $j++;
  111. }
  112. ############################################################
  113. if($next >= $numrows){
  114. $anc .= "<li class='previous-off'>Next</li>";
  115. $anc .= "<li class='previous-off'>Last</li>";
  116. }else{
  117. $anc .= "<li class='next'><a href='javascript:pagination($next);'>Next </a></li>";
  118. $anc .= "<li class='next'><a href='javascript:pagination($last);'>Last</a></li>";
  119. }
  120. $anc .= "</ul>";
  121. $this->anchors = $anc;
  122.  
  123. $this->total = "Page : $page_showing <i> Of </i> $total_page . Total Records Found: $numrows";
  124. }
  125. }
  126. ?>
  127.  
  128. <?php
  129. include('connect.php');
  130. mysql_query("SET NAMES 'utf8");
  131. mysql_query("SET CHARACTER SET 'utf8'");
  132. if(isset($_POST['action']) && $_POST['action']=="add") //menangani aksi penambahan data pelanggan
  133. {
  134.  
  135. $name=$_POST['add_name'];
  136. $address=$_POST['add_address'];
  137. $exam_no=$_POST['add_exam_no']; $pattern="/^[A-Z]{2}d{4}b/";
  138. if(($name=="")||($address == "")||($exam_no == "")){
  139. echo '{"status":"1"}';
  140. exit;
  141. }
  142. else if( !preg_match($pattern,$exam_no)){
  143. echo '{"status":"2"}';
  144. exit;
  145. }else{
  146.  
  147. $test=mysqli_query($con,"INSERT INTO students(name,address,exam_no) VALUES('$name','$address','$exam_no')") or die ("data gagal ditambahakan!");
  148.  
  149. echo '{"status":"3"}';
  150. exit;
  151. }
  152. }
  153.  
  154. elseif(isset($_POST['action'])&& $_POST['action']=="update") //menangani aksi perubahan data pelanggan
  155. {
  156.  
  157.  
  158. $id=$_POST['edit_id'];
  159. $name=$_POST['edit_name'];
  160. $address=$_POST['edit_address'];
  161. $exam_no=$_POST['edit_exam_no'];
  162.  
  163. $test = mysqli_query($con,"UPDATE students SET name='$name',address='$address',exam_no='$exam_no' WHERE id='$id'") or die ("data gagal di-update!");
  164. echo '{"status":"3"}';
  165. exit;
  166. }
  167. elseif(isset($_POST['action']) && $_POST['action']=="delete") //menangani aksi penghapusan data pelanggan
  168. {
  169.  
  170. $id = $_POST['delete_id'];
  171. $test = mysqli_query($con,"delete from students where id='$id'");
  172. if(mysqli_affected_rows($con) == 1){ //jika jumlah baris data yang dikenai operasi delete == 1
  173. echo '{"status":"1"}';
  174. }else{
  175. echo '{"status":"0"}';
  176. }
  177. exit;
  178. }
  179. ?>
  180.  
  181. <script>
  182. function pagination(page){
  183. var search = $("input#fieldSearch").val();
  184. var record = $("select#pageRecord").val();
  185. if (search!==""){
  186. dataString = 'starting='+page+'&name='+search+'&perpage='+ record+'&random='+Math.random();
  187. }
  188.  
  189. else{
  190. dataString = 'starting='+page+'&perpage='+record+'&random='+Math.random();
  191. }
  192.  
  193. $.ajax({
  194. url:"tablepage.php",
  195. data: dataString,
  196. type:"GET",
  197. success:function(data)
  198. {
  199. $('#divPageData').html(data);
  200. }
  201. });
  202. }
  203.  
  204. function loadData(){
  205. var dataString;
  206. var search = $("input#fieldSearch").val();
  207. var record = $("select#pageRecord").val();
  208. dataString = 'name='+ search + '&perpage=' + record;
  209.  
  210. $.ajax({
  211. url: "tablepage.php",
  212. type: "GET",
  213. data: dataString,
  214. success:function(data)
  215. {
  216. $('#divPageData').html(data);
  217. }
  218. });
  219. }
  220.  
  221.  
  222.  
  223. $('#students tr:even:not(#nav):not(#total)').addClass('even');
  224. $('#students tr:odd:not(#nav):not(#total)').addClass('odd')
  225. $("form#form1").submit(function(){
  226. var vId = $("input#edit_id").val();
  227. var vName = $("input#edit_name").val();
  228. var vAddress = $("input#edit_address").val();
  229. var vExam = $("input#edit_exam_no").val();
  230. var myRegExp=/^[A-Z]{2}d{4}b/;
  231.  
  232.  
  233. if ((vName=="")||(vAddress == "")||(vExam == "")){
  234. alert("Please complete the missing field(s)");
  235. $("input#edit_name").focus();
  236. return false;
  237. }
  238. else if( !myRegExp.test(vExam)){
  239. alert ('Invalid Format for Exam No.');
  240. $("input#edit_exam_no").focus();
  241. return false;
  242. }
  243. else{
  244. $.ajax({
  245. url: "process_data.php",
  246. type:$(this).attr("method"),
  247. data:$(this).serialize(),
  248. dataType: 'json',
  249. success:function(response){
  250. if(response.status == 3) // return nilai dari hasil proses
  251. {
  252. alert("Data Successfully Updated");
  253.  
  254. $(".morph-content").hide(2000);
  255.  
  256. loadData();
  257. }
  258. else if(response.status==1)
  259. {
  260. alert("Please complete the missing field(s)");
  261. $("input#add_name").focus();
  262. }
  263. else if(response.status==2)
  264. {
  265. alert("Invalid Format for Exam No.");
  266. $("input#add_exam_no").focus();
  267. }
  268.  
  269. else
  270. {
  271. alert("Data update unsccessful");
  272. }
  273. }
  274. });
  275. return false;
  276. }
  277. return false;
  278. });
  279.  
  280.  
  281. $("form#form2").submit(function(){
  282.  
  283. $.ajax({
  284. url: "process_data.php",
  285. type:$(this).attr("method"),
  286. data:$(this).serialize(),
  287. dataType: 'json',
  288. success:function(response){
  289. if(response.status == 1) // return nilai dari hasil proses
  290. {
  291. alert("Data Successfully Delected");
  292.  
  293. $(".morph-content").hide(2000);
  294.  
  295. loadData();
  296. }
  297. else
  298. {
  299. alert("Data Failed to Delete");
  300. }
  301. }
  302. });
  303. return false;
  304.  
  305.  
  306. });
  307. </script>
  308. <script src="http://triass.ru/view/js/modernizr.custom.js"></script>
  309. <script src="http://triass.ru/view/js/classie.js"></script>
  310. <script src="http://triass.ru/view/js/uiMorphingButton_fixed.js"></script>
  311. <script src="http://triass.ru/view/js/buttonMorp.js"></script>
  312.  
  313.  
  314. <?php
  315. error_reporting(E_ALL^E_NOTICE);
  316. include('pagination_class.php');
  317. include('connect.php');
  318.  
  319. if (isset($_GET['name']) and !empty($_GET['name'])){
  320. $name = $_GET['name'];
  321. $sql = "select * from students where name like '%$name%'";
  322. }
  323. else{
  324. $sql = "select * from students order by id";
  325. }
  326.  
  327. if(isset($_GET['starting'])){ //starting page
  328. $starting=$_GET['starting'];
  329. }else{
  330. $starting=0;
  331. }
  332.  
  333. $recpage=$_GET['perpage'];
  334.  
  335. $obj = new pagination_class($sql,$starting,$recpage);
  336. $result = $obj->result;
  337. ?>
  338. <div id="page_contents">
  339.  
  340. <div id="addDiv">
  341. <div class="morph-button morph-button-modal morph-button-modal-2 morph-button-fixed">
  342. <button type="button">ADD</button>
  343. <div class="morph-content">
  344. <div>
  345. <div class="content-style-form content-style-form-1">
  346. <span class="icon icon-close">Close the dialog</span>
  347. <h2>Add Data</h2>
  348. <form id="form1" method="post" >
  349. <p><label>Name</label><input type="text" id="add_name" name="add_name" /></p>
  350. <p><label>Address</label><input type="text" id="add_address" name="add_address" /> </p>
  351. <p><label>Exam No. (format: AA000)</label><input type="text" id="add_exam_no" name="add_exam_no" /></p>
  352. <p><input type="submit" value="Add" /></p>
  353. <input type="hidden" id="action" name="action" value="add" />
  354. </form>
  355. </div>
  356. </div>
  357. </div>
  358. </div>
  359. </div>
  360.  
  361. <div id="student_wrap">
  362. <table id="students" width="100%" >
  363. <tr><th>Sl No</th><th>Student Name</th><th>Address</th><th>Exam No.</th><th style="padding-left:19px;">Action</th>
  364. </tr>
  365. <?php if(mysqli_num_rows($result)!=0){
  366. $counter = $starting + 1;
  367. while($data = mysqli_fetch_array($result)) {?>
  368. <tbody><tr>
  369. <td><?php echo $data['id']; ?></td>
  370. <td><?php echo $data['name']; ?></td>
  371. <td><?php echo $data['address']; ?></td>
  372. <td><?php echo $data['exam_no']; ?></td>
  373. <td>
  374. <div class="morph-button morph-button-modal morph-button-modal-2 morph-button-fixed">
  375. <button type="button">Edit</button>
  376. <div class="morph-content">
  377. <div>
  378. <div class="content-style-form content-style-form-1">
  379. <span class="icon icon-close">Close the dialog</span>
  380. <h2>Update Data</h2>
  381. <form id="form1" method="post" >
  382. <p><label>ID</label><input type="text" id="edit_id" name="edit_id" value="<?php echo $data['id']; ?>" readonly /></p>
  383. <p><label>Name</label><input type="text" id="edit_name" name="edit_name" value="<?php echo $data['name']; ?>" /></p>
  384. <p><label>Address</label><input type="text" id="edit_address" name="edit_address" value="<?php echo $data['address']; ?>" /></p>
  385. <p><label>Exam No. (format: AA000)</label><input type="text" id="edit_exam_no" name="edit_exam_no" value="<?php echo $data['exam_no']; ?>" /></p>
  386. <p><input type="submit" value="Update" /></p>
  387. <input type="hidden" name="action" value="update" />
  388. </form>
  389. </div>
  390. </div>
  391. </div>
  392. </div>
  393. <div class="morph-button morph-button-modal morph-button-modal-2 morph-button-fixed">
  394. <button type="button">Delete</button>
  395. <div class="morph-content">
  396. <div>
  397. <div class="content-style-form content-style-form-1">
  398. <span class="icon icon-close">Close the dialog</span>
  399. <h2>Delete Data</h2>
  400. <p ><h2 style="margin:10px 10px;">Do you really want to delete from "demo" where SL No="<?php echo $data['id']; ?>" </h2></p>
  401. <form id="form2" method="post" >
  402. <p><input type="hidden" name="delete_id" value="<?php echo $data['id']; ?>" /></p>
  403. <p><input type="submit" value="Delete" /></p>
  404. <input type="hidden" name="action" value="delete" />
  405. </form>
  406. </div>
  407. </div>
  408. </div>
  409. </div>
  410. </td>
  411.  
  412.  
  413. </tr></tbody> <?php } ?>
  414.  
  415. <tfoot><tr id="nav"><td colspan="5"><div><?php echo $obj->anchors; ?></div></td>
  416. </tr>
  417. <tr id="total"><td colspan="5"><?php echo $obj->total; ?></td>
  418. </tr>
  419. <?php } else{ ?>
  420. <tr><td align="center" colspan="5">No Data Found</td>
  421. </tr></tfoot>
  422. <?php } ?>
  423.  
  424. </table>
  425. </div>
  426. </div>
  427.  
  428. CREATE TABLE IF NOT EXISTS `students` (
  429. `id` int(11) NOT NULL AUTO_INCREMENT,
  430. `name` varchar(25) NOT NULL,
  431. `address` varchar(50) NOT NULL,
  432. `exam_no` varchar(15) NOT NULL,
  433. PRIMARY KEY (`id`)
  434. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=17 ;
  435.  
  436. $.ajax({
  437. url: "tablepage.php",
  438. type: "GET",
  439. data: dataString,
  440. scriptCharset: "utf-8",
  441. success: .....
Advertisement
Add Comment
Please, Sign In to add comment