addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscontroller-playcrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobe--smallglobegmailgooglegroupshelp-with-circleimageimagesinstagramFill 1launch-new-window--smalllight-bulblinklocation-pinm-swarmSearchmailmessagesminusmoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonprintShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruserwarningyahoo

Re: [php-49] Duplicates in creating a select box, need highest valued items only

From: Mark S.
Sent on: Friday, November 9, 2012 4:38 PM
Im guessing you can't alter the sql, but one though I had was to
change the sql so that it only returns the highest value.

SELECT * FROM `test` AS t1
WHERE t1.city_id =
( SELECT max(city_id_created)­
FROM test WHERE unit_name = t1.unit_name )

Another thought would be to maintain two arrays, one that maps the
city ids and one that maintains the html, something like:

<?php

// tracks highest city id
$mapping_array = array();

// maintains your html
$html_array = array();

// for loop
foreach ($cities as $city) {
    // check if mapping_array has the city yet
    if (empty($mapping_arra­y[$city['city_name']­])) {

        $mapping_array[$city­['city_name']][$city­['city_id']]  = array();

        $html_array[$city['c­ity_id']] = '<option>stuff­</option>';
    // if the city exists lets check to see if id is higher or not
    } else {
        // get first element of our city_name array
        $key = key( $mapping_array[$city­['city_name']][$city­['city_id']] );

        // check if current id is lower than incoming id
        if(  $key < $city['city_id'] ) {
            // update our city_name with the new city id
            $mapping_array[$city­['city_name']][$city­['city_id']] = array();

            // unset old html
            unset( $html_array[$key] );

            // update html
            $html_array[$city['c­ity_id']] = '<option></­option>';
        }
    }
}

// implode array so we now have all of our dropdowns
$dropdown_html = implode( PHP_EOL, $html_array );

Anyway I'm sure there are more elegant ways to do this, but this was
my second thought.

Good Luck!

Mark

On Fri, Nov 9, 2012 at 1:38 PM, Jd Daniel <[address removed]> wrote:
> Hey guys and gals, I'm having a bit of an issue with duplication here,
> I believe I can clip dupes at any point as long as my return value for the
> dupe remains the the largest int value. Thanks ahead of time
>
> Here's a code stamp:
>         $unitList = Base_Client_Boss::ge­tBusinessUnitList(fa­lse,
> $selectedMarket, false, true);
>         $marketListBox = $marketOptions = '';
>         $marketList = array();
>
>             for ($x = 1; $x <= $unitList['NUMBER_OF­_BUSINESS_UNITS']; $x++)
> {
>                 $unitStr = 'BUSINESS_UNIT_' . $x . '_';
>
>                 // clip the one we don't want
>                 if ($unitList[$unitStr . 'ID'] && $x != 42) {
>
>                     $businessUnit =
> Base_Client_Boss::ge­tBusinessUnit($unitL­ist[$unitStr . 'ID']);
>                     if ($unitList[$unitStr . 'ID'] && $unitList[$unitStr .
> 'ID'] != 42) {
>                         $i++;
>
>                         $marketListBox = "<option value='" .
> $unitList[$unitStr . 'ID'] . "'";
>                         if($selectedMarket == $unitList[$unitStr . 'ID']) {
>                             $marketListBox .= ' selected';
>                             $selectedMarket = 0;
>                         }
>
>                         $marketListBox .= ">&nbsp;&nbsp;­&nbsp;" .
> preg_replace('/-Wima­x/i', '', $unitList[$unitStr . 'NAME']) . "</option>\n";­
>                         $marketList[$busines­sUnit->getElement­('STATE')][] =
> $marketListBox;
>                     }
>                 }
>         }
>
>         ksort($marketList);
>         foreach($marketList as $key => $val) {
>             $marketOptions .= "<option
> disabled='disabled'>­&nbsp;</optio­n><option
> disabled='disabled'>­{$key}&#172;<­/option>\n" . implode($val);
>         }
>
>         $this->view->m­arketOptions = $marketOptions;
>     }
>
>
> Here's a sample return:
> <option disabled="disabled">­&nbsp;</optio­n><option
> disabled="disabled">­WA¬</option>
> <option value="56">&nbsp;­&nbsp;&nbsp;­Aberdeen</option>­
> <option value="82">&nbsp;­&nbsp;&nbsp;­Bellingham</optio­n>
> <option value="106">&nbsp;­&nbsp;&nbsp;­Everett</option>­
> <option value="109">&nbsp;­&nbsp;&nbsp;­Kitsap County</option>­
> <option value="102">&nbsp;­&nbsp;&nbsp;­Seattle</option>­
> <option value="105">&nbsp;­&nbsp;&nbsp;­Tacoma</option>­
> <option value="20">&nbsp;­&nbsp;&nbsp;­Tri-Cities</optio­n>
> <option value="173">&nbsp;­&nbsp;&nbsp;­Tri-Cities</optio­n>
> <option value="64">&nbsp;­&nbsp;&nbsp;­Wenatchee</option­>
> <option value="65">&nbsp;­&nbsp;&nbsp;­Yakima</option>­
> <option value="162">&nbsp;­&nbsp;&nbsp;­Yakima</option>­
>
> Here is the EXPECTED output:
>
> <option disabled="disabled">­&nbsp;</optio­n><option
> disabled="disabled">­WA¬</option>
> <option value="56">&nbsp;­&nbsp;&nbsp;­Aberdeen</option>­
> <option value="82">&nbsp;­&nbsp;&nbsp;­Bellingham</optio­n>
> <option value="106">&nbsp;­&nbsp;&nbsp;­Everett</option>­
> <option value="109">&nbsp;­&nbsp;&nbsp;­Kitsap County</option>­
> <option value="102">&nbsp;­&nbsp;&nbsp;­Seattle</option>­
> <option value="105">&nbsp;­&nbsp;&nbsp;­Tacoma</option>­
> <option value="173">&nbsp;­&nbsp;&nbsp;­Tri-Cities</optio­n>
> <option value="64">&nbsp;­&nbsp;&nbsp;­Wenatchee</option­>
> <option value="162">&nbsp;­&nbsp;&nbsp;­Yakima</option>­
>
> --
> Jd Daniel
> Sr. Zend Developer
> 7901 Delridge Way SW #36D, Seattle
>
> C.   [masked]
>
>
>
>
>
>
> --
> Please Note: If you hit "REPLY", your message will be sent to everyone on
> this mailing list ([address removed])
> This message was sent by Jd Daniel ([address removed]) from The Seattle PHP
> Meetup Group.
> To learn more about Jd Daniel, visit his/her member profile
> Set my mailing list to email me As they are sent | In one daily email |
> Don't send me mailing list messages
>
> Meetup, PO Box 4668 #37895 New York, New York[masked] |
> [address removed]



-- 
--------------------­--------------------­-
Mark Steudel
P:[masked]
F:[masked]
[address removed]

. : Work : .
http://www.mindfu...­

. : Play : .
http://www.steude...­

. : LinkedIn : .
http://www.linked...­

Our Sponsors

  • PluralSight

    PluralSight subscriptions for developer training

  • O'Reilly

    Disc Code: PCBW is good for 40% off print and 50% off ebooks and videos

  • JetBrains PhpStorm

    Occasional free licenses to raffle off at meetups

  • DynaTrace

    Find yourself, promote yourself, stay true to yourself.

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy