valodia

noticias

Mar 11th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. <?php
  2.  
  3. class crudNews
  4. {
  5. private $db;
  6.  
  7. function __construct($DB_con)
  8. {
  9. $this->db = $DB_con;
  10. }
  11.  
  12. public function create($titulo,$conteudo,$data,$img)
  13. {
  14. $data = date('d-m-Y');
  15.  
  16. try
  17. {
  18. $stmt = $this->db->prepare("INSERT INTO tbl_noticias (titulo,conteudo,data,img) VALUES(:titulo, :conteudo, :data, :img)");
  19. $stmt->bindparam(":titulo",$titulo);
  20. $stmt->bindparam(":conteudo",$conteudo);
  21. $stmt->bindparam(":data",$data);
  22. $stmt->bindparam(":img",$img);
  23. $stmt->execute();
  24. return true;
  25. }
  26. catch(PDOException $e)
  27. {
  28. echo $e->getMessage();
  29. return false;
  30. }
  31.  
  32. }
  33.  
  34. public function getID($noticias_id)
  35. {
  36. $stmt = $this->db->prepare("SELECT * FROM tbl_noticias WHERE noticias_id=:noticias_id");
  37. $stmt->execute(array(":noticias_id"=>$noticias_id));
  38. $editRow=$stmt->fetch(PDO::FETCH_ASSOC);
  39. return $editRow;
  40. }
  41.  
  42. public function update($noticias_id,$titulo,$conteudo,$data,$img)
  43. {
  44. try
  45. {
  46. $stmt=$this->db->prepare("UPDATE tbl_noticias SET titulo=:titulo,
  47. conteudo=:conteudo,
  48. data=:data,
  49. img=:img
  50. WHERE noticias_id=:noticias_id ");
  51. $stmt->bindparam(":titulo",$titulo);
  52. $stmt->bindparam(":conteudo",$conteudo);
  53. $stmt->bindparam(":data",$data);
  54. $stmt->bindparam(":img",$img);
  55. $stmt->bindparam(":noticias_id",$noticias_id);
  56. $stmt->execute();
  57.  
  58. return true;
  59. }
  60. catch(PDOException $e)
  61. {
  62. echo $e->getMessage();
  63. return false;
  64. }
  65. }
  66.  
  67. public function delete($noticias_id)
  68. {
  69. $stmt = $this->db->prepare("DELETE FROM tbl_noticias WHERE noticias_id=:noticias_id");
  70. $stmt->bindparam(":noticias_id",$noticias_id);
  71. $stmt->execute();
  72. return true;
  73. }
  74.  
  75. /* paging */
  76.  
  77. public function dataview($query)
  78. {
  79. $stmt = $this->db->prepare($query);
  80. $stmt->execute();
  81.  
  82. if($stmt->rowCount()>0)
  83. {
  84. while($row=$stmt->fetch(PDO::FETCH_ASSOC))
  85. {
  86. ?>
  87. <tr>
  88. <td><?php print($row['noticias_id']); ?></td>
  89. <td><?php print($row['titulo']); ?></td>
  90. <td><?php print($row['conteudo']); ?></td>
  91. <td><?php print($row['data']); ?></td>
  92. <td><?php print($row['img']); ?></td>
  93. <td align="center">
  94. <a href="edit-data.php?edit_id=<?php print($row['noticias_id']); ?>"><i class="glyphicon glyphicon-edit"></i></a>
  95. </td>
  96. <td align="center">
  97. <a href="delete.php?delete_id=<?php print($row['noticias_id']); ?>"><i class="glyphicon glyphicon-remove-circle"></i></a>
  98. </td>
  99. </tr>
  100. <?php
  101. }
  102. }
  103. else
  104. {
  105. ?>
  106. <tr>
  107. <td>Nothing here...</td>
  108. </tr>
  109. <?php
  110. }
  111.  
  112. }
  113.  
  114. public function paging($query,$records_per_page)
  115. {
  116. $starting_position=0;
  117. if(isset($_GET["page_no"]))
  118. {
  119. $starting_position=($_GET["page_no"]-1)*$records_per_page;
  120. }
  121. $query2=$query." limit $starting_position,$records_per_page";
  122. return $query2;
  123. }
  124.  
  125. public function paginglink($query,$records_per_page)
  126. {
  127.  
  128. $self = $_SERVER['PHP_SELF'];
  129.  
  130. $stmt = $this->db->prepare($query);
  131. $stmt->execute();
  132.  
  133. $total_no_of_records = $stmt->rowCount();
  134.  
  135. if($total_no_of_records > 0)
  136. {
  137. ?><ul class="pagination"><?php
  138. $total_no_of_pages=ceil($total_no_of_records/$records_per_page);
  139. $current_page=1;
  140. if(isset($_GET["page_no"]))
  141. {
  142. $current_page=$_GET["page_no"];
  143. }
  144. if($current_page!=1)
  145. {
  146. $previous =$current_page-1;
  147. echo "<li><a href='".$self."?page_no=1'>First</a></li>";
  148. echo "<li><a href='".$self."?page_no=".$previous."'>Previous</a></li>";
  149. }
  150. for($i=1;$i<=$total_no_of_pages;$i++)
  151. {
  152. if($i==$current_page)
  153. {
  154. echo "<li><a href='".$self."?page_no=".$i."' style='color:red;'>".$i."</a></li>";
  155. }
  156. else
  157. {
  158. echo "<li><a href='".$self."?page_no=".$i."'>".$i."</a></li>";
  159. }
  160. }
  161. if($current_page!=$total_no_of_pages)
  162. {
  163. $next=$current_page+1;
  164. echo "<li><a href='".$self."?page_no=".$next."'>Next</a></li>";
  165. echo "<li><a href='".$self."?page_no=".$total_no_of_pages."'>Last</a></li>";
  166. }
  167. ?></ul><?php
  168. }
  169. }
  170.  
  171. /* paging */
  172.  
  173. }
Advertisement
Add Comment
Please, Sign In to add comment