dskurtzjr Posted April 24, 2009 Share Posted April 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/155444-loop-through-mysql-result-incrementally/ Share on other sites More sharing options...
mikesta707 Posted April 24, 2009 Share Posted April 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/155444-loop-through-mysql-result-incrementally/#findComment-817960 Share on other sites More sharing options...
dskurtzjr Posted April 24, 2009 Author Share Posted April 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/155444-loop-through-mysql-result-incrementally/#findComment-818036 Share on other sites More sharing options...
mikesta707 Posted April 24, 2009 Share Posted April 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/155444-loop-through-mysql-result-incrementally/#findComment-818062 Share on other sites More sharing options...
dskurtzjr Posted April 24, 2009 Author Share Posted April 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/155444-loop-through-mysql-result-incrementally/#findComment-818214 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.