Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1021591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:20:00+00:00 2026-05-16T11:20:00+00:00

I have a photo gallery that has a previous and a Next link. When

  • 0

I have a photo gallery that has a previous and a Next link. When it gets to the last image it stops there. I want it to start over at the first picture. The function is a for loop and I think it should be a while loop and some things changed to make it an infinite loop. I just don’t know how to convert this function. Still not that strong in PHP. Any help is appreciated.

$query  = "SELECT pho_id FROM album_photos WHERE alb_id='$alb_id' ORDER by srt_id ASC";
    $ret    = mysql_query($query);
    $num    = mysql_numrows($ret);
    for ($i = 0; $i < $num; $i++)
    {
        $row = trim(mysql_result($ret, $i));
        if ($row == $pho_id)
        {
                        $cur = $i;
            $forw = @trim(mysql_result($ret, ($i+1))) or $forw = NULL;
            $back = @trim(mysql_result($ret, ($i-1))) or $back = NULL;
        }

    }
$query = "SELECT * FROM photos WHERE pho_id='$back'";
        $rets  = mysql_query($query);
        $back_good = mysql_numrows($rets);

        $query = "SELECT * FROM photos WHERE pho_id='$forw'";
        $rets  = mysql_query($query);
        $forw_good = mysql_numrows($rets);

    $back = "photo-$per_id-$back-$alb_id.html";
    $forw = "photo-$per_id-$forw-$alb_id.html";

    if (strstr($back, 'pho_id=&') || $back_good == 0) { $back = NULL; }
    if (strstr($forw, 'pho_id=&') || $forw_good == 0) { $forw = NULL; }
    $ret = array();

    $ret['back'] = $back;
    $ret['next'] = $forw;
    $cur++;
    $ret['viewing']   = "Photo $cur of ".($num);

    if($ret['back'] == NULL){
    $ret['back'] = "";
    $spacer = str_repeat('&nbsp;',12);
    }
    if($ret['next'] == NULL){
    $ret['next'] = "";
    $spacer = str_repeat('&nbsp;',12);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:20:01+00:00Added an answer on May 16, 2026 at 11:20 am

    I suggest modifying your loop’s if content like this.

    $cur = $i;
    
    //compute the indexes of the next and previous elements
    //the next item is the first if we are at the end.
    $forward_index = $i == $num - 1 ? 0 : $i + 1; 
    //the previous one is the last if we are at the beginning.
    $backward_index = $i === 0 ? $num - 1 : $i - 1;
    
    //fetch the elements
    $forw = @trim(mysql_result($ret, $forward_index));
    $back = @trim(mysql_result($ret, $backward_index));
    
    //exit the for loop
    break;
    

    It is true you could turn the for loop into a while loop. However, there is no absolute need for this. I added a break statement in order to quit as soon as you locate the element you want to load.

    Edit: fixed the code as per the OP comment. Silly mistake

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have photo gallery code that does image re-sizing and thumbnail creation. I use
I know it sounds weird. I have a photo gallery that saves image info(url,
I have a Gallery application that has Album and Photo models. I have the
I have a PHP photo gallery that reads the GPS coordinates from images. I'd
I have a photo gallery I would like that every time a user does
I have a simple form that generates a new photo gallery, sending the title
I have created a rather simple photo gallery that I wish to apply PayPal
I have a photo gallery page that shows a series of thumbnails that are
I have a listview that has a photograph from the gallery for each entry
My rails application has two models, gallery and photo. Galleries have many photos and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.