Guest User

Untitled

a guest
Mar 15th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class Manipula_BD
  5. {
  6. private $host="localhost", $user="root", $pass="", $con="", $seldb="", $tabela="";
  7. private $sql_ins="", $sql_sel="", $sql_upd="", $sql_del="";
  8. private $ins="", $sel="", $upd, $del="";
  9. public $menu = "<center><b>Aplicativo Iniciante em PHPOO - Menu Principal</b><br><br></center>", $form="";
  10.  
  11.  
  12. public function __construct($banco, $tabela)
  13. {
  14. $this->tabela = $tabela;
  15. if($this->con = mysql_connect($this->host, $this->user, $this->pass))
  16. {
  17. if(!$this->seldb = mysql_select_db($banco, $this->con))
  18. {
  19. die ('<center>Erro ao selecionar o banco '. "<br>" .mysql_error())."</center>";
  20. }
  21. }
  22. }
  23.  
  24. public function inserir($campos, $valores)
  25. {
  26. $this->sql_ins = "INSERT INTO " . $this->tabela . " ($campos) VALUES ($valores)";
  27. if(!$this->ins = mysql_query($this->sql_ins, $this->con))
  28. {
  29. die ("<center>Erro na inclusão <br>" . mysql_error() . "<br>
  30. <a href='menu.php'>Menu</a></center>");
  31. }else{
  32. print "<center><a href='menu.php'>Menu</a></center>";
  33. }
  34. }
  35.  
  36. public function consultar($campos, $where = NULL, $orderby = NULL)
  37. {
  38. if ($where)
  39. {
  40. $this->sql_sel = "SELECT $campos FROM " . $this->tabela . " WHERE $where ORDER BY $orderby";
  41. }else{
  42. $this->sql_sel = "SELECT $campos FROM " . $this->tabela." ORDER BY $orderby";
  43. }
  44.  
  45. if(!$this->sel = mysql_query($this->sql_sel, $this->con))
  46. {
  47. die ("Erro na Consulta <br>" .mysql_error() ."<br><a href='menu.php'>Menu</a>");
  48. }else{
  49. $regs = "<center><table border=\"0\"> ";
  50. $regs .= "<tr><td colspan=\"2\"><h2>Resultado da Consulta</h2></td></tr>";
  51.  
  52. $row=0;
  53. $nf=mysql_num_fields($this->sel);
  54. $regs .= "<tr>";
  55. for($x=0;$x < $nf ;$x++){
  56. $regs .="<td><b>".ucfirst(mysql_field_name($this->sel,$x))."</b></td>";
  57. }
  58. $regs .= "</tr>";
  59. while ( $data = mysql_fetch_array($this->sel)) {
  60. $regs .= "<tr>";
  61. for($x=0;$x < $nf;$x++){
  62. $regs .= "<td>" . $data[$x] . "</td>";
  63. }
  64. $row++;
  65. $regs .= "</tr>";
  66. }
  67. if (!$this->sel){
  68. die ("<center>Erro na Consulta <br>" .mysql_error() ."<br>
  69. <a href='menu.php'>Menu</a></center>");
  70. }else{
  71. $regs .= "</table>";
  72. print $regs;
  73. print "<br><br><a href='menu.php'>Voltar ao Menu</a>";
  74. }
  75. }
  76. }
  77.  
  78. public function atualizar($camposvalores, $where = NULL)
  79. {
  80. if ($where)
  81. {
  82. $this->sql_upd = "UPDATE " . $this->tabela . " $camposvalores WHERE $where";
  83. }else{
  84. $this->sql_upd = "UPDATE " . $this->tabela . " $camposvalores";
  85. }
  86.  
  87. if(!$this->upd = mysql_query($this->sql_upd, $this->con))
  88. {
  89. die ("<center>Erro na atualização <br>" .mysql_error() . "<br><a href='menu.php'>Menu</a></center>");
  90. }else{
  91. print "<a href='menu.php'>Voltar ao Menu</a>";
  92. }
  93. }
  94.  
  95. public function excluir($where = NULL)
  96. {
  97. if ($where)
  98. {
  99. $this->sql_sel = "SELECT * FROM " . $this->tabela . " WHERE $where";
  100. $this->sql_del = "DELETE FROM " . $this->tabela . " WHERE $where";
  101. }else{
  102. $this->sql_sel = "SELECT * FROM " . $this->tabela;
  103. $this->sql_del = "DELETE FROM " . $this->tabela;
  104. }
  105. $sel=mysql_query($this->sql_sel);
  106. $regs=mysql_num_rows($sel);
  107.  
  108. if ($regs > 0){
  109. if(!$this->del = mysql_query($this->sql_del, $this->con))
  110. {
  111. die ("<center>Erro na exclusão <br>" .mysql_error() ."<br>
  112. <a href='menu.php'>Menu </a></center>" );
  113. }else{
  114. print "<center>Registro Excluído com Sucesso!<br><a href='menu.php'>Menu</a></center>";
  115. }
  116. }else{
  117. print "<center>Registro Não encontrado!<br><a href='menu.php'>Menu</a></center>";
  118. }
  119. }
  120.  
  121.  
  122. public function menu ($scripts=array())
  123. {
  124. reset($scripts);
  125. $this->menu .="<center><table border=\"0\"> ";
  126. while (list($chave, $arquivo) = each ($scripts)) {
  127. $this->menu .= "<tr><td><a href='$arquivo'>$arquivo</a></td></tr>";
  128. }
  129. $this->menu .="</table></center>";
  130. print $this->menu;
  131. }
  132.  
  133. public function form ($campos=array(), $acao, $nome, $metodo)
  134. {
  135. reset($campos);
  136. $this->form .= "<center><table border=\"0\"> ";
  137. $this->form .= "<form name=\"$nome\" action=\"$acao\" method=\"$metodo\"><br />\n";
  138.  
  139. while (list($chave, $campo) = each ($campos)) {
  140. $this->form .= "<tr><td>$campo</td><td><input type=\"text\" name=\"$campo\" value=\"\"></td></tr>";
  141. }
  142. $this->form .= "<tr><td>&nbsp;</td><td colspan=\"2\"><input type=\"submit\" value=\"Enviar\">";
  143. $this->form .= "&nbsp;<input type=\"reset\" value=\"Limpar\"></td></tr>";
  144. $this->form .= "</form>";
  145. $this->form .= "</table></center>";
  146. print $this->form;
  147. }
  148.  
  149. public function form_dados($where = NULL, $acao, $metodo, $nome)
  150. {
  151. if ($where)
  152. {
  153. $this->sql_sel = "SELECT * FROM " . $this->tabela . " WHERE $where";
  154. }else{
  155. $this->sql_sel = "SELECT * FROM " . $this->tabela;
  156. }
  157.  
  158. if(!$this->sel = mysql_query($this->sql_sel, $this->con))
  159. {
  160. die ("<center>Erro na Consulta <br>" .mysql_error() ."<br>
  161. <a href='javascript:history.back()'>Voltar</a></center>");
  162. }else{
  163. $form = "<form name=$nome method=$metodo action=$acao><center><table border=\"0\">";
  164. while ($campo = mysql_fetch_array($this->sel, MYSQL_NUM)) {
  165. $form .= "<tr><td>".mysql_field_name($this->sel,0)."</td><td><input type=\"text\"
  166. name=".mysql_field_name($this->sel,0)." value=\"$campo[0]\"></td></tr>";
  167. $form .= "<tr><td>".mysql_field_name($this->sel,1)."</td><td><input type=\"text\"
  168. name=".mysql_field_name($this->sel,1)." value=\"$campo[1]\"></td></tr>";
  169. }
  170. $form .= "<tr><td>&nbsp;</td><td><input type=\"submit\" value=\"Enviar\">&nbsp;";
  171. $form .= "&nbsp;<input type=\"reset\" value=\"Limpar\"></td></tr>";
  172. $form .= "</table></form>";
  173.  
  174. print $form;
  175. }
  176. }
  177. }
  178.  
  179. // Deixar o objeto já criado para ter sempre a conexão aberta
  180. $objCliente = new Manipula_Bd('iniciante', 'cliente'); // Conectar com $banco e $tabela
  181. ?>
Add Comment
Please, Sign In to add comment