/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] PHP Comparing and Establishing a Hierarchy

I am fairly new to PHP. I’ve only created a few PHP/MySQL scripts so far, so what I am trying to accomplish is a little out of my level of expertise.

Here is what I have:

In essence, I have 6 dynamically populated drop down menus, which are populated by a MySQL database. The end user selects an option from each of the 6 drop down menus. The end user then clicks the submit button, which collects the data from the drop down menus.

Here is what I want to accomplish:

Upon the user clicking the submit button, I want to sort the end user’s choices, based on a hierarchy that I’ve defined, and then echo the results back to the browser in the correct order. So, how would I:

[LIST=1]

  • [*]

    Define the Hierarchy?


  • [*]

    Sort the Data


  • [*]

    Arrange the Data (based on the hierarchy); and


  • [*]

    Echo the results to the browser out


  • [/LIST]

    For instance, is it possible to define my hierarchy like this?

    [CODE]
    $a = 110A;
    $b = 130B;
    $c = 9000;

    $a > $b > $c;
    [/CODE]

    to post a comment
    PHP

    12 Comments(s)

    Copy linkTweet thisAlerts:
    @NogDogAug 26.2016 — A lot of unknowns there for me (plus it's Friday afternoon with the post-lunch desire for a nap), so I'm not sure what the "right" answers are. Maybe you could provide an abbreviated example of 2 or 3 drop-downs, what they would contain (at least a few entries each?), and what you would expect the output to be for a couple different user selections?

    Depending on how dynamic the drop-down data is and how complex this hierarchy you allude to is, it might simply be a JSON config file and a bit of usort()-ing to get the results, or it might be a relational database solution with relation tables describing the hierarchy and complex DB queries to get the result. ?
    Copy linkTweet thisAlerts:
    @ginerjmAug 26.2016 — From your list of things to do, the only one you should not be able to do as of now is #1 - define the hierarchy. Sorting is a simple command; arranging the data is a simple result of the sorting and the echo'ing is a piece of cake.

    So - a hierarchy is a formalized relationship, both between elements of one 'group' and one group among other groups. You haven't given us a single idea of what the relationships of any of those things are, so your first step would be to either tell us what they are, or come up with your rules yourself and run it past us.
    Copy linkTweet thisAlerts:
    @Wahoo92583authorAug 26.2016 — I apologize for the vagueness of my post. I am new to forums and therefore, I do not know the proper etiquette (for the lack of better words) for posting nor the proper way to ask my question(s). I will; however, do my best to clarify my question(s).

    I work for a law enforcement agency. The state of Florida has created a hierarchy of crime codes. Based on the hierarchy, cases will be coded a specific way. In my department, an often recurring problem is that employees code cases incorrectly because they either do not know the hierarchy or simply do not care. So, my drop down menus will be populated with the crime codes. For instance, here are some of the crime codes, as they appear in relation to the hierarchy.

    1) 090A

    2) 090C

    3) 110A

    4) 110C

    5) 1200

    6) 130A

    7) 130D


    So, if the user selected 1200, 110A, 090A, and 130A (in that order), I would want the end result to be in the following order 090A, 110A, 1200, and 130A.

    End-User selects codes in random order from drop down menu ----> Clicks "Sort" button ----> PHP collects the data from the user's selection ----> PHP sorts the data ----> returns the results back to the browser in a table.

    NOTE: The hierarchy I will define in my script will be the pre-defined state of Florida hierarchy.
    Copy linkTweet thisAlerts:
    @ginerjmAug 26.2016 — So you have a hierarchy defined for you. And - it appears that a straight-forward php sort will put things in order for you. So now you simple do your query(?), sort the results and output them. No?
    Copy linkTweet thisAlerts:
    @Wahoo92583authorAug 26.2016 — I'm still confused. Although I have the hierarchy defined for me, PHP doesn't know the pre-defined hierarchy and therefore, how will it know how to sort the data correctly?

    How do I establish the state defined hierarchy in PHP?

    What function would I use to sort my data?

    Should I input the data into an array?
    Copy linkTweet thisAlerts:
    @ginerjmAug 26.2016 — No - you are confusing me. Show us more of this "hierarchy". Apparently you have only shown us half. You showed us codes that you demonstrated can be place in proper sequence with a simple sort. But that is n't enough for you?

    What are the dropdowns - a code and a literal for that code? Do they come from a database? So you take the codes, do a query to get the literals then sort the results by code again and display the code and the literal in order. Or do I have it wrong?
    Copy linkTweet thisAlerts:
    @NogDogAug 26.2016 — So...if I understand correctly, you need to define the hierarchy somewhere. A fairly simple way would be in an array. If you don't want to maintain it in your PHP code, it could be in a JSON file, or even a DB table, either of which you load into an array as needed. Then something like this (probably over-complex) code could be used:
    [code=php]
    /**
    * @param array $codes
    * @return array
    * @throws Exception
    */
    function sortCodes(array $codes)
    {
    $hierarchy = array(
    '090A',
    '090C',
    '110A',
    '110C',
    '1200',
    '130A',
    '130D'
    );
    $result = array();
    foreach($codes as $code) {
    $key = array_search($code, $hierarchy);
    if($key === false) {
    throw new Exception("'$code' not found in hierarchy");
    }
    $result[$key] = $code;
    }
    ksort($result);
    return array_values($result);
    }

    // test it:
    $codes = array(
    '090A',
    '090C',
    '110A',
    '110C',
    '1200',
    '130A',
    '130D'
    );
    for($i=0; $i<20; $i++) {
    $test = $codes;
    shuffle($test);
    $test = array_slice($test, 0, 3);
    echo "Test data:".PHP_EOL;
    print_r($test);
    $sorted = sortCodes($test);
    echo "Sorted data:".PHP_EOL;
    print_r($sorted);
    echo PHP_EOL;
    }
    [/code]

    Sample test run:
    <i>
    </i>[~]$ php sort.php
    Test data:
    Array
    (
    [0] =&gt; 130D
    [1] =&gt; 130A
    [2] =&gt; 110C
    )
    Sorted data:
    Array
    (
    [0] =&gt; 110C
    [1] =&gt; 130A
    [2] =&gt; 130D
    )

    Test data:
    Array
    (
    [0] =&gt; 130D
    [1] =&gt; 090A
    [2] =&gt; 090C
    )
    Sorted data:
    Array
    (
    [0] =&gt; 090A
    [1] =&gt; 090C
    [2] =&gt; 130D
    )

    Test data:
    Array
    (
    [0] =&gt; 090C
    [1] =&gt; 130D
    [2] =&gt; 090A
    )
    Sorted data:
    Array
    (
    [0] =&gt; 090A
    [1] =&gt; 090C
    [2] =&gt; 130D
    )

    Test data:
    Array
    (
    [0] =&gt; 130D
    [1] =&gt; 090A
    [2] =&gt; 110A
    )
    Sorted data:
    Array
    (
    [0] =&gt; 090A
    [1] =&gt; 110A
    [2] =&gt; 130D
    )

    Test data:
    Array
    (
    [0] =&gt; 110C
    [1] =&gt; 1200
    [2] =&gt; 130D
    )
    Sorted data:
    Array
    (
    [0] =&gt; 110C
    [1] =&gt; 1200
    [2] =&gt; 130D
    )

    Test data:
    Array
    (
    [0] =&gt; 110C
    [1] =&gt; 090C
    [2] =&gt; 110A
    )
    Sorted data:
    Array
    (
    [0] =&gt; 090C
    [1] =&gt; 110A
    [2] =&gt; 110C
    )

    Test data:
    Array
    (
    [0] =&gt; 090C
    [1] =&gt; 1200
    [2] =&gt; 130A
    )
    Sorted data:
    Array
    (
    [0] =&gt; 090C
    [1] =&gt; 1200
    [2] =&gt; 130A
    )

    Test data:
    Array
    (
    [0] =&gt; 130D
    [1] =&gt; 110A
    [2] =&gt; 090A
    )
    Sorted data:
    Array
    (
    [0] =&gt; 090A
    [1] =&gt; 110A
    [2] =&gt; 130D
    )

    Test data:
    Array
    (
    [0] =&gt; 1200
    [1] =&gt; 110C
    [2] =&gt; 130D
    )
    Sorted data:
    Array
    (
    [0] =&gt; 110C
    [1] =&gt; 1200
    [2] =&gt; 130D
    )

    Test data:
    Array
    (
    [0] =&gt; 110A
    [1] =&gt; 110C
    [2] =&gt; 1200
    )
    Sorted data:
    Array
    (
    [0] =&gt; 110A
    [1] =&gt; 110C
    [2] =&gt; 1200
    )

    Test data:
    Array
    (
    [0] =&gt; 110C
    [1] =&gt; 130A
    [2] =&gt; 090C
    )
    Sorted data:
    Array
    (
    [0] =&gt; 090C
    [1] =&gt; 110C
    [2] =&gt; 130A
    )

    Test data:
    Array
    (
    [0] =&gt; 110C
    [1] =&gt; 130A
    [2] =&gt; 090C
    )
    Sorted data:
    Array
    (
    [0] =&gt; 090C
    [1] =&gt; 110C
    [2] =&gt; 130A
    )

    Test data:
    Array
    (
    [0] =&gt; 130A
    [1] =&gt; 1200
    [2] =&gt; 130D
    )
    Sorted data:
    Array
    (
    [0] =&gt; 1200
    [1] =&gt; 130A
    [2] =&gt; 130D
    )

    Test data:
    Array
    (
    [0] =&gt; 130D
    [1] =&gt; 090A
    [2] =&gt; 130A
    )
    Sorted data:
    Array
    (
    [0] =&gt; 090A
    [1] =&gt; 130A
    [2] =&gt; 130D
    )

    Test data:
    Array
    (
    [0] =&gt; 1200
    [1] =&gt; 090C
    [2] =&gt; 110C
    )
    Sorted data:
    Array
    (
    [0] =&gt; 090C
    [1] =&gt; 110C
    [2] =&gt; 1200
    )

    Test data:
    Array
    (
    [0] =&gt; 090A
    [1] =&gt; 110C
    [2] =&gt; 1200
    )
    Sorted data:
    Array
    (
    [0] =&gt; 090A
    [1] =&gt; 110C
    [2] =&gt; 1200
    )

    Test data:
    Array
    (
    [0] =&gt; 090A
    [1] =&gt; 1200
    [2] =&gt; 110C
    )
    Sorted data:
    Array
    (
    [0] =&gt; 090A
    [1] =&gt; 110C
    [2] =&gt; 1200
    )

    Test data:
    Array
    (
    [0] =&gt; 090A
    [1] =&gt; 130A
    [2] =&gt; 130D
    )
    Sorted data:
    Array
    (
    [0] =&gt; 090A
    [1] =&gt; 130A
    [2] =&gt; 130D
    )

    Test data:
    Array
    (
    [0] =&gt; 090A
    [1] =&gt; 130D
    [2] =&gt; 090C
    )
    Sorted data:
    Array
    (
    [0] =&gt; 090A
    [1] =&gt; 090C
    [2] =&gt; 130D
    )

    Test data:
    Array
    (
    [0] =&gt; 090A
    [1] =&gt; 130A
    [2] =&gt; 090C
    )
    Sorted data:
    Array
    (
    [0] =&gt; 090A
    [1] =&gt; 090C
    [2] =&gt; 130A
    )
    Copy linkTweet thisAlerts:
    @Wahoo92583authorAug 27.2016 — Awesome!! Thank you!! I don't fully understand your code, but sorting the array via their keys makes perfect sense. Can you explain your code to me a little, please?


    How would I account for "exceptions" to the hierarchy? In my example, I didn't list all of the codes, as there's just way too many. For instance, when a 230G is listed with a 2400, the 230G needs to be changed to 0000 and the case needs to be coded as a 2400.

    Naturally, a 230G sits higher in the hierarchy than a 2400. However, when a 230G and a 2400 appear in the same case, the 2400 trumps the 230G.

    There's a few other exceptions to account for, but if you help me with this one, I'm sure I can account for the others.
    Copy linkTweet thisAlerts:
    @Wahoo92583authorAug 27.2016 — Awesome!! Thank you!! I don't fully understand your code, but sorting the array via their keys makes perfect sense. Can you explain your code to me a little, please?


    How would I account for "exceptions" to the hierarchy? In my example, I didn't list all of the codes, as there's just way too many. For instance, when a 230G is listed with a 2400, the 230G needs to be changed to 0000 and the case needs to be coded as a 2400.

    Naturally, a 230G sits higher in the hierarchy than a 2400. However, when a 230G and a 2400 appear in the same case, the 2400 trumps the 230G.

    There's a few other exceptions to account for, but if you help me with this one, I'm sure I can account for the others.
    Copy linkTweet thisAlerts:
    @NogDogAug 27.2016 — As far as what my code was doing, it's building an array that uses the values from the array of codes the user selected, but using the matching array key (or index if you prefer) from the hierarchy array. (array_search() returns the matched values key). Then we're just doing a sort by the keys in that new array, then finally spitting out the array with the keys renumbered in sequence from 0 up via the array_values() function. (I figured it would be a little less confusing to ultimately work with a sequentially indexed array.)

    As for dealing with exceptions, probably each one has to be handled on its own. Each might have to be analyzed to determine if you deal with it before the sorting gets done, or after. For instance, you suggest you might change a code to "0000" in a particular circumstance. If you want that change before you do the sort, then you'll have to search for that condition, make the change, then call the sort function.

    If a certain condition might actually change how you want the sorting hierarchy to be, you might want to actually pass the hierarchy array in as an additional parameter to that function. In that case, you might define a number of hierarchy arrays, and the select which one to pass in depending on such special circumstances. If there are only a few of these special cases, that all seems reasonably manageable, but if there are a lot (for some undefined value of "a lot"), I might have to start reading up on "expert systems", which typically deal with a lot of "if this then that, unless this, in which case that..." sort of things, and maybe see if there's a clever, cool way to do this in a maintainable way. ?
    Copy linkTweet thisAlerts:
    @Wahoo92583authorAug 27.2016 — Thanks, again!! This works exactly how I wanted it to! I still don't fully understand your program, but that's okay. How did you learn PHP? For the most part, I'm self taught and my knowledge is very minimal, which I'm sure is evident. Any advice or resources you recommend to help me learn? I would like to become proficient in PHP.

    Really, the only true PHP script I've created was a simple MySQL database with some PHP code. It collected the user's input, searched a database to find any and all things containing their search criteria, and then echoed the results into a table. If the number of results was greater than 6, the user could input a keyword to help narrow their results down. If the results equaled zero, a contact form would be generated so the user could email my boss, letting her know.

    Although my program is a simple one, it has proven to be very beneficial over the years. Now, this program will be even more beneficial to my site.
    Copy linkTweet thisAlerts:
    @NogDogAug 27.2016 — I learned by doing -- which included lots of Googling and trying things, along with reading a few books. ?

    [img]https://pbs.twimg.com/media/Cf7eHZ1W4AEeZJA.jpg[/img]
    ×

    Success!

    Help @Wahoo92583 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...