Jump to content

[SOLVED] LIMIT Question


JSHINER

Recommended Posts

I have the following function:

 

function zips($db, $state = false) {

$zip = $db->escape($zip);

$arr = $db->getArray("SELECT 

		towns_zipcodes.town_id AS town_id, 

		towns_zipcodes.zip AS zip,

		towns.name AS name,

		towns.id AS id,

		towns_zipcodes.state AS state

		FROM towns, towns_zipcodes

		WHERE towns_zipcodes.town_id = towns.id AND

		towns_zipcodes.state = '$state' ORDER BY name ASC");

return $arr;

}

 

Which I have displayed as:

 

	<?php

if($page['zips']) {

foreach ($page['zips'] as $z) {

echo '';
		echo '<option value="show.php?zip=', $z['zip'], '">', $z['name'], '</option>';
}
}

	?>

 

Problem is it's calling Towns & Zips - some towns have multiple zips, so it will show Town 00001, Town 00002, Town 2 00003, Town 2 00004 .... etc. I want it to limit it to ONE town name. How can I do this?

Link to comment
Share on other sites

Try using this for output:

 

	
<?php
if($page['zips']) :

  $last = '';
  foreach ($page['zips'] as $z) :

    if ($last != $z['name'])  : 
      if (!empty($last)) echo '</optgroup>';
      $last = $z['name'];
?>
      <optgroup label=<?=$z['name'] ?>>
<?php else : ?>
      <option value="show.php?zip=<?=$z['zip'] ?>"><?=$z['zip'] ?></option>
<?php 
    endif;

  endforeach;

endif;
?>

Link to comment
Share on other sites

Thank you - but - I only want ONE TOWN and ONE ZIP - the second, third, etc. zip for each town is not necessary for this function.

 

I simply Want Town A - 00001, Town B - 00003 (Omitting Town A - 00002) and so on.

Link to comment
Share on other sites

My apologies.  You should probably just use this query:

 

SELECT 
z.town_id AS town_id,
z.zip AS zip,
t.name AS name,
t.id AS id,
z.state AS state
FROM towns t
JOIN towns_zipcodes z ON z.town_id = t.id
WHERE z.state = '$state' 
GROUP BY z.zip
ORDER BY name ASC

Link to comment
Share on other sites

SELECT 
z.town_id AS town_id,
z.zip AS zip,
t.name AS name,
t.id AS id,
z.state AS state
FROM towns t
JOIN towns_zipcodes z ON z.town_id = t.id
WHERE z.state = '$state' 
GROUP BY t.id
ORDER BY t.name ASC

 

Sorry, I used the wrong GROUP BY field.  You want to have each city once, and ZIP codes won't overlap, right?  So we need to group the results by the city ID.  My earlier post told the server to group by ZIP.

Link to comment
Share on other sites

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.