Foro de elhacker.net

Programación => PHP => Mensaje iniciado por: geshiro en 19 Septiembre 2015, 00:39 am



Título: ayuda con php orientado a objetos
Publicado por: geshiro en 19 Septiembre 2015, 00:39 am
el problema es que al mandar llamar la funcion conectar para la conexion no porque porq dice indefinida como le haria para poder hacerla lllamar es poo en php
la que tengo que hacer llamar en la parte del table
Código
  1.  
  2. <?php
  3. include('../Models/product.php');
  4. $product = new product();
  5. session_start();
  6.  if (isset($_SESSION['login']) && $_SESSION['login']=='YES')
  7.  {?>
  8. <!DOCTYPE html>
  9. <html lang="en">
  10.    <meta charset="UTF-8">
  11.    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  12.    <meta name="viewport" content="width=device-width, initial-scale=1">
  13.    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  14.    <title>Products</title>
  15.  
  16.    <link rel="stylesheet" href="../Resources/css/bootstrap.min.css">
  17.    <link rel="stylesheet" href="../Resources/css/main.css">
  18.  
  19. </head>
  20.  
  21.    <!--Barra de Navegacion-->
  22. <nav class="navbar navbar-default frm-nav">
  23.        <div class="navbar-header">
  24.            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
  25.                <span class="sr-only">Cambiar Navegacion</span>
  26.                <span class="icon-bar"></span>
  27.                <span class="icon-bar"></span>
  28.                <span class="icon-bar"></span>
  29.            </button>
  30.            <a href="#" class="navbar-brand">Sales</a>
  31.        </div>
  32.        <ul class="nav navbar-nav navbar-right">
  33.            <li class="dropdrown">
  34.             <a href="javascript:void(0)" onclick="" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-user"></span> <?php echo $_SESSION['name'];?><span class="caret"></span></a>
  35.            <ul class="dropdown-menu" role="menu">  
  36.                <li><a href="javascript:void(0)" onclick="settings();"><span class="glyphicon glyphicon-cog"></span> Settings</a></li>
  37.                <li><a href="javascript:void(0)" onclick="edit();"><span class="glyphicon glyphicon-edit"></span> Edit</a></li>
  38.                <li><a href="javascript:void(0)" onclick="change();
  39.                    "><span class="glyphicon glyphicon-repeat"></span> Change your password</a></li>
  40.                <li class="divider"></li>
  41.                <li><a href="javascript:void(0)" onclick="cerrar();"><span class="glyphicon glyphicon-off"> Sign out</span></a></li>
  42.            </ul>
  43.        </ul>  
  44.        <ul class="nav navbar-nav navbar-right">
  45.        <li class="dropdown">
  46.           <a href="javascript:void(0)" onclick="" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-flag"></span> Reports<span class="caret"></span></a>
  47.          <ul class="dropdown-menu" role="menu">
  48.            <li><a href=""><span class="glyphicon glyphicon-list"></span> Items</a></li>
  49.            <li><a href="#"><span class="glyphicon glyphicon-list"></span> Parents list</a></li>
  50.          </ul>
  51.        </li>
  52.      </ul>
  53.      <ul class="nav navbar-nav navbar-right">
  54.        <li><a href="javascript:void(0)" onclick=""><span class="glyphicon glyphicon-home"></span> home</a></li>
  55.        <li><a href="javascript:void(0)" id="low_stock" onclick=""><span class="glyphicon glyphicon-alert"></span> slow stock <span id="notif" class="badge">0</span></a></li>
  56.      </ul>
  57. </nav>  
  58. <div class="col-md-3">
  59.    <ul class="nav nav-pills nav-stacked">
  60.        <li class="active"><a href="javascript:void(0)" onclick="home();"><span class="glyphicon glyphicon-home"></span> home</a></li>
  61.        <li><a href="javascript:void(0)" onclick="product();"><span class=" glyphicon glyphicon-bed"></span> products</a></li>
  62.        <li><a href="javascript:void(0)" onclick="sales();"><span class="glyphicon glyphicon-credit-card"></span> Sales</a></li>
  63.       <li><a href="javascript:void(0)" id="msg" onclick="users();"><span class="glyphicon glyphicon-user"></span> Users <span id="notif" class="badge">0</span></a></li>
  64.    </ul>
  65. </div>
  66.    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModallabel" aria-hidden="true">
  67.                 <div class="modal-dialog">
  68.                   <div class="modal-content">
  69.                    <div class="modal-header">
  70.                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  71.                        <h4 class="modal-title" id="myModalLabel">Registro</h4>
  72.                    </div>
  73.            <div class="modal-body">
  74.                <form class="form-horizontal" id="formCliente">
  75.                    <div class="form-group">
  76.                        <label for="fecha" class="control-label col-xs-5">date: </label>
  77.                        <div class="col-xs-4">
  78.                             <input type="text" class="form-control" value="<?php echo date('y-m-d'); ?>" disabled="disabled"/>
  79.                        </div>
  80.                    </div>
  81.                     <div class="form-group">
  82.                          <label for="code" class="control-label col-xs-5">code:</label>
  83.                          <div class="col-xs-2 frm-code">
  84.                               <input type="text" id="code" name="code" class="form-control" onkeyup="validacion('code');" aria-describedby="inputSuccess2Status">
  85.                               <span class="help-block"></span>
  86.                         </div>
  87.                   </div>
  88.                    <div class="form-group">
  89.                          <label for="product" class="control-label col-xs-5">Product:</label>
  90.                          <div class="col-xs-4 frm-product">
  91.                               <select class="form-control" id="product" name="product" onchange="validacion('product');">
  92.                                        <option value="reset_product">Select an option</option>
  93.                                        <option>usb 8gb</option>
  94.                                        <option>usb 16gb  Kingston</option>
  95.                                        <option>usb 32gb  Kingston</option>
  96.                                        <option>usb 64gb  Kingston</option>
  97.                                        <option>usb 128gb Kingston</option>
  98.                                        <option>SD 8gb</option>
  99.                                        <option>SD 16gb</option>
  100.                                        <option>SD 32GB</option>
  101.                                        <option>SD 64gb</option>
  102.                                        <option>SD 128gb</option>
  103.                                        <option>GO PRO HERO</option>
  104.                                        <option>GO PRO BLACK EDITION</option>
  105.                                        <option>GO PRO SILVER EDITION</option>
  106.                                        <option>GO PRO 2 HERO</option>
  107.                                        <option>GO PRO 2 HERO BLACK EDITION</option>
  108.                                        <option>GO PRO 2 HERO SILVER EDITION</option>
  109.                                        <option>GO PRO 3 HERO</option>
  110.                                        <option>GO PRO 3 HERO</option>
  111.                                        <option>GO PRO 3 HERO BLACK EDITION</option>
  112.                                        <option>GO PRO 3 HERO SILVER EDITION</option>
  113.                                        <option>Snorkel</option>
  114.                                        <option>Aletas</option>
  115.                                        <option>Tanque  24lb</option>
  116.                               </select>
  117.                         </div>
  118.                   </div>
  119.                     <div class="form-group">
  120.                          <label for="price" class="control-label col-xs-5">Price:</label>
  121.                          <div class="col-xs-3 frm-price">
  122.                               <input type="text" class="form-control"  id="price" name="price" onkeyup="validacion('price');">
  123.                               <span class="help-block"></span>
  124.                         </div>
  125.                   </div>
  126.                     <div class="form-group">
  127.                          <label for="stock" class="control-label col-xs-5">Stock: </label>
  128.                          <div class="col-xs-4">
  129.                               <select class="form-control" id="stock" name="stock" onchange="validacion('stock')";>
  130.                                        <option value="rest_stock">Select an option</option>
  131.                                        <option>1</option>
  132.                                        <option>2</option>
  133.                                        <option>3</option>
  134.                                        <option>5</option>
  135.                                        <option>6</option>
  136.                                        <option>7</option>
  137.                                        <option>8</option>
  138.                                        <option>9</option>
  139.                                        <option>10</option>      
  140.                               </select>
  141.                         </div>
  142.                   </div>
  143.                </form>
  144.            </div>
  145.            <div class="form-group">
  146.                  <div class="alert alert-success text-center" style="display:none;" id="success">
  147.                        <strong>Felicidades: </strong>Su registro ha sido guardado
  148.                  </div>
  149.                  <div class="alert alert-danger text-center" style="display:none;" id="error">
  150.                        <strong>Alert: </strong>you must to complete all fields
  151.                  </div>
  152.            </div>
  153.            <div class="modal-footer">
  154.                <button type="button" class="btn btn-default" data-dismiss="modal">close</button>
  155.                <button type="button" class="btn btn-success" onclick="add_product();"><span class="glyphicon glyphicon-plus"></span> Add</button>
  156.            </div>                
  157.        </div>
  158.    </div>
  159. </div>
  160. <div class="container">
  161.  <div class="tab-content nav col-md-8 frm-grid">
  162.    <h4>Products</h4>
  163.      <div class="form-group col-xs-3 frm-search">
  164.          <div class="form-group has-feedback">
  165.                <input type="text" class="form-control" id="inputValidation" placeholder="Search"/>
  166.                <span class="glyphicon glyphicon-search form-control-feedback"></span>
  167.          </div>
  168.    </div>
  169.    <button class="btn btn-success frm-add" data-toggle="modal" data-target="#myModal"><span class="glyphicon glyphicon-plus"></span> Add products</button>
  170.    <table class="table table-hover table-resposive">
  171.      <thead>
  172.          <tr>
  173.            <th>Item</th>
  174.            <th>code</th>
  175.            <th>Description</th>
  176.            <th>Price</th>
  177.            <th>Stock</th>
  178.            <th>Actions</th>
  179.          </tr>
  180.      </thead>  
  181.      <tbody>
  182.             <?php
  183.                  $product->conectar();
  184.                  $product->show_product();
  185.             ?>
  186.      </tbody>
  187.    </table>
  188.  </div>
  189. </div>
  190.    <script src="../Resources/js/jquery-1.11.2.js"></script>
  191.    <script src="../Resources/js/bootstrap.min.js"></script>
  192.    <script src="../Resources/js/actions.js"></script>
  193.    <script src="../Resources/js/validacion.js"></script>
  194.  
  195.    <script>
  196.        function cerrar(){
  197.            $.ajax({
  198.                url:'../Controllers/user.php',
  199.                type:'POST',
  200.                data:"boton=cerrar"
  201.            }).done(function(resp){
  202.                location.href = '../Views/'
  203.            });
  204.        }
  205.       function add_product(){
  206.           var code    = $('#code').val();
  207.           var product = $('#product').val();
  208.           var price   = $('#price').val();
  209.           var stock   = $('#stock').val();
  210.           var valCheck = verificar();
  211.             if(valCheck == 1){
  212.              $.ajax({
  213.                      url: '../Controllers/user.php',
  214.                      type: 'POST',
  215.                      data: 'code='+code+'&product='+product+'&price='+price+'&stock='+stock+'&boton=add_product'
  216.              }).done(function(ans){
  217.              if(ans == 'success'){
  218.                      $('#code').val("");
  219.                      $('#product').val('reset_product');
  220.                      $('#price').val("");
  221.                      $('#stock').val('rest_stock');
  222.                      $('#success').show().delay(2000).fadeOut();
  223.  
  224.              }else{
  225.                      alert(ans);
  226.                }
  227.              })
  228.            }
  229.            else {
  230.             }
  231.        }
  232.        $(function(){
  233.            var message = $('#notif');
  234.            $('#msg').append(message);
  235.            message.show('slow');
  236.        });
  237.  
  238.         $(function(){
  239.            var message = $('#notif');
  240.            $('#low_stock').append(message);
  241.            message.show('slow');
  242.        });
  243.    </script>
  244. </body>
  245. </html>
  246.  
  247. <?php
  248.  
  249.  }
  250.  else
  251.  {
  252.    header("location: ./");
  253.  }
  254. ?>
  255.  

Código
  1. <?php
  2. class product{
  3. private $conexion;
  4. public function __construct(){
  5. require_once('conexion.php');
  6. $this->conexion = new conexion();
  7. $this->conexion->conectar();
  8. }
  9.  
  10. function show_product(){
  11. $sql = "SELECT * FROM product";
  12. $record = $this->conexion->query($sql);
  13. $item =0;
  14.  
  15. if(!$this->conexion->query($sql)){
  16. printf("Errormessage: %s\n", $this->conexion->error);
  17. }
  18. if($record->num_rows>0){
  19. while($show = $record->fetch_array(MYSQLI_ASSOC)){
  20. $item += 1;
  21. echo '<tr>
  22. <td>'.$item.'</td>
  23. <td>'.$show["code"].'</td>
  24. <td>'.$show["product"].'</td>
  25. <td>'.$show["price"].'</td>
  26. <td>'.$show["stock"].'</td>
  27. <td><button class="btn btn-warning frm-del"><span class="glyphicon glyphicon-edit"><span<button><button class="btn btn-danger"><span class="glyphicon glyphicon-trash"><span<button></td>
  28. </tr>';
  29. }
  30. }
  31. else{
  32. echo '<tr><td colspan="5">no hay registros</td></tr>';
  33. }
  34. }
  35.  
  36. }
  37. ?>
  38.  

Código
  1. <?php
  2. class conexion
  3. {
  4. private $server;
  5. private $user;
  6. private $password;
  7. private $database;
  8. public  $conexion;
  9.  
  10. public function __construct(){
  11. $this->server    = "localhost";
  12. $this->user   = "root";
  13. $this->password  = "";
  14. $this->database  = "db_login";
  15.  
  16. }
  17.  
  18. function conectar(){
  19. $this->conexion= new mysqli($this->server,$this->user,$this->password,$this->database);
  20. }
  21.  
  22. function cerrar(){
  23. $this->conexion->close();
  24. }
  25.  
  26. }
  27. ?>
  28.  


Título: Re: ayuda con php orientado a objetos
Publicado por: T. Collins en 19 Septiembre 2015, 01:02 am
Con esto vale:

Código
  1. <?php
  2.       $product->show_product();
  3. ?>

al hacer
Código
  1. <?php
  2.       $product = new product();
  3. ?>

ya se inicia la conexión


Título: Re: ayuda con php orientado a objetos
Publicado por: geshiro en 19 Septiembre 2015, 01:43 am
Osea que ponindola en el tbale $product = new product(); automatico la unicoo solo tendria que poner table ><?php $product = new product(); $product->show_product(); ?> si no para que unicie la conexion y lo muestre no?


Título: Re: ayuda con php orientado a objetos
Publicado por: T. Collins en 19 Septiembre 2015, 01:53 am
Sí, si te fijas, el constructor de la clase product, crea la conexión y llama al método conectar() y lo deja ya preparado para usar show_product();

Código
  1. public function __construct(){
  2. require_once('conexion.php');
  3. $this->conexion = new conexion();
  4. $this->conexion->conectar();
  5. }


Título: Re: ayuda con php orientado a objetos
Publicado por: geshiro en 19 Septiembre 2015, 03:57 am
Si lo vemos de manera grafica lo deje pre-preparada como sopa instantanea solo para agregarle agua


Título: Re: ayuda con php orientado a objetos
Publicado por: geshiro en 20 Septiembre 2015, 23:02 pm
ya lo movi como me dijiste pero ahora me sale
Fatal error: Call to undefined method conexion::query() in C:\xampp\htdocs\punto\Models\product.php on line 12

Código
  1. <?php
  2. class product{
  3. private $conexion;
  4. public function __construct(){
  5. require_once('conexion.php');
  6. $this->conexion = new conexion();
  7. $this->conexion->conectar();
  8. }
  9.  
  10. function show_product(){
  11. $sql = "SELECT * FROM product";
  12. $record = $this->conexion->query($sql);
  13. $item =0;
  14.  
  15. if(!$this->conexion->query($sql)){
  16. printf("Errormessage: %s\n", $this->conexion->error);
  17. }
  18. if($record->num_rows>0){
  19. while($show = $record->fetch_array(MYSQLI_ASSOC)){
  20. $item += 1;
  21. echo '<tr>
  22. <td>'.$item.'</td>
  23. <td>'.$show["code"].'</td>
  24. <td>'.$show["product"].'</td>
  25. <td>'.$show["price"].'</td>
  26. <td>'.$show["stock"].'</td>
  27. <td><button class="btn btn-warning frm-del"><span class="glyphicon glyphicon-edit"><span<button><button class="btn btn-danger"><span class="glyphicon glyphicon-trash"><span<button></td>
  28. </tr>';
  29. }
  30. }
  31. else{
  32. echo '<tr><td colspan="5">no hay registros</td></tr>';
  33. }
  34. }
  35.  
  36. }
  37. ?>
  38.  
Código
  1. <?php
  2. class conexion
  3. {
  4. private $server;
  5. private $user;
  6. private $password;
  7. private $database;
  8. public  $conexion;
  9.  
  10. public function __construct(){
  11. $this->server    = "localhost";
  12. $this->user   = "root";
  13. $this->password  = "";
  14. $this->database  = "db_login";
  15.  
  16. }
  17.  
  18. function conectar(){
  19. $this->conexion= new mysqli($this->server,$this->user,$this->password,$this->database);
  20. }
  21.  
  22. function cerrar(){
  23. $this->conexion->close();
  24. }
  25.  
  26. function show_product(){
  27.  
  28. $sql = "SELECT * FROM product";
  29. $resultado = $this->conexion->query($sql);
  30. $item = 0;
  31. if (!$this->conexion->query($sql)) {
  32. printf("Errormessage: %s\n", $this->conexion->error);
  33. }
  34.  
  35. if($resultado->num_rows>0){
  36. while($show = $resultado->fetch_array(MYSQLI_ASSOC)){
  37.  
  38. echo '<tr>
  39. <td>'.$show["id_product"].'</td>
  40. <td>'.$show["product"].'</td>
  41. </tr>';
  42. }
  43. }else{
  44. echo '<tr><td colspan="6">no hay registros...</td></tr>';
  45. }
  46. }
  47. }
  48. ?>
  49.  


Título: Re: ayuda con php orientado a objetos
Publicado por: T. Collins en 21 Septiembre 2015, 00:06 am
Código
  1. $record = $this->conexion->conexion->query($sql);

Código
  1. if(!$this->conexion->conexion->query($sql)){
  2. printf("Errormessage: %s\n", $this->conexion->conexion->error);
  3. }


Título: Re: ayuda con php orientado a objetos
Publicado por: geshiro en 23 Septiembre 2015, 08:07 am
que le falta o puediera hacer para que mi consulta sea dinamica cuando agrege que se muestre al mismo tiempo??

Código
  1. <?php
  2. include('../Models/product.php');
  3. $product = new product();
  4. session_start();
  5.  if (isset($_SESSION['login']) && $_SESSION['login']=='YES')
  6.  {?>
  7. <!DOCTYPE html>
  8. <html lang="en">
  9.    <meta charset="UTF-8">
  10.    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  11.    <meta name="viewport" content="width=device-width, initial-scale=1">
  12.    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  13.    <title>Products</title>
  14.  
  15.    <link rel="stylesheet" href="../Resources/css/bootstrap.min.css">
  16.    <link rel="stylesheet" href="../Resources/css/main.css">
  17.  
  18. </head>
  19.  
  20.    <!--Barra de Navegacion-->
  21. <nav class="navbar navbar-default frm-nav">
  22.        <div class="navbar-header">
  23.            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
  24.                <span class="sr-only">Cambiar Navegacion</span>
  25.                <span class="icon-bar"></span>
  26.                <span class="icon-bar"></span>
  27.                <span class="icon-bar"></span>
  28.            </button>
  29.            <a href="#" class="navbar-brand">Sales</a>
  30.        </div>
  31.        <ul class="nav navbar-nav navbar-right">
  32.            <li class="dropdrown">
  33.             <a href="javascript:void(0)" onclick="" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-user"></span> <?php echo $_SESSION['name'];?><span class="caret"></span></a>
  34.            <ul class="dropdown-menu" role="menu">  
  35.                <li><a href="javascript:void(0)" onclick="settings();"><span class="glyphicon glyphicon-cog"></span> Settings</a></li>
  36.                <li><a href="javascript:void(0)" onclick="edit();"><span class="glyphicon glyphicon-edit"></span> Edit</a></li>
  37.                <li><a href="javascript:void(0)" onclick="change();
  38.                    "><span class="glyphicon glyphicon-repeat"></span> Change your password</a></li>
  39.                <li class="divider"></li>
  40.                <li><a href="javascript:void(0)" onclick="cerrar();"><span class="glyphicon glyphicon-off"> Sign out</span></a></li>
  41.            </ul>
  42.        </ul>  
  43.        <ul class="nav navbar-nav navbar-right">
  44.        <li class="dropdown">
  45.           <a href="javascript:void(0)" onclick="" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-flag"></span> Reports<span class="caret"></span></a>
  46.          <ul class="dropdown-menu" role="menu">
  47.            <li><a href=""><span class="glyphicon glyphicon-list"></span> Items</a></li>
  48.            <li><a href="#"><span class="glyphicon glyphicon-list"></span> Parents list</a></li>
  49.          </ul>
  50.        </li>
  51.      </ul>
  52.      <ul class="nav navbar-nav navbar-right">
  53.        <li><a href="javascript:void(0)" onclick=""><span class="glyphicon glyphicon-home"></span> home</a></li>
  54.        <li><a href="javascript:void(0)" id="low_stock" onclick=""><span class="glyphicon glyphicon-alert"></span> slow stock <span id="notif" class="badge">0</span></a></li>
  55.      </ul>
  56. </nav>  
  57. <div class="col-md-3">
  58.    <ul class="nav nav-pills nav-stacked">
  59.        <li class="active"><a href="javascript:void(0)" onclick="home();"><span class="glyphicon glyphicon-home"></span> home</a></li>
  60.        <li><a href="javascript:void(0)" onclick="product();"><span class=" glyphicon glyphicon-bed"></span> products</a></li>
  61.        <li><a href="javascript:void(0)" onclick="sales();"><span class="glyphicon glyphicon-credit-card"></span> Sales</a></li>
  62.       <li><a href="javascript:void(0)" id="msg" onclick="users();"><span class="glyphicon glyphicon-user"></span> Users <span id="notif" class="badge">0</span></a></li>
  63.    </ul>
  64. </div>
  65.    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModallabel" aria-hidden="true">
  66.                 <div class="modal-dialog">
  67.                   <div class="modal-content">
  68.                    <div class="modal-header">
  69.                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  70.                        <h4 class="modal-title" id="myModalLabel">Registro</h4>
  71.                    </div>
  72.            <div class="modal-body">
  73.                <form class="form-horizontal" id="formCliente">
  74.                    <div class="form-group">
  75.                        <label for="fecha" class="control-label col-xs-5">date: </label>
  76.                        <div class="col-xs-4">
  77.                             <input type="text" class="form-control" value="<?php echo date('y-m-d'); ?>" disabled="disabled"/>
  78.                        </div>
  79.                    </div>
  80.                     <div class="form-group">
  81.                          <label for="code" class="control-label col-xs-5">code:</label>
  82.                          <div class="col-xs-2 frm-code">
  83.                               <input type="text" id="code" name="code" class="form-control" onkeyup="validacion('code');" aria-describedby="inputSuccess2Status">
  84.                               <span class="help-block"></span>
  85.                         </div>
  86.                   </div>
  87.                    <div class="form-group">
  88.                          <label for="product" class="control-label col-xs-5">Product:</label>
  89.                          <div class="col-xs-4 frm-product">
  90.                               <select class="form-control" id="product" name="product" onchange="validacion('product');">
  91.                                        <option value="reset_product">Select an option</option>
  92.                                        <option>usb 8gb</option>
  93.                                        <option>usb 16gb  Kingston</option>
  94.                                        <option>usb 32gb  Kingston</option>
  95.                                        <option>usb 64gb  Kingston</option>
  96.                                        <option>usb 128gb Kingston</option>
  97.                                        <option>SD 8gb</option>
  98.                                        <option>SD 16gb</option>
  99.                                        <option>SD 32GB</option>
  100.                                        <option>SD 64gb</option>
  101.                                        <option>SD 128gb</option>
  102.                                        <option>GO PRO HERO</option>
  103.                                        <option>GO PRO BLACK EDITION</option>
  104.                                        <option>GO PRO SILVER EDITION</option>
  105.                                        <option>GO PRO 2 HERO</option>
  106.                                        <option>GO PRO 2 HERO BLACK EDITION</option>
  107.                                        <option>GO PRO 2 HERO SILVER EDITION</option>
  108.                                        <option>GO PRO 3 HERO</option>
  109.                                        <option>GO PRO 3 HERO</option>
  110.                                        <option>GO PRO 3 HERO BLACK EDITION</option>
  111.                                        <option>GO PRO 3 HERO SILVER EDITION</option>
  112.                                        <option>Snorkel</option>
  113.                                        <option>Aletas</option>
  114.                                        <option>Tanque  24lb</option>
  115.                               </select>
  116.                         </div>
  117.                   </div>
  118.                     <div class="form-group">
  119.                          <label for="price" class="control-label col-xs-5">Price:</label>
  120.                          <div class="col-xs-3 frm-price">
  121.                               <input type="text" class="form-control"  id="price" name="price" onkeyup="validacion('price');">
  122.                               <span class="help-block"></span>
  123.                         </div>
  124.                   </div>
  125.                     <div class="form-group">
  126.                          <label for="stock" class="control-label col-xs-5">Stock: </label>
  127.                          <div class="col-xs-4">
  128.                               <select class="form-control" id="stock" name="stock" onchange="validacion('stock')";>
  129.                                        <option value="rest_stock">Select an option</option>
  130.                                        <option>1</option>
  131.                                        <option>2</option>
  132.                                        <option>3</option>
  133.                                        <option>5</option>
  134.                                        <option>6</option>
  135.                                        <option>7</option>
  136.                                        <option>8</option>
  137.                                        <option>9</option>
  138.                                        <option>10</option>      
  139.                               </select>
  140.                         </div>
  141.                   </div>
  142.                </form>
  143.            </div>
  144.            <div class="form-group">
  145.                  <div class="alert alert-success text-center" style="display:none;" id="success">
  146.                        <strong>Felicidades: </strong>Su registro ha sido guardado
  147.                  </div>
  148.                  <div class="alert alert-danger text-center" style="display:none;" id="error">
  149.                        <strong>Alert: </strong>you must to complete all fields
  150.                  </div>
  151.            </div>
  152.            <div class="modal-footer">
  153.                <button type="button" class="btn btn-default" data-dismiss="modal">close</button>
  154.                <button type="button" class="btn btn-success" onclick="add_product();"><span class="glyphicon glyphicon-plus"></span> Add</button>
  155.            </div>                
  156.        </div>
  157.    </div>
  158. </div>
  159. <div class="container">
  160.  <div class="tab-content nav col-md-8 frm-grid">
  161.    <h4>Products</h4>
  162.      <div class="form-group col-xs-3 frm-search">
  163.          <div class="form-group has-feedback">
  164.                <input type="text" class="form-control" id="inputValidation" placeholder="Search"/>
  165.                <span class="glyphicon glyphicon-search form-control-feedback"></span>
  166.          </div>
  167.    </div>
  168.    <button class="btn btn-success frm-add" data-toggle="modal" data-target="#myModal"><span class="glyphicon glyphicon-plus"></span> Add products</button>
  169.    <table class="table table-hover table-resposive">
  170.      <thead>
  171.          <tr>
  172.            <th>Item</th>
  173.            <th>code</th>
  174.            <th>Description</th>
  175.            <th>Price</th>
  176.            <th>Stock</th>
  177.            <th>Actions</th>
  178.          </tr>
  179.      </thead>  
  180.      <tbody>
  181.             <?php
  182.                  $product->show_product();
  183.             ?>
  184.      </tbody>
  185.    </table>
  186.  </div>
  187. </div>
  188.    <script src="../Resources/js/jquery-1.11.2.js"></script>
  189.    <script src="../Resources/js/bootstrap.min.js"></script>
  190.    <script src="../Resources/js/actions.js"></script>
  191.    <script src="../Resources/js/validacion.js"></script>
  192.  
  193.    <script>
  194.        function cerrar(){
  195.            $.ajax({
  196.                url:'../Controllers/user.php',
  197.                type:'POST',
  198.                data:"boton=cerrar"
  199.            }).done(function(resp){
  200.                location.href = '../Views/'
  201.            });
  202.        }
  203.       function add_product(){
  204.           var code    = $('#code').val();
  205.           var product = $('#product').val();
  206.           var price   = $('#price').val();
  207.           var stock   = $('#stock').val();
  208.           var valCheck = verificar();
  209.             if(valCheck == 1){
  210.              $.ajax({
  211.                      url: '../Controllers/actions.php',
  212.                      type: 'POST',
  213.                      data: 'code='+code+'&product='+product+'&price='+price+'&stock='+stock+'&boton=add_product'
  214.              }).done(function(ans){
  215.              if(ans == 'success'){
  216.                      $('#code').val("");
  217.                      $('#product').val('reset_product');
  218.                      $('#price').val("");
  219.                      $('#stock').val('rest_stock');
  220.                      $('#success').show().delay(2000).fadeOut();
  221.  
  222.              }else{
  223.                      alert(ans);
  224.                }
  225.              })
  226.            }
  227.            else {
  228.             }
  229.        }
  230.        $(function(){
  231.            var message = $('#notif');
  232.            $('#msg').append(message);
  233.            message.show('slow');
  234.        });
  235.  
  236.         $(function(){
  237.            var message = $('#notif');
  238.            $('#low_stock').append(message);
  239.            message.show('slow');
  240.        });
  241.    </script>
  242. </body>
  243. </html>
  244.  
  245. <?php
  246.  
  247.  }
  248.  else
  249.  {
  250.    header("location: ./");
  251.  }
  252. ?>
  253.  
  254.  
Código
  1. <?php
  2. class product{
  3. private $conexion;
  4. public function __construct(){
  5. require_once('conexion.php');
  6. $this->conexion = new conexion();
  7. $this->conexion->conectar();
  8. }
  9.  
  10. function add($price,$stock,$code,$product){
  11. $sql1 ="INSERT INTO product (price,stock,code,product) VALUES('$price','$stock','$code','$product')";
  12. if($this->conexion->conexion->query($sql1)){
  13. return true;
  14. }
  15. else{
  16. return false;
  17. }
  18. $this->conexion->cerrar();
  19. }
  20.  
  21. function show_product(){
  22. $sql = "SELECT * FROM product";
  23. $record = $this->conexion->conexion->query($sql);
  24. $item =0;
  25.  
  26. if(!$this->conexion->conexion->query($sql)){
  27. printf("Errormessage: %s\n", $this->conexion->conexion->error);
  28. }
  29. if($record->num_rows>0){
  30. while($show = $record->fetch_array(MYSQLI_ASSOC)){
  31. $item += 1;
  32. echo '<tr>
  33. <td>'.$item.'</td>
  34. <td>'.$show["code"].'</td>
  35. <td>'.$show["product"].'</td>
  36. <td>'.$show["price"].'</td>
  37. <td>'.$show["stock"].'</td>
  38. <td><button class="btn btn-warning frm-del"><span class="glyphicon glyphicon-edit"><span<button><button class="btn btn-danger"><span class="glyphicon glyphicon-trash"><span<button></td>
  39. </tr>';
  40. }
  41. }
  42. else{
  43. echo '<tr><td colspan="5">no hay registros</td></tr>';
  44. }
  45. }
  46.  
  47. }
  48. ?>
  49.