Skip to content

Instantly share code, notes, and snippets.

@yoander
Last active Aug 2, 2018
Embed
What would you like to do?
Generators
<?php
function connect() {
$link = @mysqli_connect('localhost:2483', 'root', 'root', 'world');
if ($error = mysqli_connect_errno()) {
return sprintf("Error de conexión: #%s - %s\n", $error, mysqli_connect_error());
}
mysqli_set_charset($link, 'utf8');
return $link;
}
function get_countries_details_traditional_style($link) {
$query = "SELECT * FROM country LIMIT 500";
$rows = [];
if ($result = mysqli_query($link, $query)) {
/**
* Obtener filas en un arreglo asociativo
*/
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
/**
* Libera la memoria de los resultados asociados con la sentencia
*/
mysqli_free_result($result);
}
return $rows;
}
function get_countries_details_using_generators($link) {
$query = "SELECT * FROM country LIMIT 500";
if ($result = mysqli_query($link, $query)) {
/**
* Obtener filas en un arreglo asociativo
*/
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
dump_to_console(sprintf('BD > Obteniendo detalles de la fila: %u', ++$i));
yield $row;
}
/**
* Libera la memoria de los resultados asociados con la sentencia
*/
mysqli_free_result($result);
}
}
/**
* Cierro la conexión
*/
function close_connection($link) {
mysqli_close($link);
}
/*
* Muestra un mensaje en la consola del Browser: Chromium o FF
*/
function dump_to_console($message) {
echo "<script>console.log('$message');</script>";
}
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="./css/main.css">
</head>
<body>
<?php require_once __DIR__ . '/db.php' ?>
<?php if (!(($link = connect()) instanceof mysqli)): ?>
<pre><?php die($link); ?></pre>
<?php endif ?>
<table class="flat-table">
<tbody>
<tr>
<th>No</th>
<th>Código</th>
<th>Nombre</th>
<th>Continente</th>
<th>Región</th>
</tr>
<?php $i = 0 ?>
<?php foreach (get_countries_details_traditional_style($link) as $detail): ?>
<?php dump_to_console(sprintf('HTML > Volcando detalles de la fila: %u', ++$i)) ?>
<tr>
<td><?php echo $i ?></td>
<td><?php echo $detail['Code'] ?></td>
<td><?php echo $detail['Name'] ?></td>
<td><?php echo $detail['Continent'] ?></td>
<td><?php echo $detail['Region'] ?></td>
</tr>
<?php endforeach ?>
</tbody>
<tfoot>
<tr>
<td colspan="5">Memoria consumida: <?php echo memory_get_usage(true) ?> B</td>
</tr>
<tr>
<td colspan="5">CSS Style: <a href="https://codepen.io/njessen/pen/naLCv">https://codepen.io/njessen/pen/naLCv</a></td>
</tr>
</tfoot>
</table>
</body>
</html>
.flat-table {
display: block;
font-family: sans-serif;
-webkit-font-smoothing: antialiased;
font-size: 115%;
overflow: auto;
width: auto;
}
th {
background-color: rgb(112, 196, 105);
color: white;
font-weight: normal;
padding: 20px 30px;
text-align: center;
}
td {
background-color: rgb(238, 238, 238);
color: rgb(111, 111, 111);
padding: 20px 30px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment