Jump to content

Recommended Posts

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));

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.

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";
}

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

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.

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.