mercredi 30 décembre 2015

Get a random image from a database along with its caption in PHP

I need to retrieve a random image and the caption that goes along with it from a database. There is a caption column and a photo column.

Here is my (messy) code:

      $getThis = $mysqli->query("SELECT * FROM `k_pictures` ORDER BY RAND() LIMIT 1");
      $photo = "";
      while($row = $getThis->fetch_assoc()) {
        $photo .= $row['photo'];
        $photoName = $row['fileName'];
        echo "<tr>";
        echo "<td><img src='../p/$photoName' height='500px' width='500px'></td>";
        echo "</tr>\n";
        $getCaption = $mysqli->query("SELECT * FROM `k_pictures` WHERE fileName = '$photoName'");
        while($retrieve = $getCaption->fetch_array()) {
          if(isset($_POST['submit'])) {
            require "caption.php";
            insertCaption($photoName, $_POST['caption']);
            header('Location: ../success/');
            $getCapt = $mysqli->query("SELECT * FROM `k_captions` WHERE fileName = '$photoName'");
            $captionRetrieved = $getCapt->fetch_array();
            echo "Top rated caption: " . $captionRetrieved['caption'];
          }
        }
      }




Aucun commentaire:

Enregistrer un commentaire