Presto-X Posted May 4, 2010 Share Posted May 4, 2010 Hey guys, I think I have a good one, I'm trying to display two while loops on a page, the first is used for a google map markers, the next while displays the text results below the map. Now on to the problem there are a total of 4 results, the first 3 get diplayed in the map and the last one gets diplayed in the text results below the map, they should be display 4 markers in the map, and 4 text results below the map but it's spliting the results some how and it's driving me crazy, can you guys please take a look at my code and see if you can tell what the freak I'm doing wrong. $query = sprintf("SELECT *, ( 3959 * acos( cos( radians('%s') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( latitude ) ) ) ) AS distance FROM stores WHERE approved = '1' AND status = '1' HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20", mysql_real_escape_string($center_lat), mysql_real_escape_string($center_lng), mysql_real_escape_string($center_lat), mysql_real_escape_string($radius)); $results = mysql_query($query) or die(mysql_error()); do { $distance = explode('.', $row['distance']); $gm->SetAddress($row['address'].' '.$row['city'].' '.$row['state'].' '.$row['zip']); $gm->SetInfoWindowText($row['name']); } while ($row = mysql_fetch_assoc($results)); do { $distance = explode('.', $row_location['distance']); echo ' <div class="locationName">'.$row_location['name'].'</div>'."\n"; } while ($row_location = mysql_fetch_assoc($results)); Link to comment https://forums.phpfreaks.com/topic/200617-two-while-loops-on-the-same-page-using-mysql_fetch_assoc-not-working/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 4, 2010 Share Posted May 4, 2010 do{}while() loops are almost never used because they require extra code over using just a while(){} loop, because you must setup data before the start of the loop. In your code, where is $row being set at the first time through the loop? You should also be developing and debugging your code on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini (so that fatal parse errors are displayed too) so that php will help you. You will save a ton of time. The uninitialized $row variables would have produced run time errors to alert you to the fact that they did not exist the first time through the loop. Link to comment https://forums.phpfreaks.com/topic/200617-two-while-loops-on-the-same-page-using-mysql_fetch_assoc-not-working/#findComment-1052806 Share on other sites More sharing options...
Presto-X Posted May 4, 2010 Author Share Posted May 4, 2010 Ahh good call, I will give that a shot. Link to comment https://forums.phpfreaks.com/topic/200617-two-while-loops-on-the-same-page-using-mysql_fetch_assoc-not-working/#findComment-1052812 Share on other sites More sharing options...
Presto-X Posted May 4, 2010 Author Share Posted May 4, 2010 Ok I gave taht a shot, I had 8 errors or so, got them all fixed: Here is my corrent code: $query = sprintf("SELECT *, ( 3959 * acos( cos( radians('%s') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( latitude ) ) ) ) AS distance FROM stores WHERE approved = '1' AND status = '1' HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20", mysql_real_escape_string($center_lat), mysql_real_escape_string($center_lng), mysql_real_escape_string($center_lat), mysql_real_escape_string($radius)); $results = mysql_query($query) or die(mysql_error()); while ($row_location = mysql_fetch_assoc($results)){ $distance = explode('.', $row_location['distance']); $gm->SetAddress($row_location['address'].' '.$row_location['city'].' '.$row_location['state'].' '.$row_location['zip']); $gm->SetInfoWindowText($row_location['name'].' <small>('.$distance[0].' miles)</small><br>'.$row_location['address'].'<br>'.$row_location['city'].', '.$row_location['state'].' '.$row_location['zip'].'<br>'.'Phone: '.$row_location['phone'].'<br>Fax: '.$row_location['fax'].'<br>'); } while ($row_location = mysql_fetch_assoc($results)){ $distance = explode('.', $row_location['distance']); echo ' <div class="locationName">'.$row_location['name'].' <small>('.$distance[0].' miles)</small></div>'."\n"; echo ' <div class="locationAddress">'.$row_location['address'].'</div>'."\n"; echo ' <div class="locationCityStateZip">'.$row_location['city'].', '.$row_location['state'].' '.$row_location['zip'].'</div>'."\n"; echo ' <div class="locationPhone">Phone: '.$row_location['phone'].'</div>'."\n"; echo ' <div class="locationFax">Fax: '.$row_location['fax'].'</div>'."\n"; } Link to comment https://forums.phpfreaks.com/topic/200617-two-while-loops-on-the-same-page-using-mysql_fetch_assoc-not-working/#findComment-1052826 Share on other sites More sharing options...
PFMaBiSmAd Posted May 4, 2010 Share Posted May 4, 2010 I don't see a question, problem, or error mentioned, but if it was something like "The second while() loop is being skipped, how do I reset the result resource row pointer back to the beginning so that the second loop will reuse the result from the query?", the answer would be to see this link - mysql_data_seek Link to comment https://forums.phpfreaks.com/topic/200617-two-while-loops-on-the-same-page-using-mysql_fetch_assoc-not-working/#findComment-1052827 Share on other sites More sharing options...
Presto-X Posted May 4, 2010 Author Share Posted May 4, 2010 Hey PFMaBiSmAd, Thanks for your patience with me, yes in my last post I should have noted that I was having the same outcome as before, I did not know about using mysql_data_seek, as I have not needed this before, thanks to your help and patience I was able to get it working with adding mysql_data_seek($results, 0); after the first while loop. I'm guessing this is how it this is how it's done. Link to comment https://forums.phpfreaks.com/topic/200617-two-while-loops-on-the-same-page-using-mysql_fetch_assoc-not-working/#findComment-1052831 Share on other sites More sharing options...
roopurt18 Posted May 4, 2010 Share Posted May 4, 2010 I don't understand why you're using two loops. Why not use just one? Is your page XHTML? If yes, then why aren't you closing your br tags? Link to comment https://forums.phpfreaks.com/topic/200617-two-while-loops-on-the-same-page-using-mysql_fetch_assoc-not-working/#findComment-1052836 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.