Jump to content

Recommended Posts

I'm trying  to create a "slideshow" type interface for a client and I'm wondering if anyone can suggest a way to loop through the results of a mysql query row by row, so that I can always reference the next record, or the previous record from the current record that's being called. I can't figure out how to do this without re-querying my database each time. My concern is that if the row id's or any other incremental row counter I use aren't always in consecutive order, it will break the application. Hoping someone can help.

 

Thanks,

Dave

Link to comment
https://forums.phpfreaks.com/topic/155444-loop-through-mysql-result-incrementally/
Share on other sites

if you want to do this with PHP, and only want to show 1 entry at a time, and go through the entire database you are going to have to requery the server every time.

 

However, with some javascript, you could probably do what you want fairly easily

Well, I ended up getting my "slideshow" to work on a click by click basis, however it only works on records moving forward. I'm not quite sure how to allow the user to go back to the previous record from the record they're currently viewing. I call this simple loop from an ajax method which provides me with my "next" record. I'm wondering if there is a way to use the counter I've created to allow navigation forward and backwards also.

 

        while($myrow = mysql_fetch_array($result)) {

                print $myrow[1]."/".$myrow[0]."^";

                print $myrow[2]."^";

                print $count."^";

                $count++;

        }

 

I have an event handler that calls a function to retrieve the data upon click, but it only works one way right now.

this is your incrementation code yes:

while($myrow = mysql_fetch_array($result)) {
                print $myrow[1]."/".$myrow[0]."^";
                print $myrow[2]."^";
                print $count."^";
                $count++;
        }

to make a decremental, try something like


                $count--;

or something like that. you may have to do a little more than that though. Post the entire code that goes through the menu if you can

The more I think about it, the more I realize the snippet i posted earlier does not but query the database and get the results. This function is what actually displays the results:

 

function displayData(response) {

  var response = response.split("^");

  var x = 0;

  var y = 0;

  var itemcount = response.length-1;

  var bannerImg = new Array();

  var bannerTxt = new Array();

  var bannerCount = new Array();

 

while (y < itemcount) {

  bannerImg[x] = response[y];

  y++;

  bannerTxt[x] = response[y];

  y++;

  x++;

}

var count = bannerImg.length;

 

if (newBanner < count) {

  document.getElementById("ss_image").src = bannerImg[newBanner];

  document.getElementById("desc").innerHTML = bannerTxt[newBanner];

  newBanner++;

}

else {

  newBanner = 0;

  document.getElementById("ss_image").src = bannerImg[newBanner];

  document.getElementById("desc").innerHTML = bannerTxt[newBanner];

  newBanner++;

}

}

 

Now what I need to know how to do is to decrement the variable newBanner when a user clicks on "previous" in the next show, since that is what is displaying the results of the particular mysql row.

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.