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
https://forums.phpfreaks.com/topic/45674-solved-limit-question/
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
https://forums.phpfreaks.com/topic/45674-solved-limit-question/#findComment-221842
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
https://forums.phpfreaks.com/topic/45674-solved-limit-question/#findComment-221859
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.