PutASockInIt
-
Posts
2 -
Joined
-
Last visited
Posts posted by PutASockInIt
-
-
The thing is, Larry is the best book teacher I've read, but I didn't just buy his book. I have read both. Larry's book fills in a lot that Zandstra's doesn't about things other than objects, like sorting multi-dimensional arrays (problems with which brought me here). Larry also demonstrates various ways of doing things that save time, like how he brings in a utilities file in order to always start a session and tie in the database when using PDO. Zandstra, however, covers more patterns and goes deeper into the sort of acceptable styles you might code things, as you iterate through the development process. You know, as you clean up and refactor your code, or as you discover better ways to follow object oriented methodology.
Adapting The Multi Dimensional Array Sort (Recursive Sort)
in PHP Advanced and Object-Oriented Programming: Visual QuickPro Guide (3rd Edition)
Posted
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/