John Davidson

php - Method to display groups of items in differing order, based on a previous page selection

0 comments
Message:


I have a map page that will display a list of up to 6 service groupings available in any given state.


Each of those states will link to their own page, where those 6 potential service groups list out the items available to download in each of those groups.


Trying to find a way that if a user were to select the 3rd service group in the available choices for any given state, that once that state page opens, it would display the 3rd group of items first on the page, followed then by 1, 2, 4, 5, 6.


This is a WordPress site, I have things set up for the conditional groupings for the main map page, and I'm using a URL hash on those groupings to pass a number over to the state pages based on the user's selection.


That's no problem so far, so if a user selects the 3rd grouping, then I'm pulling the "3" from the URL on the state page using javascript, then passing that to PHP variable.


Please note - the groupings on the state pages are not in an array, just 6 separate groupings, conditionally displayed if they contain items.


What I'm stuck on, is an efficient way of using that resulting "3" to be able to display the 3rd grouping first on the state page - so, displaying those groups as 3, 1, 2, 4, 5, 6


Or, for a state that doesn't have a 2nd or 6th group, if the 4th group gets selected, displaying the groups as 4, 1, 3, 5


I know I can do this with a lot of repeated code bloat by checking the variable, then hard coding those 6 sections multiple times in varying order, but I assume there must be a better way to code that?


TIA



My Answer:
number 3 from the URL hash on the state page. Now, I just need a method to display the groups of items in the desired order based on that number.

One way to achieve this is to use a switch statement in PHP to determine the order in which the groups should be displayed. Here's an example of how you could implement this:

php
// Get the group number from the URL hash
$groupNumber = $_GET['group'];

// Define the order in which the groups should be displayed based on the selected group number
switch ($groupNumber) {
case 1:
$order = [1, 2, 3, 4, 5, 6];
break;
case 2:
$order = [2, 1, 3, 4, 5, 6];
break;
case 3:
$order = [3, 1, 2, 4, 5, 6];
break;
case 4:
$order = [4, 1, 2, 3, 5, 6];
break;
case 5:
$order = [5, 1, 2, 3, 4, 6];
break;
case 6:
$order = [6, 1, 2, 3, 4, 5];
break;
default:
$order = [1, 2, 3, 4, 5, 6];
}

// Loop through the groups in the desired order and display them
foreach ($order as $group) {
// Display the items for the current group here
echo "Group $group items
";
}
?>


In this code snippet, we first get the group number from the URL hash. Then, we use a switch statement to define the order in which the groups should be displayed based on the selected group number. Finally, we loop through the groups in the desired order and display them on the page.

You can customize the order in the switch statement to fit your specific requirements. This method allows you to dynamically display the groups of items in the desired order based on the user's selection.

Rate this post

3 of 5 based on 8798 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us