0

I need to optimize this code:

<div class="temporadas">
    <?php $i = 0;
    foreach ($temporadas as $temporada) {
        $i++; ?>
        <div data-id="<?= $temporada['temporada']; ?>" class="temporadaBox" onclick="temporadaTab(<?= $temporada['temporada']; ?>)">
            <span class="temporada">
                Temporada <?= $temporada['temporada']; ?>
                <?= (!empty($temporada['nome']) ? '- '.$temporada['nome'].'' : ''); ?>                          
            </span><img id="arrow<?= $temporada['temporada']; ?>" <?= (($i == 1) ? 'class="arrowDown"' : 'class="arrowLeft"'); ?>>
        </div>          
        <div class="temporadaContainer" id="temporadaContainer<?= $temporada['temporada']; ?>" <?= (($i == 1) ? '' : 'style="display:none;"'); ?>>
            <?php foreach ($episodios as $episodio) {
                if ($episodio['temporada'] == $temporada['temporada']) { ?>           
                    <div class="item" data-id="<?= $episodio['episodio']; ?>">
                        <a href="<?= $config['site_url']; ?>/<?= (($temporada['hidden_temporada'] == 1) ? $temporada['url_anime'] : $temporada['url_temporada']); ?>/episodio-<?= encodeInt($episodio['episodio']); ?>/">
                            <img src="<?= $config['site_url']; ?>/images/background_episodios/<?= $episodio['url_anime']; ?>/small/t=<?= $episodio['temporada']; ?>&ep=<?= $episodio['episodio']; ?>.jpg"/>
                            <div class="blank"></div>
                            <div class="titulo">
                                <span class="description"><u>Episódio <?= $episodio['episodio']; ?></u><?= ((!empty($episodio['titulo'])) ? " - ".$episodio['titulo']."" : ""); ?></span>
                                <span class="date"><?= ucfirst(strftime('%b. %d, %Y', $episodio['data'])); ?></span>
                            </div>
                        </a>
                        <div class="clearfix"></div>
                    </div>
                <?php } ?>
            <?php } ?>
        </div>
    <?php } ?>
</div>

Explaining the code, $temporadas have about 20 rows, and $episodios have about 1000 rows.. I need to put each episodio with temporadaID inside current temporada container..

The problem as you can see, is that if we have 20 rows of $temporada and 1000 rows of $episodio, we will loop 20,000 times.

I though in leave $episodio foreach outside $temporada, and organize the episodios with Javascript after complete the loop, however i have no idea how to do it..

Someone could help me? I appreciate, thank you!!

**UPDATE with the data that i get from MySQL:

$episodios = apcu_fetch("anime_episodios_".$get_anime['id']."");
if(!$episodios) {
    $episodios = $SQL->query("SELECT * FROM episodios WHERE url_anime = '".$get_anime['url_nome']."' order by episodio asc")->fetch_all(MYSQLI_ASSOC);
    apcu_store("anime_episodios_".$get_anime['id']."", $episodios, 0);
}

$temporadas = apcu_fetch("anime_temporadas_".$get_anime['id']."");
if(!$temporadas) {
    $temporadas = $SQL->query("SELECT * from temporadas WHERE url_anime = '".$get_anime['url_nome']."' order by temporada asc")->fetch_all(MYSQLI_ASSOC);
    apcu_store("anime_temporadas_".$get_anime['id']."", $temporadas, 0);
}
Domenyc
  • 11
  • 2
  • Where is the source data coming from originally - database? – Professor Abronsius Dec 04 '21 at 14:54
  • 1
    Probably relevant: [What is the difference between client-side and server-side programming?](https://stackoverflow.com/q/13840429) – VLAZ Dec 04 '21 at 14:54
  • [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/a/284237/9078341) – Randy Casburn Dec 04 '21 at 15:07
  • @ProfessorAbronsius i have updated the question with the source data that i get from Mysql. – Domenyc Dec 04 '21 at 15:11
  • without looking too closely at this and without seeing the structure of the two database tables you can see that there is a common column `url_anime` used in both queries which suggests that a `join` between the tables is possible and would save a lot of time and effort. – Professor Abronsius Dec 04 '21 at 19:39
  • 20,000 lines(?) is an unreasonably large amount of data to subject a user to. – Rick James Dec 05 '21 at 23:04

0 Answers0