Find a table with the name inside another

0

Sorry for my inexperience but I do not have a concrete idea of how to do the following. In a database table (project_snippets) in a table (code) I have four columns: id, code, author & link and id is the primary key. in the column code I want to enter several languages, until now PHP & Javascript but I wanted each code to have a table that separates sets where in turn has another, it would look something like

so far I was able to do the following.

try {
    $conexion = new PDO('mysql:host=localhost;dbname=proyecto_snippets','root','');
} catch (PDOException $e) {
    echo 'Error: ' . $e->getMessage();
    die();
}
$statement = $conexion->prepare('SELECT * FROM code');
$statement->execute();
$codigos = $statement->fetchAll();
foreach ($codigos as $code) {
  echo $code['id'] . ' - ' . $code['code'] . '<br>';
}

require 'views/index.view.php';

and the view would look like this

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Snippets</title>
<link rel="stylesheet" href="css/master.css">
</head>
<body>
<div class="contenedor">
    <?php foreach ($codigos as $code): ?>
    <div class="title">
        <h1 class="title"><?php echo $code['code']; ?></h1>
        <sub class="link">
            <a href="<?php echo $code['link']; ?>" class="link" target="_blank">
                <?php echo $code['autor']; ?>
            </a>
        </sub>
        <button onclick="changeType('<?php echo strtolower($code['code']); ?>')" class="type" name="<?php echo strtolower($code['code']); ?>">VSCode</button>
    </div>
    <?php endforeach; ?>
</div>
<script src="js/master.js"></script>
</body>
</html>

and it shows me this:

If someone has enough pascience to tell me or tell me where to find an answer. Honestly, it's not urgent or necessary, I just do it to practice.

    
asked by Padrón Rocha 10.11.2018 в 20:06
source

1 answer

1

I think the shortest solution would be to think about your table with a typical structure:

id | lenguaje | seccion  | titulo      | codigo
-------------------------------------------------
1  | PHP      | PDO      | Conectar    | $PDO = new PDO(...)
2  | PHP      | Array    | array_merge | array_merge($arr1, $arr2)
3  | PHP      | Iterable | Iterar      | forEach($arr as $key => $value) {...
4  | JS       | DOM      | Select      | document.querySelector(...)
5  | JS       | Event    | Listener    | element.addListener(...)
6  | JS       | function | declaration | function decirHola() {...}
7  | JS       | function | expression  | var decirHola = function () {...}

Again, this is the short solution. Later I will say why.

With a table like this you can select the same as now and then use some business logic to infer language and section changes. For example:

$query = 'SELECT * FROM code ORDER BY lenguaje, seccion, titulo;';

...

$codigos = $statement->fetchAll();
$lenguaje = null;
$seccion = null;
foreach ($codigos as $code) {

     //  Si cambié de lenguaje, viene otro contenedor
     if($code['lenguaje']!==$lenguaje) {
          $lenguaje=$code['lenguaje'];
         echo '<div class="title">';
         echo '<h1 class="title">'. $code['lenguaje'] .'</h1>';
         echo '</div>';
     }

    //  Si cambié de sección, viene un subcontenedor
     if($code['seccion']!==$seccion) {
          $seccion=$code['seccion'];
         echo '<div class="seccion">';
         echo '<h2 class="seccion">'. $code['seccion'] .'</h2>';
         ecoh '</div>';
     }
     echo '<div class="snippet">';
     echo '<h4>'.$code['titulo'].'</h4>';
     echo '<pre>'.$code['codigo'].'</pre>';
     echo '</div>';

}

Obviously you have to change the classes and layout according to your styles, but basically you get all the rows sorted by language and then by section. If the language of a row is different from the previous one, a main container must be created. (This is always true in the first row, because before you declared that the language was NULL). With the section the same thing happens. Since they come in order, if there was a section change it is necessary to create a subcontainer. And within each subcontainer go the snippets with their respective title and code.

What would be the elaborate solution?

First, in the proposed solution, the main containers do not contain the subcontainers, nor these to their snippets. They are simply one more element and are stacked online. It is not difficult to introduce logic to achieve this but the example becomes very dirty.

Second, a "sheet" type table is never really flexible. If you want to change something you end up adding columns, it does not have referential integrity (then you could misspell a language and create a new section unintentionally). There should be a language entity capable of having many sections, an entity section that belongs to a language and can have many snippets, and a snippet entity that belongs to a section. That in broad strokes.

Third, in my suggestion you are diagramming in PHP, and that is suboptimal in our time. The backend should deliver a nested json and the frontend consume it to draw their nodes in the DOM, but as you said you did not use Ajax, it is better not to get sophisticated and render with PHP.

Finally, I've never liked the idea of collating PHP and HTML in the same file (opening and closing the <?php tag). Semi-intelligent IDEs and publishers are not able to indent the code well and it's easy to miss syntax errors when you're doing this.

    
answered by 10.11.2018 / 21:07
source