Jump to content
Larry Ullman's Book Forums

Recommended Posts

I originally posted this under the existing multidimensional array posting, but it ought to be here.

 

I'm having trouble sorting a database driven multidimensional array. I'm taking my values from a select statement that returns all of the values in the business_type table. The function writing the array looks like this, complete with comments blocking out other things I've tried.

 static function arrayListerAll(PDO $pdo){
        $q = "SELECT busTypeID, name, description, buttonType, masterType FROM business_type WHERE busTypeID != 1 ORDER BY busTypeID";
        try {
            $bt = array();
            $result = $pdo->query($q);
            if ($result->rowCount()== 0){
                throw new Exception("There was an error retrieving the results from the database.");
            }
            $result->setFetchMode(PDO::FETCH_ASSOC);
            //while (list($busTypeID, $name, $description, $buttonType, $masterType) = $result->fetch()){
            while ($row = $result->fetch()){
                $bt[$row['masterType']][$row['busTypeID']] = array('name' => $row['name'], 'description' => $row['description'], 'buttonType' => $row['buttonType']);
                //$bt[$masterType][$busTypeID] = $name;
                //$bt[$row['busTypeID']] = array('name' => $row['name'], 'description' => $row['description'], 'buttonType' => $row['buttonType'], 'busTypeID' => $row['busTypeID'], 'masterType' => $row['masterType']);
            }
            /*function arraySorter($x, $y){
        return ($x['masterType'] > $y['masterType']);
    }
            usort($bt, 'arraySorter');*/
            return $bt;
        } catch (PDOException $ex) {
            print $ex->getMessage();

        }
    }

 

*********This returns the array, which is used by another function that is called in a view.

 

<div id="testDiv" style="color: white">
        <?php
        $bt = BusinessType::arrayListerAll($pdo);
        print_r($bt);
        AdminArrayWriter::makeList($bt, $bt);
        //foreach ($bt as $busTypeID){
        //   print_r($busTypeID) . "<br>";
        //}
       /*function makeList($parent) {
            global $bt;
            //echo '<ol>';
            foreach ($parent as $busTypeID => $type) {
                //echo "<li>" . $type['name']. "  ". $type['buttonType'];
                //echo "<li>$type";
                if (isset($type['name'])) {
                    AdminArrayWriter::listBusTypeWriter($type['name'], $type['description'], $type['buttonType']);
                }
                if (isset($bt[$busTypeID])) {
                    makeList($bt[$busTypeID]);
                }
                //echo "</li>";
            }
            //echo "</ol>";
        }*/

       // makeList($bt);
        ?>
    </div>

 

***********The called function is written like this.

 

class AdminArrayWriter {
    static function makeList($parent,$bt) {
             $bta =$bt;
            //echo '<ol>';
            foreach ($parent as $busTypeID => $type) {
                //echo "<li>" . $type['name']. "  ". $type['buttonType'];
                //echo "<li>$type";
                if (isset($type['name'])) {
                    AdminArrayWriter::listBusTypeWriter($type['name'], $type['description'], $type['buttonType']);
                }
                if (isset($bta[$busTypeID])) {
                    self::makeList($bta[$busTypeID], $bta);
                }
                //echo "</li>";
            }
            //echo "</ol>";
        }

No matter if I sort the array this way, or sort it with the commented out makeList() function, which you can see that this function was built from (using the one in the book as a basis), I get a sorted list by masterType well enough, but I also get all of this extra stuff. The extra stuff is every subgroup, and their subgroupings, listed again until it all peters out. At least the extra stuff looks like that.

 

Is my problem to do with how I am calling the recursive part of this function, given what kind of array I've got put together that holds all of this? Maybe it has to do with how I put the array together in the first place? Seeing as how I need the 'buttonType' value from the table in order to determine which kind of writer to choose when the array values are actually written out into html, it determines what class an element will have which drives a style sheet to present things differently, I had to make the original array more complicated than the book's example.

The book was

 

$tasks[$parent_id][$task_id] = $task --- on page 20

 

and my array is

 

while ($row = $result->fetch()){
                $bt[$row['masterType']][$row['busTypeID']] = array('name' => $row['name'], 'description' => $row['description'], 'buttonType' => $row['buttonType']);

 

It looks the same, but might be creating something more complicated? I don't know.

 

Anyway, I can get this to sort the array alright, but I can't get it to stop there.It does keep going through and writing the additional, even if I stick to the original makeList() function, which is taken from the book.

 

I get the spill back, where the listing for the array goes into the subcategories, even when I pare down the list to a more specific list, which is not a selection of everything in the database. So the length of it doesn't seem the matter. I'm positive I'm mismatching the array creation and the array calls, but I don't know enough about how this multi-dimensional array stuff works, both for the creation and, especially, the calling to be able to figure it out. When I print out just the array it looks right.

 

Here is the top of it through the first first position switch, which shows how the masterType changes.

 

Array
(
[3] => Array
(
[91] => Array
(
[name] => Transportation
[description] =>
[buttonType] => master
)

[92] => Array
(
[name] => Gasoline
[description] =>
[buttonType] => master
)

[93] => Array
(
[name] => Clothing
[description] =>
[buttonType] => master
)

[94] => Array
(
[name] => Construction Supply
[description] =>
[buttonType] => potential
)

[95] => Array
(
[name] => Footwear
[description] =>
[buttonType] => potential
)

[96] => Array
(
[name] => Furniture
[description] =>
[buttonType] => potential
)

[97] => Array
(
[name] => Grocery
[description] =>
[buttonType] => master
)

[98] => Array
(
[name] => Household
[description] =>
[buttonType] => master
)

[99] => Array
(
[name] => Jewelry
[description] =>
[buttonType] => potential
)

[100] => Array
(
[name] => Marijuana
[description] =>
[buttonType] => master
)

[101] => Array
(
[name] => Office Supplies
[description] =>
[buttonType] => potential
)

[102] => Array
(
[name] => Recreational
[description] =>
[buttonType] => potential
)

[103] => Array
(
[name] => Eyewear
[description] =>
[buttonType] => potential
)

[104] => Array
(
[name] => Department Store
[description] =>
[buttonType] => potential
)

[105] => Array
(
[name] => Pet Supplies
[description] =>
[buttonType] => potential
)

[106] => Array
(
[name] => Discount Store
[description] =>
[buttonType] => potential
)

[107] => Array
(
[name] => Liquor Store
[description] =>
[buttonType] => potential
)

[108] => Array
(
[name] => Book Store
[description] =>
[buttonType] => potential
)

[109] => Array
(
[name] => Art
[description] =>
[buttonType] => master
)

[110] => Array
(
[name] => Hobbies
[description] =>
[buttonType] => potential
)

[111] => Array
(
[name] => Toys
[description] =>
[buttonType] => potential
)

[112] => Array
(
[name] => Convenience Store
[description] =>
[buttonType] => potential
)

[113] => Array
(
[name] => Computers
[description] =>
[buttonType] => potential
)

[114] => Array
(
[name] => Craft Store
[description] =>
[buttonType] => potential
)

[115] => Array
(
[name] => Fabric/Sewing Store
[description] =>
[buttonType] => potential
)

[116] => Array
(
[name] => Science Supplies
[description] =>
[buttonType] => potential
)

[117] => Array
(
[name] => Phones
[description] =>
[buttonType] => potential
)

[118] => Array
(
[name] => Florists
[description] =>
[buttonType] => potential
)

)

[91] => Array
(
[119] => Array
(
[name] => Auto Parts
[description] =>
[buttonType] => sub
)

[120] => Array
(
[name] => Motorcycle Sales/Repair
[description] =>
[buttonType] => sub
)

 

*******See how the masterType has changed from 3 to 91.

 

And here is the list I get from a particular array. See how after 28, Florists the list goes back to what has already been categorized and sorted previously/

 

    1. Transportation master
      1. Auto Parts sub
      2. Motorcycle Sales/Repair sub
      3. Small Craft Sales/Repair sub
      4. Auto Sales sub
    2. Gasoline master
      1. Gas Station sub
      2. Gasoline/Convenience Store sub
      3. Gasoline/Service Station sub
    3. Clothing master
      1. Clothing New sub
      2. Clothing Used sub
      3. Clothing Specialty sub
    4. Construction Supply potential
    5. Footwear potential
    6. Furniture potential
    7. Grocery master
      1. Traditional Grocer sub
      2. Natural/Vitamin Grocer sub
      3. Boutique Grocer sub
    8. Household master
      1. Household - Electronics sub
      2. Household - Small Goods sub
      3. Household - Appliances sub
      4. Household - General sub
      5. Hardware sub
    9. Jewelry potential
    10. Marijuana master
      1. Marijuana - Medical sub
      2. Marijuana - Recreational sub
      3. Marijuana - Medical/Recreational sub
    11. Office Supplies potential
    12. Recreational potential
    13. Eyewear potential
    14. Department Store potential
    15. Pet Supplies potential
    16. Discount Store potential
    17. Liquor Store potential
    18. Book Store potential
    19. Art master
      1. Art Gallery sub
      2. Art Supplies sub
      3. Framing sub
    20. Hobbies potential
    21. Toys potential
    22. Convenience Store potential
    23. Computers potential
    24. Craft Store potential
    25. Fabric/Sewing Store potential
    26. Science Supplies potential
    27. Phones potential
    28. Florists potential
    1. Auto Parts sub
    2. Motorcycle Sales/Repair sub
    3. Small Craft Sales/Repair sub
    4. Auto Sales sub
    1. Gas Station sub
    2. Gasoline/Convenience Store sub
    3. Gasoline/Service Station sub
    1. Clothing New sub
    2. Clothing Used sub
    3. Clothing Specialty sub
    1. Traditional Grocer sub
    2. Natural/Vitamin Grocer sub
    3. Boutique Grocer sub
    1. Household - Electronics sub
    2. Household - Small Goods sub
    3. Household - Appliances sub
    4. Household - General sub
    5. Hardware sub
    1. Marijuana - Medical sub
    2. Marijuana - Recreational sub
    3. Marijuana - Medical/Recreational sub
    1. Art Gallery sub
    2. Art Supplies sub
    3. Framing sub
Link to comment
Share on other sites

  • 2 weeks later...

Sorry for the delayed reply. It's a rather long post with custom code, not easy to parse. I think the problem is that you're passing the same array to the makeList as both the parent and the $bt. In the original code, only the top-level item is passed as the parent (and then the whole array is fetched from a global var). That's where I'd start working to debug this. 

Link to comment
Share on other sites

 Share

×
×
  • Create New...