There is many example n level tree create and tree traversal but i could not understand how do i traverse the tree in codeigniter view page with out calling recursive function.I think it is not best practice to call recursive function in view page.So then how do traverse the multidimensional array for create dropdown.
This is my code in view page.how do i improve it
value="0">Root Level
$html .= ''.$valArr['category'].'';
if(array_key_exists('children', $valArr)){ $html .=
generateCategoryList($valArr['children']); } } return $html; } ?>
Related
I need help for a custom post type archive page.
I want to build three filters for a custom post type with two taxonomies registered. The first taxonomy is hierarchical like category, the second not hierarchical like tag.
The first filter is for hierarchical taxonomy parent elements. The second filter for non hierarchical taxonomy . The third is for hierarchical taxonomy child elements.
I want to make the first filter( hierarchical taxonomy parent elements) feed the second(non hierarchical taxonomy elements) and third(hierarchical taxonomy child elements), and then show results. Any ideas?
First, I have a ul list with all parent elements of the first taxonomy. By clicking on a specific li element, I am calling an ajax function that changes the second filter that contains all the child elements of the parent taxonomy.
I try to find a way to make get_terms() work in functions.php so as to use it in an ajax function. I get variable $product_type_id through ajax javascript. It is a variable to store the parent taxonomy term_id, and want to use it so as to get the children of the specific product type.
function getCurrentTerm($product_type_id){
global $product_type_id;
$my_products = get_terms([
'taxonomy' => ['awarded_product'],
'hide_empty' => false,
'parent' => $product_type_id ,
'order' => 'DESC'
]);
return $my_products;
}
function set_product_type_filter(){
$product_type_id = $_POST['product_type_id'];
if( $_POST['product_type_id']!=''){
$my_product = getCurrentTerm($product_type_id);
$response['product_type_id'] = $_POST['product_type_id'];
$response['product']=$my_product;
echo json_encode($response);
wp_reset_postdata();
wp_die();
}
}
add_action('wp_ajax_set_product_type_filter', 'set_product_type_filter');
add_action('wp_ajax_nopriv_set_product_type_filter', 'set_product_type_filter');
I've created a module for a payment method. Since this method is not well-known, I would like to place an image next to the name in the payment block, and link that to our faq.
What's the best way to place that image there ?
Some posts suggested putting the HTML straight into the title of the name; that seems to work, but the name is also used in other places (for example in the admin) and it seems like a hack.
So far the best I came up with was to add some jquery code to inject the html into the onepage checkout page, but I can only make that work if I observe the steps through the carrousel and inject the html after the payment section expands, because magento resets the html for it.
Maybe create a function in your block that returns the name and optionally returns the image.
Here is some pseudocode:
function getName($addImage = 0){
$html = '<span class="my-payment-name">' . $this->getName();
if ($addImage == 1) :
$html = $html . '<img src="' . $this->getImage() . '" />';
endif;
$html = $html . '</span>;
return $thml;
}
I'm very new to CI. :)
In my project, I have separated the page into header, footer and body. And the body portion(view) is loaded based on the controller. The header and footer are common to all pages.
For example, for a login page it would be like this:
$this->load->view('header');
$this->load->view('login');
$this->load->view('footer');
But now my concern is, how to generate the "category" section (which will list several category names on the left of the body portion). Upon clicking a category, the corresponding details page would be show to the right(ie. in content portion). So, in all views(all pages) I need to display the list of categories.
Visual example:
----------------------------
Header Portion of Page
----------------------------
Body Portion
============
Cat1 |
Cat2 |
Cat3 | Content
Cat4 |
Cat5 |
----------------------------
Footer
----------------------------
These categories are populated from the data in db.
I have just done some searching. So, I am thinking about creating a helper class and autoload it. So, in all views, I would call the function and and echo the result.
For eg:
function hlp_getCategories()
{
$ci =& get_instance();
$q = $ci->db->query('SELECT cat_name FROM tblCategories');
return $q;
}
And in the view:
<?php
$q = hlp_getCategories();
foreach ($q->result_array() as $row)
{
echo anchor('cat/' . $row['cat_name'], $row['cat_name']) ;
}
?>
Is this the correct approach ?
Am I in the right track ?
Thanks in advance :)
That is one way to solve it - although if you follow a strict MVC approach - the helper would call the model $this->category->select_cat(), and put the SQL query in the model. Furthermore, the SQL query should use active record selections, not a text SQL query.
The other way to solve it is to use some CSS that has a DIV for the left menu (i.e. categories), and a DIV for the right (i.e. content).
Then you could do
$this->load->view('header');
$this->load->view('categories');
$this->load->view('login');
$this->load->view('footer');
Then inside your categories view
<div class = "left">
// show categories here
</div>
and inside your content views
<div class = "right">
// show content here
</div>
Ive been working with CI and I saw on the website of CI you can load a view as a variable part of the data you send to the "main" view, so, according the site (that says a lot of things, and many are not like they say ...ej pagination and others) i did something like this
$data['menu'] = $this->load->view('menu');
$this->load->view ('home',data);
the result of this is that I get an echo of the menu in the top of the site (before starts my body and all) and where should be its nothing, like if were printed before everything... I have no idea honestly of this problem, did anybody had the same problem before?
Two ways of doing this:
Load it in advance (like you're doing) and pass to the other view
<?php
// the "TRUE" argument tells it to return the content, rather than display it immediately
$data['menu'] = $this->load->view('menu', NULL, TRUE);
$this->load->view ('home', $data);
Load a view "from within" a view:
<?php
// put this in the controller
$this->load->view('home');
// put this in /application/views/home.php
$this->view('menu');
echo 'Other home content';
Create a helper function
function loadView($view,$data = null){
$CI = get_instance();
return $CI->load->view($view,$data);
}
Load the helper in the controller, then use the function in your view to load another one.
<?php
...
echo loadView('secondView',$data); // $data array
...
?>
I'm coming from CodeIgniter, and the terminology overlap between it and other MVC frameworks (particularly Zend) is giving me a mental block of some kind.
I want my users to visit http://mysite.com/do/this.
I understand that "do" is the Controller, and "this" is a function (well, method) within that Controller.
My site will have common elements like a header and sidebar; I understand that those go into a Layout, which will be incorporated into the final output.
I want the /do/this page to display three visual blocks of information (I'm deliberately not using the word "modules"). Let's call them BlockA, BlockB, and BlockC. Maybe one is a list of "new events" and another is a list of "new posts" and another is something else. Whatever. The trick is, these blocks of information will also be displayed on other pages of the site - say, http://mysite.com/did/that.
Both the "did" and the "do" Controllers (and the "this" and "that" methods, obviously) would be arranging BlockA, BlockB, and BlockC differently. Each Controller would have different criteria for what went into those blocks, too - one might be current information, while another might be archived information from the past.
I want to ensure that future programmers can easily alter the appearance of BlockA, BlockB, and/or BlockC without having to touch the code that populates their data, or the code which arranges them on each page.
So my general feeling is that BlockA, BlockB, and BlockC need to have their visual layout defined in a View - but that View wouldn't be specifically associated with either the "do" or the "did" Controllers. And the code which populates those blocks - that is, queries information from a database, selects the bits that are to be displayed, and whatnot - shouldn't reside entirely in those Controllers, either.
I started down the path of putting the logic - that is, assembling what will be displayed in each block - into Models. I feel I'm on the right path, there; both the "do" and "did" Controllers can thus summon the block-creation code via Models. But how (and where) do I abstract the visual element of those blocks, in such a way that the visual elements can also be shared by these two Controllers? Do the Models somehow load a View and output HTML to the Controllers (that doesn't feel right)? Or is there a way for the Controllers to run the Model, get the data to display, and then somehow feed it to a common/centralized View?
I know how I'd do this in CodeIgniter. But... what's the correct architecture for this, using Zend Framework? I'm convinced that it's very different than what CodeIgniter would do, and I want to start writing this application with the right architecture in mind.
One small naming thing: /:controller/:action/* => /do/this = this is an action (although also both a function and a method in the controller, action is the proper name)
Your blocks to me sound like "partial views". There are a few ways to approach this problem, and depending on how the views work, or what information they need to know, you adapt your strategy
Rendering Partials
You want to use this method when you have some view code you want to be used by multiple views. There are two different approaches using the view helpers Zend_View_Helper::render or Zend_View_Helper_Partial* The render($phtmlfile) view helper is more efficient, the partial($phtmlfile, $module, $params) view helper clones a new view, unseting all parameters, and setting the ones you pass in. An example of how to use them:
case/list.phtml:
<?php
$this->headTitle($this->title);
// works because our controller set our "cases" property in the view, render
// keeps our variables
echo $this->render("case/_caseListTable.phtml");
case/view.phtml
<?php
$this->headTitle($case->title);
?><!--- some view code showing the case -->
<?php if ($cases = $case->getChildren()): ?>
<h3>Children</h3>
<?php echo $this->partial("case/_caseListTable.phtml", "default", array(
"cases"=>$cases,
)); ?>
<?php endif; ?>
case/_caseListTable.phtml
// table header stuff
<?php foreach ($this->cases as $case): ?>
// table rows
<?php endforeach; ?>
// table footer stuff
Custom View Helpers
Sometimes the controller has no business knowing what information is being displayed in the block, and preparing it for your view would be silly, at this point you want to make your own view helpers. You can easily add them to the global view in application.ini:
resources.view.doctype = "XHTML1_STRICT"
resources.view.helperPath.My_View_Helper = APPLICATION_PATH "/../library/My/View/Helper"
I have a tendency to use this method for things that will require additional information from the model not provided by the controller, or blocks of reusable formatting code for the view. A quick example, from a project that I used: Olympic_View_Helper_Ontap grabs the draught beer list and renders it:
class Olympic_View_Helper_Ontap extends Zend_View_Helper_Abstract {
public function Ontap()
{
$view = $this->view;
$box = Olympic_Db::getInstance()->getTable('box')->getBoxFromName('Draught-Beer');
if ($box) $menu = $box->getMenu(); else $menu = null;
$content = "";
if ($menu)
{
$content = "<h1>".$view->escape($menu->title)."</h1>";
$content .= "<ul>";
foreach($menu->getItems() as $item) {
$content .= "<li>".$view->escape($item->name)."</li>";
}
$content .= "</ul>";
}
return $content;
}
}
Then in my layout:
<?php echo $this->ontap(); ?>
Your View Helpers can also accept arguments (of course), can call other view helpers (including partial). Consider them template functions. I like using them for short tasks that are required a lot, for instance $this->caseLink($case) generates a properly formatted <a href='/case/2' class='case project'>Project</a> tag.