lundi 21 mai 2018

Get random results from database and insert in columns

I'm trying to generate 6 column and 9 row long movie grid with random results from database on each user visit. I easily get random 54 results but I'm not able to separate results and insert them in 6 column as 9 row in each column. I was trying to break and continue while loop but no luck. How can I achieve this? My current code looks like this:

    <?php
    $i = 0;
    $sql = "SELECT * FROM Movies ORDER BY RAND() LIMIT 54";
    $gridmovies = $mysqli->query($sql);
    if ($gridmovies->num_rows > 0) {
        while($row = $gridmovies->fetch_assoc()) {
            $i++
    ?>
                <div class="column" style="margin-top: 0px;">
                  <a href="/similar/<?php echo $row['ImdbID']; ?>" class="item-card">
                    <div class="overlay ov-show">
                      <div class="icon"><i class="fa fa-heart"></i></div>
                    </div>
                    <img src="<?php echo $row['PosterURL']; ?>" alt="<?php echo $row['EngTitle']; ?> / <?php echo $row['GeoTitle']; ?>">
                  </a>          
                </div>
                <?php if ($i = 9) {break;} ?>
                <div class="column" style="margin-top: 40px;">
                  <?php continue; ?>
                  <a href="/similar/<?php echo $row['ImdbID']; ?>" class="item-card">
                    <div class="overlay ov-show">
                      <div class="icon"><i class="fa fa-heart"></i></div>
                    </div>
                    <img src="<?php echo $row['PosterURL']; ?>" alt="<?php echo $row['EngTitle']; ?> / <?php echo $row['GeoTitle']; ?>">
                  </a>
                </div>
                <?php if ($i = 18) {break;} ?>
                <div class="column" style="margin-top: 80px;">
                  <?php continue; ?>
                  <a href="/similar/<?php echo $row['ImdbID']; ?>" class="item-card">
                    <div class="overlay ov-show">
                      <div class="icon"><i class="fa fa-heart"></i></div>
                    </div>
                    <img src="<?php echo $row['PosterURL']; ?>" alt="<?php echo $row['EngTitle']; ?> / <?php echo $row['GeoTitle']; ?>">
                  </a>
                </div>
                 <?php if ($i = 27) {break;} ?>
                <div class="column" style="margin-top: 120px;">
                  <?php continue; ?>
                  <a href="/similar/<?php echo $row['ImdbID']; ?>" class="item-card">
                    <div class="overlay ov-show">
                      <div class="icon"><i class="fa fa-heart"></i></div>
                    </div>
                    <img src="<?php echo $row['PosterURL']; ?>" alt="<?php echo $row['EngTitle']; ?> / <?php echo $row['GeoTitle']; ?>">
                  </a>
                </div>
                <?php if ($i = 36) {break;} ?>
                <div class="column" style="margin-top: 160px;">
                  <?php continue; ?>
                  <a href="/similar/<?php echo $row['ImdbID']; ?>" class="item-card">
                    <div class="overlay ov-show">
                      <div class="icon"><i class="fa fa-heart"></i></div>
                    </div>
                    <img src="<?php echo $row['PosterURL']; ?>" alt="<?php echo $row['EngTitle']; ?> / <?php echo $row['GeoTitle']; ?>">
                  </a>
                </div>
                <?php if ($i = 45) {break;} ?>
                <div class="column" style="margin-top: 200px;">
                  <?php continue; ?>
                  <a href="/similar/<?php echo $row['ImdbID']; ?>" class="item-card">
                    <div class="overlay ov-show">
                      <div class="icon"><i class="fa fa-heart"></i></div>
                    </div>
                    <img src="<?php echo $row['PosterURL']; ?>" alt="<?php echo $row['EngTitle']; ?> / <?php echo $row['GeoTitle']; ?>">
                  </a>
                </div>
    <?php
        } $gridmovies->free();
    } else {
      echo 'error';
    }
    ?>




Aucun commentaire:

Enregistrer un commentaire