Jump to content

Gallery loop didn't work


AzizEFT

Recommended Posts

Dear all,

Help required in code: I have 2 tables (sites and photo_gallery) in my database. First I display my posts from database, then it goes to my viewpost page.

Everything work file but in gallery portion my loop didn't work.

IN TOP of the page I use this code to run all viewpost pages.

if(isset($_GET['id'])) {
    require_once("_includes/LocalHost.php");
    $id = mysqli_real_escape_string($db, $_GET['id']);
    
    $sql = "SELECT * FROM sites, photo_gallery WHERE gallery_con = photo_id AND id='$id' ";
    $result= mysqli_query($db, $sql) or die("Bad Query: $sql");
    $row = mysqli_fetch_array($result);
}
In gallery portion

<?php

if($result->num_rows > 0){
    while($row = $result->fetch_assoc()) {
        $title = $row["title_post"];
        $imageURL = 'images/'.$row["photographs"];
        $imageThumbURL = 'images/thumb_'.$row["photographs"];

?>
  <a href="<?php echo $imageURL; ?>" class="image" data-fancybox="images">
      <img src="<?php echo $imageThumbURL; ?>" alt="<?php echo $title; ?>" height="200px" />
  </a>

<?php }
}
?>

1 to 15 images with every post and it will display only one 2nd image. 

Thank you for the help.

Link to comment
Share on other sites

4 minutes ago, AzizEFT said:

 $result= mysqli_query($db, $sql) or die("Bad Query: $sql");
    $row = mysqli_fetch_array($result);

I don't know if it's your only problem but the fetch_array() call after the query is effectively throwing away the first row. You don't output anything until you read the second row in the while() statement at the start of the loop.

Link to comment
Share on other sites

On 9/14/2019 at 9:03 AM, AzizEFT said:

1 to 15 images with every post and it will display only one 2nd image.

Barand already explained why the first image is missing. Since it sounds like there should be more than 2 images, did you try displaying the value of $result->num_rows? That will tell you the number of rows actually returned. If you're not getting the expected value, the query may be the issue.

Edited by cyberRobot
fixed typo
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.