Use while to show results from three columns

Asked

Viewed 71 times

0

I want to place the three columns to show your results, all within the same WHILE, as I should 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>';
    }
}
  • Isn’t there a relationship between the tables Movies , series and anime ? If it exists because it is using three queries to the database could mount only one query.

1 answer

0


There are some ways to do this. The problem in this case seems to be data modeling.

If possible post the structure of your database, so we can analyze and recommend a particular shape that is better for your problem.

One of the ways it will serve is by using a function to display the data.

For this it is necessary to standardize the captured information with the query, for that 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>';
        }
    }
}

Note that I did not use the column name "movie_key", "serie_key" etc. I only used the alias that I created with the query previous and why the pattern is so important.

To display simply call the function displayHtml. In this function it is necessary to pass the result of $db->query and the "guy". Ex:

dislplayHtml($movies, "Filmes");
dislplayHtml($movies, "Séries");
dislplayHtml($movies, "Anime");
  • I’ll test baby, if it works out, sort.

  • I tested the script, but it was the same as I had done before, only more organized, I liked you, I need your help in another detail now within this script, if you can add me skype, I’m grateful: lenux.games

Browser other questions tagged

You are not signed in. Login or sign up in order to post.