how to define custom helper in default.ctp file? - php

i created custom helper to build dynamic menu and i need to use this helper in all my site pages so i put the code to show menu in element and include it in default.ctp like this
<?php echo $this->element('menu'); ?>
,, but the default.ctp not defined the helper so how to define this helper in all views it give me this errors
Notice (8): Undefined variable: data [APP\views\elements\menu.ctp, line 5]
Warning (2): Invalid argument supplied for foreach() [APP\views\helpers\tree.php, line 28]
helpers/tree.php
<?php
class TreeHelper extends Helper
{
var $tab = " ";
var $helpers = array('Html');
// Main Function
function show($name, $data, $style='')
{
list($modelName, $fieldName) = explode('/', $name);
if ($style=='options') {
$output = $this->selecttag_options_array($data, $modelName, $fieldName, $style, 0);
} else {
//$style='';
$output = $this->list_element($data, $modelName, $fieldName, $style, 0);
}
return $this->output($output);
}
// This creates a list with optional links attached to it
function list_element($data, $modelName, $fieldName, $style, $level)
{
$tabs = "\n" . str_repeat($this->tab, $level * 2);
$li_tabs = $tabs . $this->tab;
$output = $tabs. "<ul>";
foreach ($data as $key=>$val)
{
$output .= $li_tabs . "<li>".$this->style_print_item($val[$modelName], $modelName, $style);
if(isset($val['children'][0]))
{
$output .= $this->list_element($val['children'], $modelName, $fieldName, $style, $level+1);
$output .= $li_tabs . "</li>";
}
else
{
$output .= "</li>";
}
}
$output .= $tabs . "</ul>";
return $output;
}
// this handles the formatting of the links if there necessary
function style_print_item($item, $modelName, $style='')
{
switch ($style)
{
case "link":
$output = $this->Html->link($item['name'], "view/".$item['id']);
break;
case "admin":
$output = $item['name'];
$output .= $this->Html->link(" edit", "edit/".$item['id']);
$output .= " ";
$output .= $this->Html->link(" del", "delete/".$item['id']);
break;
default:
$output = $item['name'];
}
return $output;
}
// recursively reduces deep arrays to single-dimensional arrays
// $preserve_keys: (0=>never, 1=>strings, 2=>always)
// Source: http://php.net/manual/en/function.array-values.php#77671
function array_flatten($array, $preserve_keys = 1, &$newArray = Array())
{
foreach ($array as $key => $child)
{
if (is_array($child))
{
$newArray =& $this->array_flatten($child, $preserve_keys, $newArray);
}
elseif ($preserve_keys + is_string($key) > 1)
{
$newArray[$key] = $child;
}
else
{
$newArray[] = $child;
}
}
return $newArray;
}
// for formatting selecttag options into an associative array (id, name)
function selecttag_options_array($data, $modelName, $fieldName, $style, $level)
{
// html code does not work here
// tried using " " and it didn't work
$tabs = "-";
foreach ($data as $key=>$val)
{
$output[] = array($val[$modelName]['id'] => str_repeat($tabs, $level*2) . ' ' . $val[$modelName]['name']);
if(isset($val['children'][0]))
{
$output[] = $this->selecttag_options_array($val['children'], $modelName, $fieldName, $style, $level+1);
}
}
$output = $this->array_flatten($output, 2);
return $output;
}
}
?>
elements/menu.ctp
<!-- This will turn the section name into a link -->
<h3>Basic hierarchical list with name as link</h3>
<?php echo $tree->show('Section/name', $data, 'link'); ?>

You have to define it in your AppController, located in the root of your app directory (and if it's not there, just create a file called app_controller.php You can use the file with the same name in the Cake core directory as a template for this file.
When you have your app_controller, add the following
var $helpers = array('Tree');
You might want to add some other standard helpers like Html, Form and Javascript in here as well. All the helpers that are in AppController will be available to all of your controllers.

Related

PHP retrieve object's getter methods only once in an array of objects

I've got an array of 20 objects that are all the same. These objects are all the same and contain a couple of properties and some getters and setters. I'm converting the property data to an HTML table like so:
public function addBody($objects) {
$ret = (string) NULL;
foreach($objects as $object) {
$ret .= '<tr>';
$methods = get_class_methods($object);
foreach($methods as $method) {
if(strpos($method, 'get') !== false) {
$ret .= '<td>' . call_user_func(array($object, $method)) . '</td>';
}
}
$ret .= '</tr>';
}
return $ret;
}
I'm iterating through my array of objects and then I get all methods of each object where I filter on only the getters (with strpos). The function works but retrieving all object methods is a waste of time. A solution I could think of is getting the first object and retrieve all its methods (getters) and use that in my addBody function.
Would this be a good solution of is there a better one?
Check this:
public function addBody($objects) {
$ret = '';
$obectMethods = get_class_methods(current($objects));
$methods = array_filter($obectMethods, function($method) {
return strpos($method, 'get') !== false;
});
foreach($objects as $object) {
$ret .= '<tr>';
foreach($methods as $method) {
$ret .= '<td>' . call_user_func(array($object, $method)) . '</td>';
}
$ret .= '</tr>';
}
return $ret;
}
First we retrieve methods from first object and use them in foreach loop.
I'm not sure about your definition of better, but sure there is another one... Less code and one getter to access any property for your specific case.
Our simple entity:
class myEntity {
private $name;
private $age;
public function __get($property) {
if (property_exists($this, $property)) {
return $this->$property;
}
return null;
}
}
and then in your method:
public function addBody($entities, $properties) {
$ret = '';
foreach($entities as $entity) {
$ret .= '<tr>';
foreach($properties as $property) {
$ret .= '<td>' . $entity->__get($property) . '</td>';
}
$ret .= '</tr>';
}
return $ret;
}
where you have a list of entities:
$entities = array ( new myEntity(), new myEntity());
$properties = array ('name', 'age');
var_dump($object->addBody($entities, $properties));

PHP - recursive function foreach

I would to create a recursive function in order to retrieve data from an array and to organize then.
However I have some difficulties to create the right logic. The principle must be apply to any sub level until it ends or nothing is found.
I want to prevent this kind of code by repeating a foreach inside a foreach...:
$cats = get_categories($args);
$categories = array();
foreach($cats as $cat){
$parent = $cat->category_parent;
if ($parent) {
$categories['child'][$parent][$cat->cat_ID] = $cat->name;
} else {
$categories['parent'][$cat->cat_ID] = $cat->name;
}
}
}
if (isset($categories['parent']) && !empty($categories['parent'])) {
foreach($categories['parent'] as $id => $cat){
$new_cats[$id]['title'] = $cat;
if (isset($categories['child'][$id])) {
foreach($categories['child'][$id] as $child_id => $child_cat){
$new_cats[$child_id]['title'] = $child_cat;
$new_cats[$child_id]['parent_id'] = $id;
if (isset($categories['child'][$child_id])) {
foreach($categories['child'][$child_id] as $sub_child_id => $sub_child_cat){
$new_cats[$sub_child_id]['title'] = $sub_child_cat;
$new_cats[$sub_child_id]['parent_id'] = $child_id;
}
}
}
}
}
}
}
May be this code help you to get idea for formatting your desired array format.
<?php
// Main function
function BuildArr($arr)
{
$formattedArr = array();
if(!empty($arr))
{
foreach($arr as $val)
{
if($val['has_children'])
{
$returnArr = SubBuildArr($val['children']); // call recursive function
if(!empty($rs))
{
$formattedArr[] = $returnArr;
}
}
else
{
$formattedArr[] = $val;
}
}
}
return $formattedArr;
}
// Recursive Function( Build child )
function SubBuildArr($arr)
{
$sub_fortmattedArr = array();
if(!empty($arr))
{
foreach($arr as $val)
{
if($val['has_children'])
{
$response = SubBuildArr($val['children']); // call recursive
if(!empty($response))
{
$sub_fortmattedArr[] = $response;
}
}
else
{
$sub_fortmattedArr[] = $arr;
}
}
return $sub_fortmattedArr;
}
}
?>
I use this code for my previous project for generating categories that upto n-th level

PHP - variable scope in recursively function

First assumption: Assume, we defined a variable (its name is $tmp) in a function(functioin name is 'ExpMenu') for temporary calculating and in end of function we return this variable.
Second assumption: Assume, we call that function recursively for create a navigation menu base on a multidimensional array.
My question is about scope of that variable ($tmp). In every call funtion, will its value overwritten? In other words, by every function call we lose previous value?
For more detail, please review below code:
/// --- { Declaration Block
$content = array(
array(
'level'=>'1',
'order'=>'1',
'text'=>'New Solution WorkFlow',
'is_parent'=>'yes',
'child'=> array(
array(
'level'=>'2',
'order'=>'1',
'text'=>'Define New Solution',
'is_parent'=>'no',
'url'=>'#'
),
array(
'level'=>'2',
'order'=>'2',
'text'=>'View Solutions',
'is_parent'=>'no',
'url'=>'#'
),
array(
'level'=>'2',
'order'=>'3',
'text'=>'View Solutions',
'is_parent'=>'no',
'url'=>'#'
)
)
),
array(
'level'=>'1',
'order'=>'2',
'text'=>'Solution Modify WorkFlow',
'is_parent'=>'yes',
'child'=> array(
array(
'level'=>'2',
'order'=>'1',
'text'=>'Request For Solution Modify',
'is_parent'=>'no',
'url'=>'#'
)
)
),
array(
'level'=>'1',
'order'=>'3',
'text'=>'Solution Close WorkFlow',
'is_parent'=>'yes',
'child'=> array(
array(
'level'=>'2',
'order'=>'1',
'text'=>'Declare For Solution Close',
'is_parent'=>'no',
'url'=>'#'
)
)
)
);
function ExpMenu($item_array ) {
$tmp='';
foreach ($item_array as $item) {
if ($item['is_parent']=='yes') {
$tmp = '<li class="hasChild">' . $item["text"] . '<ul>';
$tmp .= ExpMenu($item['child']);
$tmp .= '</ul></li>';
} else {
$tmp = '<li>';
$tmp .= ''. $item['text'] . '' ;
$tmp .= '</li>';
}
}
return $tmp;
}
/// --- }
$menu='<div><ul>';
$menu .= ExpMenu($content);
$menu.='</ul></div>';
echo $m . '<br />';
It seams by every call function we lose pervious value.
I thank #l0rkaY for her/him solution, But I found another solution that doesn't need add new parameter in my function.
Because $tmp scope is in 'ExpMenu' function and we call recursively function, therefore variable still alive and wasn't terminated.
So, I modify my function a bit:
function ExpMenu($item_array ) {
$tmp='';
foreach ($item_array as $item) {
if ($item['is_parent']=='yes') {
$tmp .= '<li class="hasChild">' . $item["text"] . '<ul>';
$tmp .= ExpMenu($item['child']);
$tmp .= '</ul></li>';
} else {
$tmp .= '<li>';
$tmp .= ''. $item['text'] . '' ;
$tmp .= '</li>';
}
}
return $tmp;
}
I assume your actual problem is, that your function generates only single item with a single child item.
It's because you are overwriting your previous item in your if/else blocks. That's why you only get the last item.
You just need to concatenate them to the existing items.
function ExpMenu($item_array ) {
$tmp='';
foreach ($item_array as $item) {
if ($item['is_parent']=='yes') {
$tmp .= '<li class="hasChild">' . $item["text"] . '<ul>';
$tmp .= ExpMenu($item['child']);
$tmp .= '</ul></li>';
} else {
$tmp .= '<li>';
$tmp .= ''. $item['text'] . '' ;
$tmp .= '</li>';
}
}
return $tmp;
}

Foreach act wierd

i try to make an simple way to create an box in a class.
The problem is , it only give me the first element in the array. I echo out the $values and i get the whole css code and i try to place them in style at div. But still get only the last element.
My currently code looks like:
class general {
public function box($content,$style,$width = 50,$height = 50) {
foreach ($style as $k => $v) {
$values = ''.$k.':'.$v.';';
echo($values);
$box = '<div class="testBox" style="'.$values.'">'.$content.'</div> ';
}
return $box;
}
}
$general = new general();
$test = array(
'background-color' => '#000',
'font-size' => '120px'
);
echo $general->box('testValue',$test);
Try like this:
public function box($content,$style,$width = 50,$height = 50) {
$values = '';
foreach ($style as $k => $v) {
$values .= ''.$k.':'.$v.';';
}
$box = '<div class="testBox" style="'.$values.'">'.$content.'</div> ';
return $box;
}
$box = '<div class="testBox" style="'.$values.'">'.$content.'</div> ';
to
$box .= '<div class="testBox" style="'.$values.'">'.$content.'</div> ';
And declare
$box = ''; outside the loop.
You need to concate the data using .

PHP Warning Help?

I keep getting the following warning listed below on line 3.
Warning: Invalid argument supplied for foreach()
Here is the php code.
function dyn_menu($parent_array, $sub_array, $qs_val = "menu", $main_id = "nav", $sub_id = "subnav", $extra_style = "foldout") {
$menu = "<ul id=\"".$main_id."\">\n";
foreach ($parent_array as $pkey => $pval) {
if (!empty($pval['count'])) {
$menu .= " <li><a class=\"".$extra_style."\" href=\"".$pval['link']."?".$qs_val."=".$pkey."\">".$pval['label']."</a></li>\n";
} else {
$menu .= " <li>".$pval['label']."</li>\n";
}
if (!empty($_REQUEST[$qs_val])) {
$menu .= "<ul id=\"".$sub_id."\">\n";
foreach ($sub_array as $sval) {
if ($pkey == $_REQUEST[$qs_val] && $pkey == $sval['parent']) {
$menu .= "<li>".$sval['label']."</li>\n";
}
}
$menu .= "</ul>\n";
}
}
$menu .= "</ul>\n";
return $menu;
}
Here is the whole code I'm working on.
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT id, label, link_url, parent_id FROM dyn_menu ORDER BY parent_id, id ASC");
if (!$dbc) {
// There was an error...do something about it here...
print mysqli_error();
}
while ($obj = mysqli_fetch_assoc($dbc)) {
if (empty($obj['parent_id'])) {
echo $parent_menu . $obj['id']['label'] = $obj['label'];
echo $parent_menu . $obj['id']['link'] = $obj['link_url'];
} else {
echo $sub_menu . $obj['id']['parent'] = $obj['parent_id'];
echo $sub_menu . $obj['id']['label'] = $obj['label'];
echo $sub_menu . $obj['id']['link'] = $obj['link_url'];
echo $parent_menu . $obj['parent_id']++;
}
}
mysqli_free_result($dbc);
function dyn_menu($parent_array, $sub_array, $qs_val = "menu", $main_id = "nav", $sub_id = "subnav", $extra_style = "foldout") {
$menu = "<ul id=\"".$main_id."\">\n";
foreach ($parent_array as $pkey => $pval) {
if (!empty($pval['count'])) {
$menu .= " <li><a class=\"".$extra_style."\" href=\"".$pval['link']."?".$qs_val."=".$pkey."\">".$pval['label']."</a></li>\n";
} else {
$menu .= " <li>".$pval['label']."</li>\n";
}
if (!empty($_REQUEST[$qs_val])) {
$menu .= "<ul id=\"".$sub_id."\">\n";
foreach ($sub_array as $sval) {
if ($pkey == $_REQUEST[$qs_val] && $pkey == $sval['parent']) {
$menu .= "<li>".$sval['label']."</li>\n";
}
}
$menu .= "</ul>\n";
}
}
$menu .= "</ul>\n";
return $menu;
}
function rebuild_link($link, $parent_var, $parent_val) {
$link_parts = explode("?", $link);
$base_var = "?".$parent_var."=".$parent_val;
if (!empty($link_parts[1])) {
$link_parts[1] = str_replace("&", "##", $link_parts[1]);
$parts = explode("##", $link_parts[1]);
$newParts = array();
foreach ($parts as $val) {
$val_parts = explode("=", $val);
if ($val_parts[0] != $parent_var) {
array_push($newParts, $val);
}
}
if (count($newParts) != 0) {
$qs = "&".implode("&", $newParts);
}
return $link_parts[0].$base_var.$qs;
} else {
return $link_parts[0].$base_var;
}
}
echo dyn_menu($parent_menu, $sub_menu, "menu", "nav", "subnav");
It's telling you that $parent_array isn't an array.
If you post the code that calls this function, we can tell you more.
Are you sure $parent_array is actually an array? Try checking it with is_array first (perhaps returning an empty string to represent the menu or whatever - adapt to your needs):
if (!is_array($parent_array)) {
return "";
}
This error happens when you supply not an array into forearch. Try print_r() first argument of every foreach
If you change your function signature to include type hinting (only works for arrays and objects), you'll be sure that your function gets what it needs:
function dyn_menu(array $parent_array, array $sub_array, //etc.)
And you should get an error message that pinpoints the caller of the function, which is where the problem really is.
It looks like you were expecting to build $parent_array in that while loop at the beginning. Instead it's just echoing stuff.
The lines like:
echo $parent_menu . $obj['id']['label'] = $obj['label'];
Should probably be like:
$menu['label'] = $obj['label'];
Then at the end (inside) of the loop add something like:
$parent_menu[$obj['id']] = $menu;
So you build the array you're using in dyn_menu.
In any case, the while loop looks like your problem. It's not building $parent_menu from the data.

Categories