How to use ajax to update in real time a crud in php?

0

Hello everyone I have this question, I finished a crud that shows orders but at this moment I do not know how I can do that as a new order is added to the database, my php file is updated to show the last order in the top

This is my index:

              

<title><?php if(isset($page_title)) echo $page_title; ?></title>

<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/sweetalert.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/sweetalert.css">
    <link href="css/business-casual.css" rel="stylesheet">
    <link href="css/personalizado.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>

    

<div class='table-responsive'> <table class='table-bordered table-striped'>
    <tr>
        <th>Cliente</th>


        <th>Orden de compra</th>


        <th>Productos</th>
        <th>Presentaciones</th>
                <th>Cantidades</th>
                <th>Fecha de embarque</th>
        <th>Notas</th>
        <th>Etiquetado</th>

        <?    if($_SESSION['usuario_cliente'] == 0){?>

        <th>Factura</th>

            <? } ?>


        <th>Salida de materiales</th>


        <th>Coa</th>


                <?    if($_SESSION['usuario_cliente'] == 0){?>


        <th colspan='2'>opciones de pedidos</th>
    <? } ?>




</tr>



        <?php 
  if ($_SESSION['usuario_cliente'] == 1){
   $id =  $_SESSION['id'];

       $sql = "SELECT cliente FROM users WHERE id=:id";
            $q = $db->prepare($sql);
            $q->bindparam(':id', $id);
            $q->execute();
            $cliente = $q->fetchColumn();


            $query = "SELECT * FROM pedidos where cliente = '$cliente' order by id_pedido desc"; 

              $records_per_page=20;
        $newquery = $paginate->paging($query,$records_per_page);
        $paginate->dataview($newquery);
        $paginate->paginglink($query,$records_per_page);

  }
    else{


        if(isset($_POST['Submit'])) {
                          include_once 'partials/busqueda.php';

            $busqueda = new busqueda($db);

                    $valor = $_POST['valor'];
                    $opcion = $_POST['opcion'];
switch ($opcion) {
    case "orden_de_compra":
        $where = 'orden_de_compra';
        break;

    case "cliente":
                $where = 'cliente';
        break;
    case "productos":
                $where = 'producto';

        break;

         case "presentaciones":
                $where = 'presentacion';

        break;


         case "cantidades":
                $where = 'cantidad';

        break;


        case "fechas":
                $where = 'fecha_de_embarque';
       $valor = strftime("%Y-%m-%d", strtotime($valor));

        break;
}

                          $query = "SELECT orden_de_compra, cliente, producto, presentacion, cantidad, fecha_de_embarque, notas, etiquetado,salida_materiales_terminada, coa_terminado, factura, file FROM detalle where $where = :valor order by id_detalle desc";  

                  $records_per_page=20;
        $newquery = $busqueda->paging($query,$records_per_page);
        $busqueda->dataview($newquery,$valor);
        $busqueda->paginglink($query,$valor,$records_per_page);  






        }


else{
               $query = "SELECT orden_de_compra, cliente, productos, presentaciones, cantidades, fechas, notas, etiquetados, salida_materiales_terminada, coa_terminado, factura, file FROM pedidos order by id_pedido desc";  

                  $records_per_page=20;
        $newquery = $paginate->paging($query,$records_per_page);
        $paginate->dataview($newquery);
        $paginate->paginglink($query,$records_per_page);  

        }



    }
    //registros mostrador por pagina

        ?>


</table>


        <?php endif ?>
    </div>

And this is my crud's class:

<?php

class paginate
{
     private $db;

     function __construct($db)
     {
         $this->db = $db;
     }

     public function dataview($query)
     {
         $stmt = $this->db->prepare($query);
         $stmt->execute();

         if($stmt->rowCount()>0)
         {
                while($row=$stmt->fetch(PDO::FETCH_ASSOC))
                {
                   ?>


                   <tr>
                   <td><?php echo $row['cliente']; ?></td>
                   <td><?php echo $row['orden_de_compra']; ?></td>

                       <?php

                $productos = str_replace("[x]","<br>----<br>",$row['productos']);

    echo "<td>". $productos."</td>";
            echo "<td>".str_replace("[x]","<br>----<br>",$row['presentaciones'])."</td>";   

        echo "<td>".str_replace("[x]","<br>----<br>",$row['cantidades'])."</td>";   

    $myString =  $row['fechas'];
    $myArray = explode('[x]', $myString);

    //codigo mostrar fecha
    echo "<td>";
    foreach($myArray as $fecha)
    {
                echo date('d-m-Y', strtotime($fecha)). "<br>----<br>";


    }

  echo  "</td>";
//


        echo "<td>".str_replace("[x]","<br>----<br>",$row['notas'])."</td>";    

            echo "<td>".str_replace("[x]","<br>----<br>",$row['etiquetados'])."</td>";  

                    $checkbox_checked = "<input type='checkbox'  checked onclick='return false;'";

             if($_SESSION['usuario_cliente'] == 0){
                                   if($row['factura'] == 0){



                                          echo "<td><a href=\"verdetalle.php?orden_de_compra=$row[orden_de_compra]\">Factura</a>
                                          <br><font color='red'>pend</font></td>";   
                                                      }
                                     else{

                                         echo "
                                         <td><a href=\"verdetalle.php?orden_de_compra=$row[orden_de_compra]\">Factura</a>
                                         <br>
                                         $row[file]
                                         <br>
                                         ";

                                         $limite = $row['factura'];
                                       for ($i = 0; $i < $limite; $i++) {

                                                echo "
                                       <span class='glyphicon glyphicon-star'></span>
                                       "; 

                                             }
                                         echo "</td>";

                                     }

             }


                 if($row['salida_materiales_terminada'] == 1){

                                    echo "<td>";

                                                 if($_SESSION['usuario_cliente'] == 0){

                echo "<a href=\"formulariosalidademateriales.php?orden_de_compra=$row[orden_de_compra]\">Editar salida</a>
                <br>";
                                                 }


              echo  "<a href=\"pdfsalidademateriales.php?orden_de_compra=$row[orden_de_compra]\"><span class='glyphicon glyphicon-print'></span></a>
                $checkbox_checked"; 

                     echo "</td>";
                 }

                 else{
                                                 if($_SESSION['usuario_cliente'] == 0){



                        echo "<td><a href=\"formulariosalidademateriales.php?orden_de_compra=$row[orden_de_compra]\">Completar salida</a><br><font color='red'>pendiente</font></td>";  

                                                 }

                 }




                                                      if($row['coa_terminado'] == 0){

                                                 if($_SESSION['usuario_cliente'] == 0){


                                          echo "<td><a href=\"formulariocoatorustrading.php?orden_de_compra=$row[orden_de_compra]\">Completar Coa</a>
                                          <br><font color='red'>pend</font></td>";  
                                                 }
                                                      }

                                     else{
                                         echo "<td>";

                                          if($_SESSION['usuario_cliente'] == 0){
                                                  echo "
                                         <a href=\"formulariocoatorustrading.php?orden_de_compra=$row[orden_de_compra]\">Coa</a>";


                                            }



                                       echo"<br>
                                         <a href=\"pdfcoa.php?orden_de_compra=$row[orden_de_compra]\"><span class='glyphicon glyphicon-print'></span></a>
                                         <br>
                                         ";


                                         $limite = $row['coa_terminado'];
                                       for ($i = 0; $i < $limite; $i++) {

                                                echo "
                                       <span class='glyphicon glyphicon-star'></span>
                                       "; 

                                             }
                                         echo "</td>";

                                     }








                                     if($_SESSION['usuario_cliente'] == 0){

        echo "<td><a href=\"verdetalle.php?orden_de_compra=$row[orden_de_compra]\" class='btn btn-primary'>Ver detalle</a></td> <td><a href=\"delete.php?orden_de_compra=$row[orden_de_compra]\" onClick=\"return confirm('Esta seguro de que quiere eliminar su pedido?')\" class='btn btn-block btn-danger'>Eliminar</a></td>";
                                     }
?>

                   </tr>


                   <?php
                }
         }
         else
         {
                ?>
                <tr>
                <td>Nothing here...</td>
                </tr>
                <?php
         }

 }

 public function paging($query,$records_per_page)
 {
        $starting_position=0;
        if(isset($_GET["page_no"]))
        {
             $starting_position=($_GET["page_no"]-1)*$records_per_page;
        }
        $query2=$query." limit $starting_position,$records_per_page";
        return $query2;
 }

 public function paginglink($query,$records_per_page)
 {

        $self = $_SERVER['PHP_SELF'];

        $stmt = $this->db->prepare($query);
        $stmt->execute();

        $total_no_of_records = $stmt->rowCount();

        if($total_no_of_records > 0)
        {
            ?><tr><td colspan="14"><?php
            $total_no_of_pages=ceil($total_no_of_records/$records_per_page);
            $current_page=1;
            if(isset($_GET["page_no"]))
            {
               $current_page=$_GET["page_no"];
            }
              if(!isset($_GET["page_no"]))
            {
               $current_page=1;
            }

            if($current_page!=1)
            {
               $previous =$current_page-1;
               echo "<a href='".$self."?page_no=1'>Ultimo</a>&nbsp;&nbsp;";
               echo "<a href='".$self."?page_no=".$previous."'>Anterior</a>&nbsp;&nbsp;";
            }
            //cantidad de links de paginacion mostrados $i<=
            for($i=$current_page;$i<=$current_page+10;$i++)

            {
            if($i==$current_page)
            {
                echo "<strong><a href='".$self."?page_no=".$i."' style='color:red;text-decoration:none'>".$i."</a></strong>&nbsp;&nbsp;";
            }
            elseif($i!=$current_page and $i <= $total_no_of_pages)
            {
                echo "<a href='".$self."?page_no=".$i."'>".$i."</a>&nbsp;&nbsp;";
            }

                else {

                    break;
                }
   }
   if($current_page!=$total_no_of_pages)
   {
        $next=$current_page+1;
        echo "<a href='".$self."?page_no=".$next."'>Siguiente</a>&nbsp;&nbsp;";
        echo "<a href='".$self."?page_no=".$total_no_of_pages."'>Primero</a>&nbsp;&nbsp;";
   }
   ?></td></tr><?php
  }
 }
}
    
asked by Daniel Treviño 28.11.2017 в 22:49
source

0 answers