go from php to html [closed]

-1

Here the customers of the table are ready

<?php 
$result = mysql_query("SELECT * FROM tabla " ) ; 
if ($row = mysql_fetch_array($result)){ 
echo "<table border = '0' width='100%'> n"; 
echo "<tr><td><strong>Cabecera 1</strong></td><td><strong>Cabecera 2</strong></td><td><strong>Cabecera 3</strong></td></tr> n"; 
do { 
echo "<tr><td>".$row['id']."</td><td>".$row['nombre']."</td><td>".$row['apellidos']."</td></tr> n"; 
} while ($row = mysql_fetch_array($result)); 
echo "</table> n"; 
} else { 
echo "Texto si no se encuentran resultados"; 
} 
?> 

But I want to paint the html in javascript using templates, could you tell me where to start?

    
asked by 26.05.2017 в 23:30
source

1 answer

0

Here is the client part code that allows you to use templates.

For the example use JQuery and JSRender

var clientesRender = function(data) {
  if (typeof data === "undefined") {
    data = [{
      nombre: 'Jorge',
      telefono: '3057075510'
    }];
  }


  $("#container").html($("#templateClientes").render({
    detalle: data
  }));
};

clientesRender();
<html>

<head>
  <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jsrender/0.9.84/jsrender.min.js"></script>
</head>

<body>

  <div id="container"></div>

  <script id="templateClientes" type="text/x-jsrender">
    <table>
      {{for detalle}}
      <tr>
        <td>
          {{:nombre}}
        </td>
      </tr>
      <tr>
        <td>
          {{:telefono}}
        </td>
      </tr>
      {{/for}}
    </table>
  </script>
</body>

</html>

For the server part you implement a project like Codeigniter (a very simple MVC to start) or simply create the scripts that return the data in JSON format;

Here is an example script in php:

sql = "SQL" //ejemplo frutería: SELECT id_fruta,nombre_fruta,cantidad FROM tabla_fruta;

function connectDB(){

        $server = "SERVER";
        $user = "USER";
        $pass = "PASS";
        $bd = "BD";

    $conexion = mysqli_connect($server, $user, $pass,$bd);

        if($conexion){
            echo 'La conexion de la base de datos se ha hecho satisfactoriamente
';
        }else{
            echo 'Ha sucedido un error inexperado en la conexion de la base de datos
';
        }

    return $conexion;
}

function disconnectDB($conexion){

    $close = mysqli_close($conexion);

        if($close){
            echo 'La desconexion de la base de datos se ha hecho satisfactoriamente
';
        }else{
            echo 'Ha sucedido un error inexperado en la desconexion de la base de datos
';
        }   

    return $close;
}

function getArraySQL($sql){
    //Creamos la conexión con la función anterior
    $conexion = connectDB();

    //generamos la consulta

        mysqli_set_charset($conexion, "utf8"); //formato de datos utf8

    if(!$result = mysqli_query($conexion, $sql)) die(); //si la conexión cancelar programa

    $rawdata = array(); //creamos un array

    //guardamos en un array multidimensional todos los datos de la consulta
    $i=0;

    while($row = mysqli_fetch_array($result))
    {
        $rawdata[$i] = $row;
        $i++;
    }

    disconnectDB($conexion); //desconectamos la base de datos

    return $rawdata; //devolvemos el array
}

        $myArray = getArraySQL($sql);
        echo json_encode($myArray);
?>

Reviewing this information you can do what you want.

Greetings,

    
answered by 26.05.2017 / 23:50
source