Use while to display three-column results

0

I want to put the three columns to show your results, all within the same WHILE, how should I do?

$movies = $db->query('SELECT * FROM movies AS M, highlights AS H, images AS I 
WHERE (M.movie_key = H.featured_key AND I.images_key = M.movie_key) AND (I.images_type = H.featured_type) AND H.featured_status = "ativo" 
ORDER BY H.featured_id DESC LIMIT 5');

$series = $db->query('SELECT * FROM series AS S, highlights AS H, images AS I 
WHERE (S.serie_key = H.featured_key AND I.images_key = S.serie_key) AND (I.images_type = H.featured_type) AND H.featured_status = "ativo" 
ORDER BY H.featured_id DESC LIMIT 5');

$animes = $db->query('SELECT * FROM animes AS A, highlights AS H, images AS I 
WHERE (A.anime_key = H.featured_key AND I.images_key = A.anime_key) AND (I.images_type = H.featured_type) AND H.featured_status = "ativo" 
ORDER BY H.featured_id DESC LIMIT 5');

if($movies->rowCount() != 0){
    while($database = $movies->fetch())
    {
        echo '<article class="item" id="post-'.$database['movie_key'].'">
            <div class="image">
                <a href="/assistir/filme/'.$database['movie_url'].'">
                    <img src="'.$database['images_slide'].'" alt="'.$database['movie_name'].'">
                </a>
                <a href="/assistir/filme/'.$database['movie_url'].'">
                    <div class="data">
                        <h3 class="title">'.$database['movie_name'].'</h3>
                        <span>'.$database['movie_year'].'</span>
                    </div>
                </a>
                <span class="item_type">FILME</span>
            </div>
        </article>';
    }
}

if($series->rowCount() != 0){
    while($database = $series->fetch())
    {
        echo '<article class="item" id="post-'.$database['serie_key'].'">
            <div class="image">
                <a href="/assistir/serie/'.$database['serie_url'].'">
                    <img src="'.$database['images_slide'].'" alt="'.$database['serie_name'].'">
                </a>
                <a href="/assistir/serie/'.$database['serie_url'].'">
                    <div class="data">
                        <h3 class="title">'.$database['serie_name'].'</h3>
                        <span>'.$database['serie_year'].'</span>
                    </div>
                </a>
                <span class="item_type">SÉRIE</span>
            </div>
        </article>';
    }
}

if($animes->rowCount() != 0){
    while($database = $animes->fetch())
    {
        echo '<article class="item" id="post-'.$database['anime_key'].'">
            <div class="image">
                <a href="/assistir/anime/'.$database['anime_url'].'">
                    <img src="'.$database['images_slide'].'" alt="'.$database['anime_name'].'">
                </a>
                <a href="/assistir/anime/'.$database['anime_url'].'">
                    <div class="data">
                        <h3 class="title">'.$database['anime_name'].'</h3>
                        <span>'.$database['anime_year'].'</span>
                    </div>
                </a>
                <span class="item_type">ANIME</span>
            </div>
        </article>';
    }
}
    
asked by anonymous 18.01.2018 / 02:15

1 answer

0

There are a few ways to do this. The problem here seems to be data modeling.

  

If possible, post the structure of your database so we can review and recommend a form that is best for your problem.

One way to do this is to use a function to display the data.

For this it is necessary to standardize the captured information with query , for this you should use the ALIAS . This way you will create shortcuts / nicknames following a pattern that can be used in the function.

$movies = $db->query('SELECT
    'movie_key' AS 'key',        /* Aqui eu informo que quero pegar o valor de "movie_key" e retornar em uma coluna chamada "key" */
    'movie_url' AS 'url',        /* Aqui eu informo que quero pegar o valor de "movie_url" e retornar em uma coluna chamada "url" */
    'images_slide' AS 'slide', 
    'movie_name' AS 'name', 
    'movie_year' AS 'year'
FROM 
    movies AS M, 
    highlights AS H, 
    images AS I 
WHERE 
    (M.movie_key = H.featured_key AND I.images_key = M.movie_key) 
    AND (I.images_type = H.featured_type) 
    AND H.featured_status = "ativo" 
ORDER BY H.featured_id DESC 
LIMIT 5');
  

Follow this pattern in all other tables.

Now let's define the function that will be responsible for displaying the data.

function dislplayHtml($data, $type) {
    if($data->rowCount() != 0){
        while($database = $data->fetch())
        {
            //Gera a URL
            $url = sprintf('/assistir/%s/%s',

                //Traduz caracteres especiais como áéíóú
                strtolower(preg_replace("/\W/", "", iconv("UTF-8", "ASCII//TRANSLIT", $type))),

                //Concatena a URL
                $database['url']);

            echo '<article class="item" id="post-'.$database['key'].'">
                <div class="image">
                    <a href="'.$url.'">
                        <img src="'.$database['slide'].'" alt="'.$database['name'].'">
                    </a>
                    <a href="'.$url.'">
                        <div class="data">
                            <h3 class="title">'.$database['name'].'</h3>
                            <span>'.$database['year'].'</span>
                        </div>
                    </a>
                    <span class="item_type">'.strtoupper($type).'</span>
                </div>
            </article>';
        }
    }
}

Please note that I did not use the name of the "movie_key", "serial_key", etc. columns. I used only the% w_that I created with alias earlier and so the pattern is so important.

To display, just call the query function. In this function it is necessary to pass as a parameter the result of displayHtml and the "type" . Ex:

dislplayHtml($movies, "Filmes");
dislplayHtml($movies, "Séries");
dislplayHtml($movies, "Anime");
    
18.01.2018 / 10:39