codeigniter category -> subcategory -> subsubcategory system - codeigniter

ok, so i'm very new to codeigniter and from what i have learned by now i can't figure out how can i create a dynamic category -> subcategory -> subsubcategory system. Can you give me some guidelines please...some references, anything to guide me on what should i learn to accomplish that? thanks
i should get my URL like this www.site.com/category/subcategory/subsubcategory/etc... , you know what i mean.

I have done this for the page manager in PyroCMS but it is no easy task.
Each page has its own slug and parent_id, then to read the correct page it loops through each of the page slugs and joins the child. It knows how many children there are so if there are 5 children it selects the 5th self-joined table.
Here is an example of the code:
public function get_by_path($segments = array())
{
// If the URI has been passed as a string, explode to create an array of segments
if(is_string($segments))
{
$segments = explode('/', $segments);
}
// Work out how many segments there are
$total_segments = count($segments);
// Which is the target alias (the final page in the tree)
$target_alias = 'p'.$total_segments;
// Start Query, Select (*) from Target Alias, from Pages
$this->db->select($target_alias.'.*');
$this->db->from('pages p1');
// Loop thorugh each Slug
$level = 1;
foreach( $segments as $segment )
{
// Current is the current page, child is the next page to join on.
$current_alias = 'p'.$level;
$child_alias = 'p'.($level - 1);
// We dont want to join the first page again
if($level != 1)
{
$this->db->join('pages '.$current_alias, $current_alias.'.parent_id = '.$child_alias.'.id');
}
// Add slug to where clause to keep us on the right tree
$this->db->where($current_alias . '.slug', $segment);
// Increment
++$level;
}
// Can only be one result
$this->db->limit(1);
return $this->db->get()->row();
}
It's a bit nuts but it works perfectly. This can be really slow so PyroCMS also maintains a look-up table which has id and the page URI to match quickly.
You can see the whole model here:
http://github.com/philsturgeon/pyrocms/blob/master/application/modules/core/pages/models/pages_m.php

you could:
create controller category, reroute some URIs to it and use it's internal logic to parse it's arguments to pick whatever article client requested:
About URLs:
http://codeigniter.com/user_guide/general/urls.html
About URI routing:
http://codeigniter.com/user_guide/general/routing.html

I agree with Phil's idea and I was also envisioning that you can create a separate module (if you use modular extensions for example) to handle the categories in a generic way. Then you can reuse that module in any other projects. Basically the new module may be able to handle categories and sub-categories (the hierarchy).

Related

How to convert CodeIgniter urls into user friendly ones?

I've explored lot of questions and articles regarding this but I can't find how to get this done.
I'm doing a website which provides specifications of several products such as phones, tablets, tv etc. Here's what I've:
Controller - Specs (create and display specification of all products)
Method - Display (fetches detailed specs of selected model and shows)
Method - Index (lists names of all models stored in the table. this is where I build anchor links)
Display method takes three arguments (1, 2, 3).
1 - Type of product (Phones, Tablets, TV etc)
2 - Model Slug (iphone-6, galaxy-tab-s3, bravia-kdl-50w800d etc)
3 - Model ID (1, 4, 13 etc)
My URLs right now are like this:
localhost/sitename/specs/display/phones/iphone-6/1
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
localhost/sitename/specs/display/tv/bravia-kdl-50w800d/13
What I want to achieve is URLs which are like this:
localhost/sitename/iphone-6
localhost/sitename/galaxy-tab-s3
localhost/sitename/bravia-kdl-50w800d
I don't mind restructuring my tables/controllers/methods or anything else if this can be achieved using whatever.
Thanks for reading.
Edit:
Route.php
$route['default_controller'] = 'Specs/index';
$route['404_override'] = 'Errors/show_404';
$route['translate_uri_dashes'] = FALSE;
This is how I'm building the anchor links (view_file->index.php, called from Index method):
<?php
foreach model(in the table)
echo anchor(specs_controller.display_function.product_type.model_slug.model_id, model_name);
end foreach
?>
I can get the desired URLs with following code in route.php. Only problem is I'm not able to make the 'urlController/urlMethod' return a value in the function which can be assigned to $result variable.
$route['(:any)'] = function ($1)
{
$result = 'urlController/urlMethod/'.$1;
return $result;
};
I'm not sure how to do this. Can someone suggest how I should call 'urlController/urlMethod'?
You could achieve it with CodeIgniter URI Routing. Considering
localhost/sitename/galaxy-tab-s3
maps to
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
And, model id i.e 4, in this case, is static with respect to galaxy tab s3, as you have not mentioned any such Id in the simplified URL.
My understanding is with every URL localhost/sitename/iphone-6, you need three details about the string 'iphone-6'. i.e. type of product, model-slug, model id. One way could be write something like
$route['sitename/(:any)'] = 'routingController/commonRoutingMethod/$1';
Here, create a new routingController and write some logic into commonRoutingMethod() method, which takes the string like iphone-6 and fetches its all three details i.e. product type, model id etc. And then redirects by building the exact URL using
header('Location: http://localhost/sitename/specs/display/$productType/$modelSlug/$modelId/');
NOTE : There could be more forward ways just using regex match in routes.php, given that you create diffrentiated structure of the string, based on product type and model id e.g p_iphone-6_1 or t_galaxy-tab-s3_4.
Please use below routing code, to achieve it.
localhost/sitename/specs/display/phones/iphone-6/1
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
localhost/sitename/specs/display/tv/bravia-kdl-50w800d/13
localhost/sitename/iphone-6
localhost/sitename/galaxy-tab-s3
localhost/sitename/bravia-kdl-50w800d
$route['(:any)'] = 'specs/display/$1/$1/$1';
Let me know if you need any help.

Magento - Stop child products showing in anchor category

On an anchor category page I'm trying to only show the products directly contained within the category. I don't want to show the products of child categories, it's not appropriate in this case. I really do need to filter the products in my current category, so I need layered navigation, which necessitates an anchor category.
Initially I thought to filter the products in the view.phtml template, but that only filters the products in the view and is not a sensible answer. When I do this I end up with big gaps on my page where child products are present, but simply not displayed. And the product counts (eg. "1-12 of 117 products") are "incorrect".
From what I've read, this is going to require a core rewrite. Probably of an index process. I don't really know where to start with this rewrite, and I'm sure it's going to be rather involved.
Are any of my assumptions wrong? Have you already solved this problem?
Goto app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php
copy to app/code/local/Mage/Catalog/Model/Resource/Product/Collection.php
find function addCategoryFilter( and here you you find code
public function addCategoryFilter(Mage_Catalog_Model_Category $category)
{
$this->_productLimitationFilters['category_id'] = $category->getId();
/* start to comment here
if ($category->getIsAnchor()) {
unset($this->_productLimitationFilters['category_is_anchor']);
} else {
$this->_productLimitationFilters['category_is_anchor'] = 1;
}*/
/* new line */
$this->_productLimitationFilters['category_is_anchor'] = 1;
if ($this->getStoreId() == Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID) {
$this->_applyZeroStoreProductLimitations();
} else {
$this->_applyProductLimitations();
}
return $this;
}
You don't need a single line of code if I have understood your condition correct. Magento provides the freedom to enter products in any category regardless of their hierarchy.
So consider the following scenario:
Category A->Category a
Then a product can be assigned to only category a and not to category A. This will work in harmony with the entire magento system including layered navigation and no of prouducts shown on page.

Magento - get results view HTML for a collection of products

I get a list of magento ids from a web service. I load these into and array $product_ids, so I have something like this:
Array
(
[0] => 1965
[1] => 3371
[2] => 1052
)
I can then make this into a collection:
$collection = Mage::getModel('catalog/product')->getCollection()
->addIdFilter($product_ids);
Using my Magento inspector, I've seen that the category pages use the class Mage_Catalog_Block_Product_List to display lists of products. I'd like to do something similar in my class. I've tried loading:
$ProductList = new Mage_Catalog_Block_Product_List();
$ProductList->setCollection($collection);
And then I've tried to load the HTML of the results as follows:
$CollectionHTML = $ProductList->_toHtml();
But $CollectionHTML is empty.
How would I get the HTML of what you see in the list view (i.e. the generated output of frontend/base/default/template/catalog/product/list.phtml, but given my collection)?
Making the code work the right way is much more easier in Magento than trying to work with ugly legacy code. I would gladly help you make the code the proper way when you have specific questions. Also, in the longterm, technical debt is gonna cost alot more.
Anyway, back to your issue.
In Magento block are not instantiated like in any app $myvar = new className ... almost never. This tutorial can help you understand better Magento's layout and blocks.
But if you want to create a block a way to do it is:
$block = Mage::getSingleton('core/layout')->createBlock('catalog/product_list')
Now related to your product collection you should check how Mage_Catalog_Block_Product_List::_getProductCollection actually works, because it uses the layered navigation, not a simple product collection.
Further, assuming that at least you are using a Magento controller and you are within a function, the following code will display the first page of products for a specified category:
//$category_id needs to be set
$layout = Mage::getSingleton('core/layout');
$toolbar = $layout->createBlock('catalog/product_list_toolbar');
$block = $layout->createBlock('catalog/product_list');
$block->setChild('toolbar', $toolbar);
$block->setCategoryId($category_id);
$block->setTemplate('catalog/product/list.phtml');
$collection = $block->getLoadedProductCollection();
$toolbar->setCollection($collection);
//render block object
echo $block->renderView();
Displaying specific ids:
you use root category id for $category_id variable (also make sure that display root category is set (or another category id that contains your product ids)
you can hook into catalog_block_product_list_collection event to add your ID Filter to the collection (this is called in _beforeToHtml function)
But, all this construction is not solid and there are still some points that require attention (other child blocks, filters and so on)

Removing id variables in joomla v1.5 router

I have custom Joomla(v1.5) component and currently working with component's router. The problem is I can't remove id numbers from SEF url. I get:
http://myaddress.com/componentalias/17-city-alias/130-item-alias
What I want to get:
http://myaddress.com/componentalias/city-alias/item-alias
Take a look at router.php methods below:
function ComponentnameBuildRoute(&$query) {
$segments = array();
if(isset($query['city_id'])){
$segments[] = $query['city_id'];
unset($query['city_id']);
}
if(isset($query['item_id'])){
$segments[] = $query['item_id'];
unset($query['item_id']);
}
if(isset($query['task'])){
switch($query['task']){
case 'pay':
$segments[] = JText::_('payment');
unset($query['task']);
break;
}
}
unset($query['view']);
return $segments;
}
/*
* Function to convert a SEF URL back to a system URL
*/
function ComponentnameParseRoute($segments) {
$var = array();
if(isset($segments[0])){
$cityData = explode(':',$segments[0]);
if(isset($cityData[0])){
$vars['city_id'] = $cityData[0];
}
}
if(isset($segments[1])){
$itemData = explode(':',$segments[1]);
if(isset($itemData[0])){
$vars['item_id'] = $itemData[0];
}
}
if(isset($segments[2])){
switch($segments[2]){
case JText::_('payment'):
$vars['task'] = 'pay';
break;
}
}
return $vars;
}
Any ideas? Your help would be appreciated.
The best place to start with your router.php file is reading this article (it's a bit dated but still good) and then reviewing the com_content's router.php file (components/com_content/router.php). You will notice that articles do achieve what you want so best to look at working code and got from there.
Longer answer:
You can only get rid of the item ID variables in a path to a content element if a menu item exists that points directly to the item, otherwise there is no way to find the item.
SEF URLs in Joomla! 1.5 etc are made from the alias' of the individual elements
eg. If I have this menu structure:
Recipes (The menu)
-- Seafood (<-- Category blog where category alias is `seafood` )
-- Grilled Snapper (<-- Recipe Item where item alias is `grilled-snapper` )
-- 'Other category' (<-- Another Category blog )
Full ID Removal
In the case where you're building the SEF URL for a recipe you can build the route by looking for the menu item it might appear in by getting the site menu $menu = &JSite::getMenu(); and comparing the query id in the current menu item against id value in the $query array passed in.
If you have a match you can build the segments up using the alias from the menu path and the recipe alias. (And reverse the process in your ParseRoute($segments) method).
So, from this example above you could build a SEF URL to the Grilled Snapper recipe that looks something like: recipes/seafood/grilled-snapper.
Partial ID Removal
Now say you also have another recipe (e.g. 'Garlic Prawns' alias garlic-prawns) that isn't directly linked to a menu but will appear in the 'Seafood' category blog page. In this situation you would end up with recipes/seafood/2:garlic-prawns
If you don't have a match (like the Garlic Prawns), you can build up partial match if your component has list views like category blogs or in our example Recipe category pages... Essentially in this case you look at the current menu item and determine if it's a list/category view that would contain the content item.
If it is then the path to the category/list view form you initial segments, but as there is no menu item for the article you will still have to use the ID of the item in the last element of the URL.
No Menu Item for content item or a list/category view
When the content item is being linked to directly (e.g. from an article, module or search result) and there are no menu items that point to it or could contain it then you can still create a URL without id's in it but you will be providing the path in the form of direct component access URL.
eg. /component/recipes/recipe/ice-cream-sundae where recipes is the name of the component, recipe is the view and ice-cream-sundae is the alias of the article.

CakePHP Pagination sort() on Related Models

I have two models: Plans and PlanDetails.
Relationship is: PlanDetails hasMany Plans. Plans belongTo PlanDetails.
In the PlanDetails view.ctp, I am pulling in related Plans.
I am trying to sort the Plans by ANY field (I've tried them all), and I cannot get it working. I assume I am overlooking something very simple.
Here is my base code:
PlanDetail >> view.ctp:
...foreach ($planDetail['Plan'] as $plan_edit) :
$class = null;
if ($i++ % 2 == 0) {
$class = ' class="altrow"';
}...
<?php echo $this->Paginator->sort('Plan ID', 'Plan.id'); ?>...
...<?php echo $plan_edit['id']; ?>
plan_details_controller.php:
...function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid plan detail', true));
$this->redirect(array('action' => 'index'));
}
$this->PlanDetail->recursive = 2; // To run the editable form deeper.
$this->set('planDetail', $this->PlanDetail->read(null, $id));
$this->set('plan', $this->paginate('Plan'));
}...
I should add, no errors are being thrown and the sort() arrows on the ID field are showing as expected, but the sort order DOES not change when clicked either way.
Sorry, I'm not able to comment on the question itself, but I've noticed that in your action, you set planDetail to be the PlanDetail record you read (with recursive set to 2), and then you set plan to be the result of the paginate call.
Then, in your view template, you're iterating over $planDetail's contained Plan association, like this:
foreach ($planDetail['Plan'] as $plan_edit):
But in order to get the sorting and pagination done, you need to be displaying the results of the paginate call i.e. iterate over the records contained in $plan.
Do a debug($plan) in your view template to see what results you get there and to see if the records' ordering changes when you sort by different fields.
Also, perhaps you're using syntax I'm not aware of, but if you simply call $this->paginate('Plan') in your controller, I don't know that you're going to get only the related Plan records for your particular PlanDetail record. (There's nothing tying the $id passed into your view action with the Plan records.) You might need to add some conditions to the paginate call, like so:
$this->paginate['Plan'] = array('conditions' => array('Plan.plan_detail_id' => $id));
$this->set('plans', $this->paginate('Plan'));
Here is what I did to solve this. Based on some helpful direction from johnp & tokes.
plan_details/view.ctp:
...$i = 0;
foreach ($plan as $plan_edit) : // note $plan here.
}...
In my plan_details_controller.php view action:
$conditions = array("Plan.plan_detail_id" => "$id");
$this->set('plan', $this->paginate('Plan', $conditions)); // note "plan" in the first argument of set (this is required to pass the results back to the view). Also. The $condition is set to return ONLY plans that match the plan_detail_id of id (on the plan_details table).
And in my view, in order to get my results (because I changed the array name), I had to change the way I was getting the values to:
$plan_edit['Plan']['modified'] // note I placed ['Plan'] in front of these calls as the array called for this to get the data...
Well until the next problem! SOLVED.

Resources