coolshrimp Posted January 3, 2016 Share Posted January 3, 2016 I'm trying to auto populate a multidimensional box from a csv file. Example of drop down: Example Code for drop down: <select class="form-control" name="Items[]" multiple> <optgroup label="Catagory 1"></optgroup> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> <option value="4">Option 4</option> <option value="5">Option 5</option> <option value="6">Option 6</option> <optgroup label="Catagory 2"></optgroup> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> <option value="4">Option 4</option> <option value="5">Option 5</option> <option value="6">Option 6</option> </select> Here is what CSV looks like: Iv converted the CSV to an array then converted that array into multidimensional array and generated a drop box: <select class="form-control" name="Items[]" multiple> <?php $items = array(); $count = 0; foreach ($CSVArray as $row) { $count++; if ($count > 1 && $row[0] !== "") { array_push($items, array( "Catagory" => $row[0], "Item" => $row[3], "Description" => $row[4], )); } } $count = 0; foreach ($items as $row) { $count++; if ($count > 1 && $row[0] !== "") { array_push($items, array( "Catagory" => $row[0], "Item" => $row[3], "Description" => $row[3], )); echo "<optgroup label='{$row[Catagory]}'></optgroup>"; echo "<option value='$row[Item]'>{$row[Item]}({$row[Description]})</option>"; } } ?> </select> Now i have generated the dropdown but each catagory is listed multiple times on with each item: id like to Merge the duplicate category in the array but keeping the Items how can i do this? Bathroom Items Hand Towels Shower Curtain Wash Cloth Bedroom Items Alarm clock More More Current array: Array ( [0] => Array ( [Catagory] => Bathroom Items [Item] => bath mat [Description] => bath mat ) [1] => Array ( [Catagory] => Bathroom Items [Item] => bath towels [Description] => bath towels ) [2] => Array ( [Catagory] => Bathroom Items [Item] => hand towels [Description] => hand towels ) [3] => Array ( [Catagory] => Bathroom Items [Item] => rubber bath mat [Description] => rubber bath mat ) [4] => Array ( [Catagory] => Bathroom Items [Item] => shower curtain [Description] => shower curtain ) [5] => Array ( [Catagory] => Bedroom Items [Item] => alarm clock [Description] => alarm clock ) [6] => Array ( [Catagory] => Bedroom Items [Item] => bed sheets [Description] => bed sheets ) [7] => Array ( [Catagory] => Bedroom Items [Item] => bed sheets [Description] => bed sheets ) [8] => Array ( [Catagory] => Bedroom Items [Item] => bed sheets [Description] => bed sheets ) [9] => Array ( [Catagory] => Bedroom Items [Item] => bed sheets [Description] => bed sheets ) [10] => Array ( [Catagory] => Bedroom Items [Item] => blankets [Description] => blankets ) [11] => Array ( [Catagory] => Bedroom Items [Item] => blankets [Description] => blankets ) [12] => Array ( [Catagory] => Bedroom Items [Item] => blankets [Description] => blankets ) [13] => Array ( [Catagory] => Bedroom Items [Item] => blankets [Description] => blankets ) [14] => Array ( [Catagory] => Bedroom Items [Item] => dressers [Description] => dressers, bureaus ) [15] => Array ( [Catagory] => Bedroom Items [Item] => dressers [Description] => dressers, bureaus ) [16] => Array ( [Catagory] => Bedroom Items [Item] => dressers [Description] => dressers, bureaus ) [17] => Array ( [Catagory] => Bedroom Items [Item] => duvets / comforter [Description] => duvets / comforter ) [18] => Array ( [Catagory] => Bedroom Items [Item] => duvets / comforter [Description] => duvets / comforter ) [19] => Array ( [Catagory] => Bedroom Items [Item] => duvets / comforter [Description] => duvets / comforter ) [20] => Array ( [Catagory] => Bedroom Items [Item] => duvets / comforter [Description] => duvets / comforter ) [21] => Array ( [Catagory] => Bedroom Items [Item] => hamper [Description] => clothes hampers ) [22] => Array ( [Catagory] => Bedroom Items [Item] => hanger [Description] => clothes hangers ) [23] => Array ( [Catagory] => Bedroom Items [Item] => mattress covers [Description] => mattress covers ) [24] => Array ( [Catagory] => Bedroom Items [Item] => mirrors [Description] => mirrors ) [25] => Array ( [Catagory] => Bedroom Items [Item] => pillow cases [Description] => pillow cases ) [26] => Array ( [Catagory] => Bedroom Items [Item] => pillows [Description] => pillows ) [27] => Array ( [Catagory] => Bedroom Items [Item] => pillows [Description] => pillows ) [28] => Array ( [Catagory] => Bedroom Items [Item] => pillows [Description] => pillows ) [29] => Array ( [Catagory] => Bedroom Items [Item] => pillows [Description] => pillows ) [30] => Array ( [Catagory] => Bedroom Items [Item] => small garbage cans [Description] => small garbage cans ) ) Link to comment Share on other sites More sharing options...
Ingolme Posted January 3, 2016 Share Posted January 3, 2016 The ideal data structure would be like this: $categories = array( 'Bathroom Items' => array( array( 'Item' => 'bath mat', 'Description' => 'bath mat' ), array( 'Item' => 'bath towels', 'Description' => 'bath towels' ), // ... More items ... ), 'Bedroom Items' => array( array( 'Item' => 'pillows', 'Description' => 'pillows' ), array( 'Item' => 'alarm clock', 'Description' => 'alarm clock' ), // ... More items ... ) ); You can generate this structure from your CSV structure like this: $categories = array(); $count = 0; foreach ($CSVArray as $row) { if ($count > 1 && !empty($row[0])) { $category = $row[0]; $item = $row[3]; $description = $row[4]; if(!isset($categories[$category])) { $categories[$category] = array(); } $categories[$category][] = array( 'Item' => $item, 'Description' => $description ); } Once you have that structure you can use it like this: foreach($categories as $name => $data) { echo "<optgroup label='$name'>"; foreach($data as $item) { echo "<option value='{$item['Item']}'>{$item['Item']} ({$item['Description']})</option>"; } echo '</optgroup>'; } Link to comment Share on other sites More sharing options...
coolshrimp Posted January 3, 2016 Author Share Posted January 3, 2016 I getParse error: syntax error, unexpected end of file Link to comment Share on other sites More sharing options...
Ingolme Posted January 3, 2016 Share Posted January 3, 2016 That's a syntax error, you should be able to find it. Look for any unclosed parentheses or braces or a line missing a semi-colon. I don't write code to be copied and pasted, but as a learning experience. Link to comment Share on other sites More sharing options...
coolshrimp Posted January 3, 2016 Author Share Posted January 3, 2016 That's a syntax error, you should be able to find it. Look for any unclosed parentheses or braces or a line missing a semi-colon. I don't write code to be copied and pasted, but as a learning experience. i found what was missing but when i print out array to a page it is blank.. <?php $categories = array(); $count = 0; foreach ($CSVArray as $row) { if ($count > 1 && !empty($row[0])) { $category = $row[0]; $item = $row[3]; $description = $row[4]; if(!isset($categories[$category])) { $categories[$category] = array(); } $categories[$category][] = array( 'Item' => $item, 'Description' => $description ); } } //List categories on screen print_r ($categories); ?> Link to comment Share on other sites More sharing options...
Ingolme Posted January 3, 2016 Share Posted January 3, 2016 Looks like there's a $count++ missing at the end of the loop. Link to comment Share on other sites More sharing options...
coolshrimp Posted January 4, 2016 Author Share Posted January 4, 2016 Thank you very much i got it working. http://demosites.com/Refugee/ now i work on comparing 2 arrays and not showing items if someone has already registered for an item. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now