canty Posted November 18, 2010 Share Posted November 18, 2010 This is a pain. It seems totally logical to me and I had it working before I accidentally saved over the working version (major boo boo). This is two code snippets, first one is how I want it to work - One Query and the loop through results. Second does work, but then its going to have to run mysql 60 times, which could get slow as the site gets more use. $results = mysql_query($query); $response .= "<table><tr>"; if($nit == 0){ for($i = 0; $i < 60; $i++){ $dateCompare = date("Y-m-d", mktime(0,0,0,date("m"),date("d")+$i,date("Y"))); $dateForString = date("D d/m/Y", mktime(0,0,0,date("m"),date("d")+$i,date("Y"))); $response .= "<td height='350px' style='color:white; background-color:grey;'>$dateForString</td>"; while($row = mysql_fetch_array($results)){ $id = $row['id']; $poster = $row['poster']; $date = $row['date']; $response .= "$date : $dateCompare<br>"; if($date == $dateCompare){ $response .= "<td><a href='fullinfo.php?selected_id=$id'><img src='$poster' height='350px' /></a></td>"; } } } } FOLLOWED BY THE SECOND $response .= "<table><tr>"; if($nit == 0){ for($i = 0; $i < 60; $i++){ $dateCompare = date("Y-m-d", mktime(0,0,0,date("m"),date("d")+$i,date("Y"))); $dateForString = date("D d/m/Y", mktime(0,0,0,date("m"),date("d")+$i,date("Y"))); $response .= "<td height='350px' style='color:white; background-color:grey;'>$dateForString</td>"; $results = mysql_query($query); while($row = mysql_fetch_array($results)){ $id = $row['id']; $poster = $row['poster']; $date = $row['date']; if($date == $dateCompare){ $response .= "<td><a href='fullinfo.php?selected_id=$id'><img src='$poster' height='350px' /></a></td>"; } } } } First one results in -it seems - to only go through the while loop once, the first time. So out put is todays date, for the number of results from the database. The second works fine, prints out in order and matches up. I just don't like its inefficiency. Thanks Canty Quote Link to comment https://forums.phpfreaks.com/topic/219121-while-mysql_fetch_array-within-for-loop/ Share on other sites More sharing options...
AbraCadaver Posted November 18, 2010 Share Posted November 18, 2010 I can't really see what you're doing to well enough to optimize it, but in the current configuration of the first example you need to put mysql_data_seek($results, 0) either right before or right after the while loop. Once you fetch all records from a result the pointer is at the end so you have to reset the pointer to the beginning. Quote Link to comment https://forums.phpfreaks.com/topic/219121-while-mysql_fetch_array-within-for-loop/#findComment-1136341 Share on other sites More sharing options...
canty Posted November 18, 2010 Author Share Posted November 18, 2010 Right, its one of those. Came back to it and can't quite tell what happened. But now it works. Damned if I know! So the one I wanted to work now works. I think I just haven't been to sleep in a while. Quote Link to comment https://forums.phpfreaks.com/topic/219121-while-mysql_fetch_array-within-for-loop/#findComment-1136350 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.