Search the Community
Showing results for tags 'recursion'.
-
I have the following arrays: //$this->links is an array with the following Array ( [-1] => Array ( [text] => Home [url] => pages/HOME.htm ) [1] => Array ( [text] => About us [url] => pages/About_us.htm ) [2] => Array ( [text] => Services [url] => pages/Services.htm ) [3] => Array ( [text] => Portfolio [url] => pages/Portfolio.htm ) [4] => Array ( [text] => FAQs [url] => pages/FAQs.htm ) [5] => Array ( [text] => Contact Us [url] => pages/Contact_Us.htm ) ) //$this->PrItems is an array with the following Array ( [1] => 886_P [2] => 962_P ) //$this->chiItems is an array with the following Array ( [1] => Array ( [886_P] => Array ( [959_P] => Array ( [text] => Overview [url] => pages/Overview.htm ) [960_P] => Array ( [text] => Directors [url] => pages/Directors.htm ) [961_P] => Array ( [text] => Representatives [url] => pages/Representatives.htm ) ) ) [2] => Array ( [962_P] => Array ( [965_P] => Array ( [text] => Custom Designed Homes [url] => pages/Custom_Designed_Homes.htm ) [966_P] => Array ( [text] => Pre-Designed Homes [url] => pages/Pre-Designed_Homes.htm ) [967_P] => Array ( [text] => Ready to Occupy [url] => pages/Ready_to_Occupy.htm ) ) ) ) To get to [text] => Directors i get backwards as follows: [960_P] then [886_P] then [1] in $this->PrItems THEN match [1] (parent above ) with [1] in $this->links to get [text] => About us I wanted to achieve a result like (using above): About Us .:. Directors Like a Breadcrumb What i have tried is: foreach($this->links as $Lnkkey=>$val) { if(strtolower($val['text']) == $page) { //$ParentId=$Lnkkey; echo '<li class="active"><a href="#" onclick="return false;" >'.@$val['text'].'</a></li>'; } else { $breadcrumb='<li class="active"><a href="#" onclick="return false;" >'.@$val['text'].'</a></li>'; $parentkey = find_parent($this->chItems, $page); echo $parentkey; } } function find_parent($array, $needle, $parent = null) { foreach ($array as $key => $value) { if (is_array($value)) { $pass = $parent; if (is_string($key)) { $pass = $key; } $found = find_parent($value, $needle, $pass); if ($found !== false) { return $found; } } else if ($key === 'text' && $value === $needle) { return $parent; } } return false; } thanks in advance
-
I have a cart function that is supposed to use the post array (with product numbers as indices and the associated value is the customer's requested quantity) and populate product information in the session by accessing the database. I would like this function to add a $key and $value pair to $prodDetail for each item in the post array. Instead, it is replacing all the $value entries with the most recently selected product data. I do not see why it is resetting all of the $value fields upon each new iteration. Please let me know if this is still unclear after you have read the following. Here is the function that I believe is the culprit: function prodArr() { if (!empty($_POST['cart'])) { global $dbType; $prodDetail = Array(); //connect to database $mysqli = Database::getInstance(); //Retrieve product data if ($dbType === 'distro') { $results = array('img' => &$img, 'artist' => &$artist, 'title' => &$title, 'label' => &$label, 'year' => &$year, 'price' => &$price, 'qty' => &$qty); $tbl = 'products'; } elseif ($dbType === 'releases') { $results = array('img' => &$img, 'artist' => &$artist, 'title' => &$title, 'year' => &$year, 'price' => &$price, 'qty' => &$qty); $tbl = 'products'; } elseif ($dbType === 'merch') { $results = array('img' => &$img, 'title' => &$title,'size' => &$size, 'color' => &$color, 'sex' => &$sex, 'price' => &$price, 'qty' => &$qty); $tbl = 'merch'; } $query = 'SELECT '; $query .= '`'.implode('`, `', array_keys($results)).'`'; $query .= ' FROM ' . $tbl . ' WHERE (`id` = ?) AND (`qty` > 0) AND (`agedOff` <> 1);'; foreach ($_POST['cart'] as $elKey => $element) { if (!$stmt = $mysqli->prepare($query)) { echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error; } if (!$stmt->bind_param('s', $elKey)) { echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error; } if (!$stmt->execute()) { echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; } if (!call_user_func_array(array($stmt, 'bind_result'), $results)) { echo "Binding results failed: (" . $stmt->errno . ") " . $stmt->error; } if (!$stmt->fetch()){ echo "Fetching results failed: (" . $stmt->errno . ") " . $stmt->error; } $prodDetail[$elKey] = $results; printArray($prodDetail); I do not show the entire function, because I believe this is where the problem lies. Through dumping and echoing variables, I show that the $results array changes through each iteration of the foreach loop, but unfortunately it is setting all of the child arrays in $prodDetail to match the current $results array. All I want to do is add each new result set to the array. Maybe I have been staring at this for too long, but I can't seem to see the problem. Can someone please point how to fix this?