Link to home
Start Free TrialLog in
Avatar of wfninpa
wfninpa

asked on

PHP Array all possible combinations into SELECT OPTION form drop boxes.

Goal: Output HTML drop boxes containing all possible combinations between two arrays values.

In my example arrays below I have a COLOR array and a SIZE array.  I would like the output to look something like this:

Color: Black  Size: S
Color: Black  Size: M
Color: Black  Size: L
Color: Black  Size: XL
Color: Cocoa  Size: S
Color: Cocoa  Size: M
Color: Cocoa  Size: L
Color: Cocoa  Size: XL

Where the above words of "Color:" and "Size:" are just select box labels and the values are just select boxes themselves.  And the "id=" attributes for the select/option tags should correspond to the "[id]" value shown.

For each row of color and size combination I only want the select boxes to have single values, not all available values.

In other words I want my output to look exactly as shown above.  I don't want my users to be able to change the select box values and I'm not interested in using a DISABLE or specifying a default SELECTED value.

Here is my COLOR array:

Array
(
    [0] => Array
        (
            [id] => 2
            [text] => Black
        )

    [1] => Array
        (
            [id] => 63
            [text] => Cocoa
        )
)

Here is my SIZE array:

Array
(
    [0] => Array
        (
            [id] => 2400
            [text] => S
        )

    [1] => Array
        (
            [id] => 878
            [text] => M
        )

    [2] => Array
        (
            [id] => 929
            [text] => L
        )

    [3] => Array
        (
            [id] => 58
            [text] => XL
        )

)

I would be very happy to have my solution today in PHP code of course.  Thanks.
Avatar of AielloJ
AielloJ
Flag of United States of America image

wfninpa:

Is this data stored in a database?  It would be much simpler and flexible to create what you want in the SQL query if it's in a database.

Aielloj
Avatar of wfninpa
wfninpa

ASKER

Yes it is but for the sake of not having to change the rest of the world to suit this particular instance as I said I've got two arrays so go with that please.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wfninpa

ASKER

After the first post I did think that myself.  It's Sunday here so I just didn't feel like walking through and coding it myself. I will take a look at the code posted.  Thanks.
Do you care about the use of the "id" fields in these arrays of arrays?  And regarding this, "I don't want my users to be able to change the select box values" - you should PLAN on having your users change the select box values.  The nice ones won't; the hackers will - it's just that simple.  That kind of planning is basic to application security, and is implemented in the data validation logic of the action script that processes your form input.
Also, are you talking about an HTML drop-down select control or about checkboxes?  And do you need the "id" values submitted or just used to style the HTML?  Do you want single select only or multiple select?   Thanks, ~Ray

<?php // RAY_temp_wfninpa.php
error_reporting(E_ALL);
echo "<pre>";

/* DESCRIPTION OF OUTPUT FROM THE POST AT EE
Color: Black  Size: S
Color: Black  Size: M
Color: Black  Size: L
Color: Black  Size: XL
Color: Cocoa  Size: S
Color: Cocoa  Size: M
Color: Cocoa  Size: L
Color: Cocoa  Size: XL
*/

// TEST DATA FROM THE POST AT EE
// COLOR ARRAY
$colors
= Array
(
    "0" => Array
        ( "id" => 2
        , "text" => "Black"
        )
        ,

    "1" => Array
        ( "id" => 63
        , "text" => "Cocoa"
        )
)
;

// SIZE ARRAY
$sizes
= Array
(
    "0" => Array
        ( "id" => 2400
        , "text" => "S"
        )
        ,
    "1" => Array
        ( "id" => 878
        , "text" => "M"
        )
        ,
    "2" => Array
        ( "id" => 929
        , "text" => "L"
        )
        ,

    "3" => Array
        ( "id" => 58
        , "text" => "XL"
        )

)
;

// DOES THE TEST DATA LOOK RIGHT? - CLOSE ENOUGH!
// print_r($colors);
// print_r($sizes);


// IF THE FORM HAS BEEN SUBMITTED
if (!empty($_POST["thing"]))
{
    // SET DEFAULT VALUES
    $chosen_color = 'UNKNOWN';
    $chosen_size  = 'UNKNOWN';

    // ISOLATE THE COMPONENTS OF THE POSTED DATA
    $arr = explode('-', $_POST["thing"]);
    $my_color = $arr[0];
    $my_size  = $arr[1];

    // LOOK UP THE INFORMATION IN OUR ARRAYS
    foreach ($colors as $key => $color_array)
    {
        if ($color_array["id"] == $my_color) $chosen_color = $color_array["text"];
    }
    foreach ($sizes as $key => $size_array)
    {
        if ($size_array["id"] == $my_size)   $chosen_size  = $size_array["text"];
    }

    // SHOW THE CHOICES
    echo PHP_EOL . "YOU CHOSE COLOR: $chosen_color AND SIZE: $chosen_size";
}

// CREATE THE FORM FROM THE ARRAYS
echo '<form method="post">' . PHP_EOL;
echo '<select name="thing">' . PHP_EOL;
foreach ($colors as $key => $color_array)
{
    foreach ($sizes as $key => $size_array)
    {
        $option
        = '<option value="'
        . $color_array['id']
        . '-'
        . $size_array['id']
        . '">'
        . 'COLOR: '
        . $color_array['text']
        . ' SIZE: '
        . $size_array['text']
        . '</option>'
        . PHP_EOL
        ;
        echo $option;
    }
}
echo '</select>' . PHP_EOL;
echo '<input type="submit" />' . PHP_EOL;
echo '</form>';

Open in new window

Avatar of wfninpa

ASKER

My apologies if my post was misleading.  I did not mean checkbox when I said select boxes.  No for the multiple select.  Thanks for looking out for me but processing is not a concern because that is locked down already and protected from all sorts of injection.

I do care about the use of the key "id" from the array as it will be the option value:

<select><option id="combo1_color" value="2">Black</option></select>
<select><option id="combo1_size" value="2400">S</option></select>
<input type="text" id="combo1_quantity" name="combo1_quantity" value="0" />

<select><option id="combo2_color" value="2">Black</option></select>
<select><option id="combo2_size" value="878">M</option></select>
<input type="text" id="combo2_quantity" name="combo2_quantity" value="0" />

etc....  Until all possible combinations are displayed.

I have a reason for all of this.  I guess all I really needed was how to get all of the possible combinations and also have access to the key "id" if I decided to use it for anything.  If you're curious I am modifying OSCommerce a little bit to integrate and provide further information regarding a particular products color and size combination with data provided from another table I've created within OSCommerce that is updated every 15 minutes from our CRM and inventory systems.  Basically I will be creating the combinations and then iterating through the loop of combinations with some MySQL SELECT statements inside the loop that will do various things for me.  It's a long and deep explanation as to why.

Anyway, I have been working on this today and have attached what I've come up with (using snippets here and there).

At this point I'm not concerned with code to create my form elements.

I think I've created what I need but possibly you can suggest some things here and there that would make it better.

Thanks.
<?php
    header('Content-Type: text/plain');

    function showCombinations($string, $traits, $i)
    {
        if ($i >= count($traits))
            echo trim($string) . "\n";
        else
        {
            foreach ($traits[$i] as $trait)
                showCombinations("$string $trait", $traits, $i + 1);
        }
    }
    
	// COLORS
	$colors = array
	(
	  array("id" => "72", "value" => "Black"),
	  array("id" => "54", "value" => "White"),
	  array("id" => "62", "value" => "Red"),
	  array("id" => "119", "value" => "Orange"),
	  array("id" => "23", "value" => "Navy"),
	  array("id" => "29", "value" => "Sparky Blue")
	);

	// SIZES
	$sizes = array
	(
	  array("id" => "22", "value" => "S"),
	  array("id" => "115", "value" => "M"),
	  array("id" => "85", "value" => "L"),
	  array("id" => "9", "value" => "XL"),
	  array("id" => "4", "value" => "XXL")
	);

$colorsArr = array();
for ( $row = 0; $row < sizeof($colors); $row++ )
{
 while ( list( $key, $value ) = each( $colors[$row] ) )
 {	
	if ($row2 == 0)
	{  	
	  $row2++;
	} else {
	  echo "$value";  // SCREEN OUTPUT - only display the 2nd index value which is KEY 'value'
	  $colorsArr[] = $value;  // put the value of KEY 'value' into new array
	  $row2 = 0;
	}
 }
 echo "\n";
}
$row = 0;  // reset variable to zero out because I'm paranoid
$row2 = 0;  // reset variable to zero out because I'm paranoid

$sizesArr = array();
for ( $row = 0; $row < sizeof($sizes); $row++ )
{
 while ( list( $key, $value ) = each( $sizes[$row] ) )
 {
	if ($row2 == 0)
	{  	
	  $row2++;
	} else {
	  echo "$value";  // SCREEN OUTPUT - only display the 2nd index value which is KEY 'value'
	  $sizesArr[] = $value;  // put the value of KEY 'value' into new array
	  $row2 = 0;
	}
 }
 echo "\n";
}
$row = 0;  // reset variable to zero out because I'm paranoid
$row2 = 0;  // reset variable to zero out because I'm paranoid

// SCREEN OUTPUT for testing
echo "\n";
echo "New Arrays (without 'id' key)";
echo "\n";
print_r($colorsArr);  // SCREEN OUTPUT of new COLORS only array
print_r($sizesArr);  // SCREEN OUTPUT of new SIZES only array

	// create multidimensional array
	$traits = array
    (
		$colorsArr,$sizesArr
    );
    
    showCombinations('', $traits, 0);
?>

Open in new window

Sorry if i got ya wrong ,but i think you want a chained select. I'm not on my production PC now so i can't suggest you a adapted code but this link will do the trick for you i guess.

http://www.yxscripts.com/cs/chainedselects.html

You can populate JS variables which will be injected into <option>

But maybe id deal make you confused. (a tip : you can use option values as joined 2 variables.  )
Did you try the code I posted above?  You can install it and run it to see how the moving parts interoperate.

Regarding this:
<select><option id="combo1_color" value="2">Black</option></select>
<select><option id="combo1_size" value="2400">S</option></select>

You will want to have a SELECT control to wrap around these option tags.  The <select> will contain the name of the key to the POST array.

When it comes to things like this:
<input type="text" id="combo1_quantity" name="combo1_quantity" value="0" />
<input type="text" id="combo2_quantity" name="combo2_quantity" value="0" />

You may want to use an array structure instead of generating unique names.  The resulting code will be much easier to debug, modify and maintain if it is not dependent on variable names.

Here is the code from above, now using the new arrays of colors and sizes.  In the colors and sizes array, I changed the key name from "value" to "text" mainly because it is a "text" string and has no real computer-science meaning as a value - it just provides visual information.

HTH, ~Ray
<?php // RAY_temp_wfninpa.php
error_reporting(E_ALL);
echo "<pre>";

/* DESCRIPTION OF OUTPUT FROM THE POST AT EE
Color: Black  Size: S
Color: Black  Size: M
Color: Black  Size: L
Color: Black  Size: XL
Color: Cocoa  Size: S
Color: Cocoa  Size: M
Color: Cocoa  Size: L
Color: Cocoa  Size: XL
*/

// TEST DATA NEWLY COPIED FROM THE POST AT EE
	// COLORS
	$colors = array
	(
	  array("id" => "72",  "text" => "Black"),
	  array("id" => "54",  "text" => "White"),
	  array("id" => "62",  "text" => "Red"),
	  array("id" => "119", "text" => "Orange"),
	  array("id" => "23",  "text" => "Navy"),
	  array("id" => "29",  "text" => "Sparky Blue")
	);

	// SIZES
	$sizes = array
	(
	  array("id" => "22",  "text" => "S"),
	  array("id" => "115", "text" => "M"),
	  array("id" => "85",  "text" => "L"),
	  array("id" => "9",   "text" => "XL"),
	  array("id" => "4",   "text" => "XXL")
	);

// DOES THE TEST DATA LOOK RIGHT? - CLOSE ENOUGH!
// print_r($colors);
// print_r($sizes);


// IF THE FORM HAS BEEN SUBMITTED
if (!empty($_POST["thing"]))
{
    // SET DEFAULT VALUES
    $chosen_color = 'UNKNOWN';
    $chosen_size  = 'UNKNOWN';

    // ISOLATE THE COMPONENTS OF THE POSTED DATA
    $arr = explode('-', $_POST["thing"]);
    $my_color = $arr[0];
    $my_size  = $arr[1];

    // LOOK UP THE INFORMATION IN OUR ARRAYS
    foreach ($colors as $key => $color_array)
    {
        if ($color_array["id"] == $my_color) $chosen_color = $color_array["text"];
    }
    foreach ($sizes as $key => $size_array)
    {
        if ($size_array["id"] == $my_size)   $chosen_size  = $size_array["text"];
    }

    // SHOW THE CHOICES
    echo PHP_EOL . "YOU CHOSE COLOR: $chosen_color AND SIZE: $chosen_size";
}

// CREATE THE FORM FROM THE ARRAYS
echo '<form method="post">' . PHP_EOL;
echo '<select name="thing">' . PHP_EOL;
foreach ($colors as $key => $color_array)
{
    foreach ($sizes as $key => $size_array)
    {
        $option
        = '<option value="'
        . $color_array['id']
        . '-'
        . $size_array['id']
        . '">'
        . 'COLOR: '
        . $color_array['text']
        . ' SIZE: '
        . $size_array['text']
        . '</option>'
        . PHP_EOL
        ;
        echo $option;
    }
}
echo '</select>' . PHP_EOL;
echo '<input type="submit" />' . PHP_EOL;
echo '</form>';

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wfninpa

ASKER

Exact solution not provided.  
If you have a question, EE is a good place to get answers, and you have gotten complete answers, and more, to this question.  

But it's been hanging around since October!  If you want an exact solution (one that works seamlessly in your servers) then you need to hire a professional developer to create your solution.  Volunteers here at EE cannot do that because we do not have access to your servers and data model.

Best of luck with your project, ~Ray
Avatar of wfninpa

ASKER

PLEASE CLOSE THIS.  THE QUESTION WAS NOT ANSWERED FULLY OR CORRECTLY AND I'M NO LONGER IN NEED OF THE ANSWER.
Complete, correct and easy-to-understand answers were posted here.   These answers contained tested and working code snippets.  ID:34136499.  ID:34136839.  I do not know what you expected beyond these exact solutions.  All we had to work with are the examples you posted.  Can you please explain why you marked the grade down to the lowest possible grade you can give at EE?  And please tell us why anyone should try to help you next time?  Thanks. ~Ray
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wfninpa

ASKER

Everyone was helpful.  Thank you.
"And please tell us why anyone should try to help you next time? " .... is a very good question for all the people like this asker. None of these people needs your points, but expecting their work to be considered is everyone's right i think.

P & L  
I concur with erdincgc.

Your comment "Most of your responses were suggestive that I do things a different way that would ultimately require changing other things (the way oscommerce builds/fills select options in the product_details.php page).  I did mention what I wanted to do and what I did not want to do in the initial post." was totally unappreciative of the efforts to help you with your project, thus I stopped assisting on this question.  I saw that response as a predictor of how the rest of your responses would be like.  Unfortunately I was correct in my assessement.  Most of the names of the experts that responded to you are very familiar to me, and I have a great respect for their opinions which was lacking in your responses.  There are certain people I don't bother to respond to their posts.  Unfortunately I'll be adding your name to that list.

This could have been done very simply with a SQL query and such was suggested.  It would have been the shortest and most reliable way to get this accomplished as opposed to the hand coded solution you insisted on.  We're not here to tell you what you want to hear, but to offer suggestions as to the best way to get things done.  Having customized many open source software packages, it's possible to do things efficiently, and in my opinion correctly, if you're open to some of the suggestions made.

AielloJ