Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. <?php
  2. $DB_host = "localhost";
  3. $DB_user = "root";
  4. $DB_pass = "";
  5. $DB_name = "dbpdo";
  6.  
  7. try
  8. {
  9. $DB_con = new PDO('mysql:host{$DB_host}; dbname={$DB_name}',$DB_user,$DB_pass);
  10. $DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  11. }
  12. catch (PDOException $e)
  13. {
  14. echo $e->getMessage();
  15. }
  16.  
  17. include_once 'class.crud.php';
  18. $crud = new crud($DB_con); ?>
  19.  
  20. <?php
  21.  
  22. class crud
  23. {
  24. private $db;
  25.  
  26. function __construct($DB_con)
  27. {
  28. $this->db = $DB_con;
  29. }
  30.  
  31. /*para criar novo*/
  32. public function create($xnome,$xsobrenome,$xemail,$xtelefone)
  33. {
  34. try
  35. {
  36. $stmt = $this->db->prepare("INSERT INTO tbl_usuarios(nome,sobrenome,email,telefone) VALUES(:xnome, :xsobrenome, :xemail, :xtelefone)");
  37.  
  38. $stmt->bindparam(":xnome",$xnome);
  39. $stmt->bindparam(":xsobrenome",$xsobrenome);
  40. $stmt->bindparam(":xemail",$xemail);
  41. $stmt->bindparam(":xtelefone",$xtelefone);
  42. $stmt->execute();
  43. return true;
  44. }
  45. catch(PDOException $e)
  46. {
  47. echo $e->getMessage();
  48. return false;
  49. }
  50. }
  51.  
  52. /*para buscar por id*/
  53. public function getID($id)
  54. {
  55. $stmt=$this->db->prepare("SELECT * FROM tbl_usuarios WHERE id=:id");
  56. $stmt->execute(array(":id"=>$id));
  57. $editRow=$stmt->fetch(PDO::FETCH_ASSOC);
  58. return $editRow;
  59. }
  60.  
  61. /*para actualizar*/
  62. public function update($xid, $xnome, $xsobrenome, $xemail, $xtelefone)
  63. {
  64. try
  65. {
  66. $stmt=$this->db->prepare("UPDATE tbl_usuarios SET nome=:ynome, sobrenome=:ysobrenome, email=:yemail, telefone=:ytelefone WHERE id=:yid ");
  67. $stmt->bindparam(":ynome",xnome);
  68. $stmt->bindparam(":ysobrenome",xsobrenome);
  69. $stmt->bindparam(":yemail",xemail);
  70. $stmt->bindparam(":ytelefone",xtelefone);
  71. $stmt->bindparam(":yid",xid);
  72. $stmt->execute();
  73.  
  74. return true;
  75. }
  76. catch(PDOException $e)
  77. {
  78. echo $e->getMessage();
  79. return false;
  80. }
  81.  
  82. }
  83.  
  84. /*para eliminar*/
  85. public function delete($id)
  86. {
  87. $stmt=$this->db->prepare("DELETE tbl_usuarios WHERE id=:id");
  88. $stmt->bindparam(":id",$id);
  89. $stmt->execute();
  90. return true;
  91. }
  92.  
  93. /*para paginar*/
  94. public function dataview($query)
  95. {
  96. try
  97. {
  98. $stmt=$this->db->prepare($query);
  99. $stmt->execute();
  100.  
  101. if($stmt->rowCount()>0)
  102. {
  103. while($row=$stmt->fetch(PDO::FETCH_ASSOC))
  104. {
  105. ?>
  106. <tr>
  107. <td><?php print($row['id']); ?></td>
  108. <td><?php print($row['nome']); ?></td>
  109. <td><?php print($row['sobrenome']); ?></td>
  110. <td><?php print($row['email']); ?></td>
  111. <td><?php print($row['telefone']); ?></td>
  112. <td align="center">
  113. <a href="edit-data.php?edit_id=<?php print($row['id']); ?>"><i class="glyphicon glyphicon-edit"></i></a>
  114. </td>
  115. <td align="center">
  116. <a href="delete.php?delete_id=<?php print($row['id']); ?>"><i class="glyphicon glyphicon-remove-circle"></i></a>
  117. </td>
  118. </tr>
  119. <?php
  120. }
  121. }
  122. else
  123. {
  124. ?>
  125. <tr>
  126. <td>Não existem dados para visualizar!</td>
  127. </tr>
  128. <?php
  129. }
  130. }
  131. catch(PDOException $e)
  132. {
  133. echo $e->getMessage();
  134. }
  135. }
  136.  
  137.  
  138. public function paging($query, $record_per_page)
  139. {
  140. $starting_position=0;
  141. if(isset($_GET["page_no"]))
  142. {
  143. $starting_position=($_GET["page_no"]-1)*$record_per_page;
  144. }
  145. $query2=$query."limit $starting_position,$record_per_page";
  146. return $query2;
  147. }
  148.  
  149. public function paginglink($query, $record_per_page)
  150. {
  151. $self=$_SERVER['PHP_SELF'];
  152.  
  153. $stmt=$this->db->prepare($query);
  154. $stmt->execute();
  155. $total_no_of_records=$stmt->rowCount();
  156. echo $query;
  157.  
  158. if($total_no_of_records > 0)
  159. {
  160. ?><ul class="pagination"><?php
  161. $total_no_of_records=ceil($total_no_of_records/$record_per_page);
  162. $current_page=1;
  163.  
  164. if(isset($_GET["page_no"]))
  165. {
  166. $current_page=$_GET["page_no"];
  167. }
  168.  
  169. if($current_page!=1)
  170. {
  171. $previous=$current_page-1;
  172. echo "<li><a href='".$self."?page_no=1'>Primeira</a></li>";
  173. echo "<li><a href='".$self."?page_no=".$previous."'>Anterior</a></li>";
  174. }
  175.  
  176. for($i=1;$i<=$total_no_of_records;$i++)
  177. {
  178. if($i==$current_page)
  179. {
  180. echo "<li><a href='".self."?page_no=".$i."'style='color:red;'>".$i."</a></li>";
  181. }
  182. else
  183. {
  184. echo "<li><a href='".$self."?page_no=".$i."'>".$i."</a></li>";
  185. }
  186. }
  187.  
  188. if($current_page!=$total_no_of_records)
  189. {
  190. $next=$current_page+1;
  191. echo "<li><a href='".$self."?page_no=".$next."'>Próximo</a></li>";
  192. echo "<li><a href='".$self."?page_no=".$total_no_of_records."'>Última</a></li>";
  193. }
  194. ?></ul><?php
  195. }
  196. }
  197. } ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement