Like Query Is Not Working properly - codeigniter

In Code igniter Model I am using like query to fetch all the products having first name rice which is not working controller while using get_where('name') it works fine.
public function fetchdeal_products($id) {
$this->db->select('*');
$this->db->from('products');
$q = $this->db->like("name", $id);
if ($q->num_rows() > 0) {
foreach (($q->result()) as $row) {
$data[] = $row;
}
return $data;
}
}
I am using like query to fetch all the products having first name rice which is not working controller while using get_where('name') it works fine.
//Controller
function ajaxdealcategorydata($id = NULL) {
$this->sma->checkPermissions('index');
$id = $this->input->get('id');
$subcategories = $this->pos_model->getdealcategory($id);
$scats = '';
foreach ($subcategories as $category) {
$scats .= "<button id=\"subcategory-" . $category->id . "\" type=\"button\" value='" . $category->id . "' class=\"btn-prni subcategory\" ><img src=\"assets/uploads/thumbs/" . ($category->image ? $category->image : 'no_image.png') . "\" style='width:" . $this->Settings->twidth . "px;height:" . $this->Settings->theight . "px;' class='img-rounded img-thumbnail' /><span>" . $category->name . "</span></button>";
}
$products = $this->ajaxdealproducts($id);
if (!($tcp = $this->pos_model->products_count($id))) {
$tcp = 0;
}
echo json_encode(array('products' => $products, 'subcategories' => $scats, 'tcp' => $tcp));
}
I am using like query to fetch all the products having first name rice which is not working controller while using get_where('name') it works fine.
function ajaxdealproducts() {
$this->sma->checkPermissions('index');
if ($this->input->get('id') ) {
$id = $this->input->get('id');
} else {
$category_id = $this->pos_settings->default_category;
}
if ($this->input->get('subcategory_id')) {
$subcategory_id = $this->input->get('subcategory_id');
} else {
$subcategory_id = NULL;
}
if ($this->input->get('per_page') == 'n') {
$page = 0;
} else {
$page = $this->input->get('per_page');
}
$this->load->library("pagination");
$config = array();
$config["base_url"] = base_url() . "pos/ajaxdealproducts";
$config["total_rows"] = $subcategory_id ? $this->pos_model- >products_count($id, $subcategory_id) : $this->pos_model->products_count($id);
$config["per_page"] = $this->pos_settings->pro_limit;
$config['prev_link'] = FALSE;
$config['next_link'] = FALSE;
$config['display_pages'] = FALSE;
$config['first_link'] = FALSE;
$config['last_link'] = FALSE;
$this->pagination->initialize($config);
$products = $this->pos_model->fetchdeal_products($id, $config["per_page"], $page);
$pro = 1;
$prods = '<div>';
foreach ($products as $product) {
$count = $product->id;
if ($count < 10) {
$count = "0" . ($count / 100) * 100;
}
if ($category_id < 10) {
$category_id = "0" . ($category_id / 100) * 100;
}
$prods .= "<button id=\"product-" . $category_id . $count . "\" type=\"button\" value='" . $product->code . "' title=\"" . $product->name . "\" class=\"btn-prni btn-" . $this->pos_settings->product_button_color . " product pos-tip\" data-container=\"body\"><img src=\"" . base_url() . "assets/uploads/thumbs/" . $product->image . "\" alt=\"" . $product->name . "\" style='width:" . $this->Settings->twidth . "px;height:" . $this->Settings->theight . "px;' class='img-rounded' /><span>" . character_limiter($product->name, 15) . "</span></button>";
$pro++;
}
$prods .= "</div>";
if ($this->input->get('per_page')) {
echo $prods;
} else {
return $prods;
}
}

The database LIKE function only creates a LIKE in the SQL statement. You'll still need to call the GET mothod afterwards; Like this;
$this->db->like('name', $id);
$q = $this->db->get();

You are using $this->db_like() incorrectly.
$this->db->like('title', 'match');
// Produces: WHERE title LIKE '%match%'
(from documentation)
In your case the usage would be
$this->db->like('name', 'rice')
More info

public function fetchdeal_products($id)
{
$query = $this->db->query("Select products.* From products Where (products.name Like '%$id%')");
$result = $query->result_array();
return $result;
}

you have to write it as bellow.
public function fetchdeal_products($id) {
$this->db->like("name", $id);
$q = $this->db->get('products');
if ($q->num_rows() > 0) {
foreach (($q->result()) as $row) {
$data[] = $row;
}
return $data;
}
}

Related

N-tier navigation in codeigniter

How to create top navigation in code-igniter?
In controller I have declare a function and call model
private function getNavigation(){
$this->load->model('xx');
$data['nav'] = $this->xx->prepareTree();
$this->load->view('index',$data);
}
And In model I have declare three functions
public function prepareTree(){
$this->db->select("`catalog_parent` as parent_id, `catalog_name` as menu_item, `catalog_id` as id, `catalog_template` as pager_id");
$this->db->from("catalog_category");
$this->db->where("`catalog_navigation` = '1'");
$this->q = $this->db->get();
$create = '';
if ($this->q->num_rows() > 0) {
$create = $this->prepareList($this->q->result_array());
}
if(!empty($create)){
return $this->category_tree($create);
} else {
return '';
}
}
private function prepareList(array $items, $pid = 0) {
$output = array();
foreach ($items as $item) {
if ((int) $item['parent_id'] == $pid) {
if ($children = $this->prepareList($items, $item['id'])) {
$item['children'] = $children;
}
$output[] = $item;
}
}
return $output;
}
private function category_tree($menu_items, $child = false){
$output = '';
if (count($menu_items)>0) {
$output .= ($child === false) ? '<ul id="main-menu" class="sm sm-blue">' : '<ul>' ;
foreach ($menu_items as $item) {
$output .= '<li>';
$output .= ''.$item['menu_item'].'';
if (isset($item['children']) && count($item['children'])) {
$output .= $this->category_tree($item['children'], true);
}
$output .= '</li>';
}
$output .= '</ul>';
}
return $output;
}
If some suggest an more easy way please suggest us.
Thanks

magento configrable product collection 1500 with pagination but showing time out not loading

please visite my url and seee
https://staging.raptorsupplies.com/catalogsearch/result/?q=baldwin
you click on first product also you can see this product showing
enter image description here
if click on then it will be showing error like
and if click on other product then it open and working, because variation/collection list have less but it is in 1500 variation
i have create pagination for this request but i am still facing error
you can see my code where is error i think error in pagination but i am get it show please any one can help me
[enter image description here][2]
public function getConfigAttribOption($childProductList, $attribbid = NULL, $AllchildProductList = null) {
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$storeId = Mage::app()->getStore()->getStoreId(); //die;
if ($attribbid > 0) {
$SqlAttributeID = "select distinct attribute_id from eav_attribute_option where option_id in($attribbid)";
$RowAttributeID = $this->executeQuery($SqlAttributeID);
foreach ($RowAttributeID as $arrKey => $Arrval) {
$arrAttributeId[] = $Arrval['attribute_id'];
}
$attributeids = implode(',', $arrAttributeId);
$sqlConfigurableAttribute = "select a.attribute_id as id,a.frontend_label as label,a.attribute_code as attribute_code,group_concat(distinct i.value) as option_id,GROUP_CONCAT(DISTINCT o.value SEPARATOR '&|&') as option_value from catalog_product_entity_int as i join eav_attribute as a on i.attribute_id=a.attribute_id JOIN eav_attribute_option_value AS o ON i.value=o.option_id where o.store_id=".$storeId." and (a.attribute_id in(81,92) OR a.attribute_id>20000) and entity_id in($childProductList) AND a.attribute_id NOT IN($attributeids) group by i.attribute_id having count(distinct i.value)>1";
$arrCount = 0;
mysql_query('SET SESSION group_concat_max_len=10000');
foreach ($arrAttributeId as $key => $val) {
if ($arrCount == count($attribbid) - 1) {
$sqlCheckedAttribute.=" union all SELECT a.attribute_id AS id,a.frontend_label AS label,a.attribute_code AS attribute_code,IF(COUNT(DISTINCT i.value)>1,GROUP_CONCAT(DISTINCT i.value),'') AS option_id,GROUP_CONCAT(DISTINCT o.value SEPARATOR '&|&') AS option_value FROM catalog_product_entity_int AS i JOIN eav_attribute AS a ON i.attribute_id=a.attribute_id JOIN eav_attribute_option_value AS o ON i.value=o.option_id WHERE o.store_id=".$storeId." and (a.attribute_id in(81,92) OR a.attribute_id>20000) AND entity_id in(" . $AllchildProductList . ") AND a.attribute_id=" . $val . " GROUP BY i.attribute_id ";
} else {
$sqlCheckedAttribute.=" union all SELECT a.attribute_id AS id,a.frontend_label AS label,a.attribute_code AS attribute_code,IF(COUNT(DISTINCT i.value)>1,GROUP_CONCAT(DISTINCT i.value),'') AS option_id,GROUP_CONCAT(DISTINCT o.value SEPARATOR '&|&') AS option_value FROM catalog_product_entity_int AS i JOIN eav_attribute AS a ON i.attribute_id=a.attribute_id JOIN eav_attribute_option_value AS o ON i.value=o.option_id WHERE o.store_id=".$storeId." and (a.attribute_id in(81,92) OR a.attribute_id>20000) AND entity_id in(" . $AllchildProductList . ") AND a.attribute_id=" . $val . " GROUP BY i.attribute_id ";
}
$arrCount = $arrCount + 1;
}
$sqlConfigurableAttribute = $sqlConfigurableAttribute . $sqlCheckedAttribute;
} else {
$sqlConfigurableAttribute = "select a.attribute_id as id,a.frontend_label as label,a.attribute_code as attribute_code,group_concat(distinct i.value) as option_id,GROUP_CONCAT(DISTINCT o.value SEPARATOR '&|&') as option_value from catalog_product_entity_int as i join eav_attribute as a on i.attribute_id=a.attribute_id JOIN eav_attribute_option_value AS o ON i.value=o.option_id where o.store_id=$storeId and (a.attribute_id in(81,92) OR a.attribute_id>20000) and entity_id in($childProductList) group by i.attribute_id having count(distinct i.value)>1";
}
$sqlConfigurableAttribute = $sqlConfigurableAttribute . " order by id"; //die;
$productAttributeOptions = $readConnection->fetchAll($sqlConfigurableAttribute);
return $productAttributeOptions;
}
public function executeQuery($query) {
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
return $readConnection->fetchAll($query);
}
function getRelativeOptionBlock($productId = null, $attribbid = null, $pageno = null) {
$encrypted_string=$this->getRequest()->getParam('active_filters');
$decrypted_string=urldecode(base64_decode($encrypted_string));
if($decrypted_string){
$attribbid=$decrypted_string;
$productId= $this->getProduct()->getId();
}
if ($attribbid && $productId) {
//$childProducts = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($productId);
//$childProductList = implode(',', $childProducts[0]);
$AllchildProducts = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($productId);
$AllchildProductList = implode(',', $AllchildProducts[0]);
$attribb = explode(',', $attribbid);
$query = 'SELECT eav_attribute.attribute_code FROM eav_attribute JOIN eav_attribute_option ON eav_attribute.attribute_id=eav_attribute_option.attribute_id WHERE option_id IN(' . $attribbid . ')';
$results = $this->executeQuery($query);
$attribvalue = $results['0']['attribute_code'];
$product = Mage::getModel('catalog/product')->load($productId);
$childProductsColl = Mage::getModel('catalog/product_type_configurable')
->getUsedProductCollection($product)
->addAttributeToSelect('*');
foreach ($results as $res) {
$childProducts = $childProductsColl->addFieldToFilter($res['attribute_code'], $attribb);
}
$childProductList = array();
foreach ($childProducts as $cp2) {
$childProductList[] = $cp2->getId();
}
$childProductList = implode(',', $childProductList);
} elseif ($productId) {
$childProducts = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($productId);
$childProductList = implode(',', $childProducts[0]);
} else {
$childProducts = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($this->getProduct()->getId());
$childProductList = implode(',', $childProducts[0]);
}
$attributeOptions = array();
$productAttributeTh = array();
$productAttributeId = array();
$resource = Mage::getSingleton('core/resource');
$writeConnection = $resource->getConnection('core_write');
$querytemp="SET group_concat_max_len=100000";
$writeConnection->query($querytemp);
$productAttributeOptions = $this->getConfigAttribOption($childProductList, $attribbid, $AllchildProductList);
Mage::getSingleton('core/session')->setOptionlength(count($productAttributeOptions));
$html = "";
$html .= "<div class='box-container list' style='margin-left: 40px;'>";
foreach ($productAttributeOptions as $productAttribute) {
$productAttributeTh[] = $productAttribute['label'];
$productAttributeThId[] = $productAttribute['id'];
$productAttributeThCode[] = $productAttribute['attribute_code'];
$productAttributeId = array();
$productAttributeVal = array();
$productAttributeId = explode(',', $productAttribute['option_id']);
$productAttributeVal = explode('&|&', $productAttribute['option_value']);
if((int)$productAttribute['id'] == 81)
{
$this->brandcount = count($productAttributeVal);
}
if((int)$productAttribute['id'] == 81 && count($productAttributeVal) ==1)
continue;
$attribute_to= count($productAttributeId);
$attribute_option=array();
for($i=0;$i<$attribute_to;$i++){
$attribute_option[$productAttributeId[$i]]=$productAttributeVal[$i];
}
asort($attribute_option);
$label =$productAttribute['label'];
$html .= "<div class='inner-box-container list-content'>";
$html .= "<p class='new_align' title='".ucwords($productAttribute['label'])."'>" .ucwords($label) . "</p>";
///$productAttributeLbel = $productAttribute['label'];
$productAttributeLbel = preg_replace('/[^A-Za-z0-9\-]/', '', $productAttribute['label']);
$html .= "<script> multiselected('" . str_replace(' ', '', $productAttributeLbel) . "')</script>";
$html .= "<select class='" . str_replace(' ', '', $productAttributeLbel) . "'>";
//echo"<select >";
foreach ($attribute_option as $OptionKey => $OptionVal) {
if((int)$productAttribute['id'] == 81 && count($attribute_option) ==1)
continue;
$attridarray = explode(',', $attribbid);
if (in_array($OptionKey, $attridarray)) {
//echo $attribbid.$OptionVal; die;
$html .= "<option value='" . $OptionKey . "' title='title'>". $OptionVal . "</option>";
} else {
$html .= "<option value='" . $OptionKey ."' >" . $OptionVal . "</option>";
}
}
$html .= "</select>";
$html .= "</div>";
}
Mage::getSingleton('core/session')->setAttributeIds($productAttributeThId);
$html .= "</div><br class='clearBoth' />";
return $html;
}
function getRelativeHeaderBlock($productId = null, $attribbid = null) {
$encrypted_string=$this->getRequest()->getParam('active_filters');
$decrypted_string=urldecode(base64_decode($encrypted_string));
if($decrypted_string){
$attribbid=$decrypted_string;
$productId= $this->getProduct()->getId();
}
if ($productId) {
$childProducts = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($productId);
} else {
$childProducts = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($this->getProduct()->getId());
}
$childProductList = implode(',', $childProducts[0]);
$productAttributeTh = array();
$attribidvalue= array();
if ($attribbid) {
$attribb = explode(',', $attribbid);
$query = 'SELECT * FROM eav_attribute_option where option_id in(' . $attribbid . ')';
$results = $this->executeQuery($query);
foreach($results as $key=>$val){
$attribidvalue[]=$val['attribute_id'];
}
//$attribidvalue = $results['0']['attribute_id'];
}
$productAttributeOptions = $this->getConfigAttribOption($childProductList);
foreach ($productAttributeOptions as $productAttribute) {
$productAttributeTh[$productAttribute['id']] = $productAttribute['label'];
}
$Productdetail = Mage::getModel('catalog/product')->load($productId);
?>

Error Message show encountered Severity: Notice Message:

On my controller for development I have added error_reporting(0); just below the opening php tag on my controller. So will not show some errors.
When go to live mode and remove or comment out the added error_reporting(0); I get a couple of errors
A PHP Error was encountered Severity: Notice Message: Undefined
variable: other_sub Filename: common/Filemanager.php
A PHP Error was encountered Severity: Notice Message: Undefined
variable: histSub Filename: common/Filemanager.php
Question: I have those variables defined not sure why errors showing?
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//error_reporting(0);
class Filemanager extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->library('pagination');
$this->lang->load('admin/common/filemanager', 'english');
$this->load->model('admin/tool/model_tool_image');
}
public function index() {
$directory = FCPATH . 'image/catalog/';
$element = $this->input->get('element'); // Element
$input = $this->input->get('input'); // Target
$sub_get = $this->input->get("sub"); // Sub Folders
$end_url = '?';
$end_url .= 'input='.$input;
$end_url .= '&element='.$element;
$endHist = $end_url;
$uris = $this->uri->segment_array();
$sub_folder = "";
if ($sub_get) {
$sub_folder = $sub_get;
}
if ($sub_folder) {
$end_url .= '&sub=' .$sub_folder;
if (sizeof($uris) >3 ) {
for ($i=3; $i < sizeof($uris); $i++) {
$directory .= $uris[$i].'/';
$other_sub .= $uris[$i].'/';
}
$directory .= $sub_folder.'/';
$other_sub .= $sub_folder.'/';
} else {
$directory .= $sub_folder.'/';
$other_sub = $sub_folder.'/';
}
}
$data['images'] = array();
// Get directories
$directories = glob($directory . '*', GLOB_ONLYDIR);
if (!$directories) {
$directories = array();
}
// Get files
$files = glob($directory.'*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}', GLOB_BRACE);
if (!$files) {
$files = array();
}
// Merge directories and files
$images = array_merge($directories, $files);
// Get total number of files and directories
$image_total = count($images);
$per_page = 8;
$segment = $this->input->get('per_page');
$segment += $per_page;
foreach ($images as $key => $image) {
if ($key < $segment && $key >= $segment-$per_page) {
$name = str_split(basename($image), 18);
if (is_dir($image)) {
$data['images'][] = array(
'thumb' => '',
'name' => implode(' ', $name),
'type' => 'directory',
'path' => utf8_substr($image, utf8_strlen(FCPATH .'image/')),
'href' => site_url('admin/filemanager') .'/'. utf8_substr($image, utf8_strlen(FCPATH . 'image/catalog/')),
);
} elseif (is_file($image)) {
$subDirect = 'catalog/'.$other_sub;
$data['images'][] = array(
'thumb' => $this->model_tool_image->resize(utf8_substr($image, utf8_strlen(DIR_IMAGE)), 100, 100),
'name' => implode(' ', $name),
'type' => 'image',
'path' => $subDirect.utf8_substr($image, utf8_strlen($directory)),
'href' => base_url() . 'image/' . utf8_substr($image, utf8_strlen(FCPATH . 'image/catalog/'))
);
}
}
}
$data['title'] = "Image Manager";
$data['heading_title'] = "Image Manager";
if (isset($sub_folder)) {
$data['directory'] = $this->uri->segment(3) .'/'. $this->uri->segment(4);
} else {
$data['directory'] = '';
}
if ($element) {
$data['element'] = $element;
} else {
$data['element'] = "";
}
if ($input) {
$data['target'] = $input;
} else {
$data['target'] = "";
}
$endRep = $end_url == '?' ? '' : rtrim($end_url,'&');
// Sets go to previous parent folder.
if ($other_sub) {
$arraySub = explode('/', rtrim($other_sub,'/'));
unset($arraySub[sizeof($arraySub) -1]);
$histSub = "";
foreach ($arraySub as $one) {
$histSub .= $one.'/';
}
$endHist .= "&sub=".$arraySub[sizeof($arraySub) - 1];
}
// Parent folder link
$data['parent'] = site_url('admin/filemanager') .'/'. $histSub . $endHist;
// Refesh current Page
$data['refresh'] = current_url().$endRep;
$config['base_url'] = base_url('admin/filemanager/') .'/'. $other_sub.$endRep;
$config['end_url'] = $end_url == '?' ? '' : rtrim($end_url,'&');
$config['total_rows'] = $image_total;
$config['per_page'] = $per_page;
$config['page_query_string'] = TRUE;
$config['num_links'] = "16";
$config['full_tag_open'] = "<ul class='pagination'>";
$config['full_tag_close'] ="</ul>";
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
$config['next_tag_open'] = "<li>";
$config['next_tagl_close'] = "</li>";
$config['prev_tag_open'] = "<li>";
$config['prev_tagl_close'] = "</li>";
$config['first_tag_open'] = "<li>";
$config['first_tagl_close'] = "</li>";
$config['last_tag_open'] = "<li>";
$config['last_tagl_close'] = "</li>";
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
return $this->load->view('template/common/filemanager_view', $data);
}
}
Problem now solved I had to change a few things and make the input->get setup a lot more simple now works fine.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Filemanager extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->library('pagination');
$this->lang->load('admin/common/filemanager', 'english');
$this->load->model('admin/tool/model_tool_image');
}
public function index() {
$input_get_directory = $this->input->get('directory');
$input_get_page = $this->input->get('page');
$input_get_filter = $this->input->get('filter_name');
$input_get_target = $this->input->get('target');
$input_get_thumb = $this->input->get('thumb');
if (isset($input_get_filter)) {
$filter_name = $input_get_filter .'/';
} else {
$filter_name = null;
}
// Make sure we have the correct directory
if (isset($input_get_directory)) {
$directory = FCPATH . 'image/catalog/' . $input_get_directory;
} else {
// Do not add extra tralier slash at end /
$directory = FCPATH . 'image/catalog';
}
if (isset($input_get_page)) {
$page = $input_get_page;
} else {
$page = 1;
}
$data['images'] = array();
// Get directories
$directories = glob($directory . '/' . $filter_name . '*', GLOB_ONLYDIR);
if (!$directories) {
$directories = array();
}
// Get files
$files = glob($directory . '/' . $filter_name . '*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}', GLOB_BRACE);
if (!$files) {
$files = array();
}
// Merge directories and files
$images = array_merge($directories, $files);
// Get total number of files and directories
$image_total = count($images);
// Split the array based on current page number and max number of items per page of 10
$images = array_splice($images, ($page - 1) * 16, 16);
foreach ($images as $image) {
$name = str_split(basename($image), 14);
if (is_dir($image)) {
$url = '';
if (isset($input_get_target)) {
$url .= '&target=' . $input_get_target;
}
if (isset($input_get_thumb)) {
$url .= '&thumb=' . $input_get_thumb;
}
$data['images'][] = array(
'thumb' => '',
'name' => implode(' ', $name),
'type' => 'directory',
'path' => utf8_substr($image, utf8_strlen(FCPATH . 'image/')),
'href' => site_url('admin/common/filemanager' . '?&token=' . $this->session->userdata('token') . '?&directory=' . utf8_substr($image, utf8_strlen(FCPATH . 'image/' . 'catalog/')) . $url)
);
} elseif (is_file($image)) {
$data['images'][] = array(
'thumb' => $this->model_tool_image->resize(utf8_substr($image, utf8_strlen(FCPATH . 'image/')), 100, 100),
'name' => implode(' ', $name),
'type' => 'image',
'path' => utf8_substr($image, utf8_strlen(FCPATH . 'image/')),
'href' => base_url() . 'image/' . utf8_substr($image, utf8_strlen(FCPATH . 'image/'))
);
}
}
$data['heading_title'] = "Image Manager";
$data['text_no_results'] = "No Results";
$data['text_confirm'] = "Are You Sure";
$data['entry_search'] = "Search..";
$data['entry_folder'] = "New Folder";
$data['button_parent'] = "Parent";
$data['button_refresh'] = "Refresh";
$data['button_upload'] = "Upload";
$data['button_folder'] = "New Folder";
$data['button_delete'] = "Delete";
$data['button_search'] = "Search";
// Session token for ajax
$data['token'] = $this->session->userdata('token');
if (isset($input_get_directory)) {
$data['directory'] = $input_get_directory;
} else {
$data['directory'] = '';
}
// Return the filter name
if (isset($input_get_filter)) {
$data['filter_name'] = $input_get_filter;
} else {
$data['filter_name'] = '';
}
// Return the target ID for the file manager to set the value
if (isset($input_get_target)) {
$data['target'] = $input_get_target;
} else {
$data['target'] = '';
}
// Return the thumbnail for the file manager to show a thumbnail
if (isset($input_get_thumb)) {
$data['thumb'] = $input_get_thumb;
} else {
$data['thumb'] = '';
}
// Parent
$url = '';
if (isset($input_get_directory)) {
$pos = strrpos($input_get_directory, '/');
if ($pos) {
$url .= '?&directory=' . substr($input_get_directory, 0, $pos);
}
}
$data['parent'] = site_url('admin/common/filemanager' .'?&token='. $this->session->userdata('token') . $url);
// Refresh
$url = '';
if (isset($input_get_directory)) {
$url .= '?&directory=' . $input_get_directory;
}
$data['refresh'] = site_url('admin/common/filemanager' .'?&token='. $this->session->userdata('token') . $url);
$this->load->view('template/common/filemanager_view', $data);
}
}

Magento Issues with category dropdown

I need to display all the subcategories under root category name using . I am unable to find the root category details. Root category name is displaying as '/' How to overcome this issue?
Currently i am getting all the categories including root categories (/).
My code is as in below:
public function toOptionArray()
{
$categories = array();
$categoryCollection = Mage::getResourceModel('catalog/category_collection')->addFieldToFilter(
'path',
array('neq' => '1')
);
foreach ($categoryCollection as $category) {
var_dump($category->getData());
echo '<br />';
$category = Mage::getModel('catalog/category')->load($category->getId());
$categories[$category->getId()] = $category->getUrlPath();
}
return $categories;
}
protected function _renderOptions(Varien_Object $row)
{
$categories = $this->getColumn()->getOptions();
$html = sprintf('<select class="category_select" name="mapping[%s]">', $row->getData('reference_id'));
$html .= '<option value=""></option>';
foreach ($categories as $id => $name) {
$html .= sprintf(
'<option value="%s"%s>%s</option>',
$this->escapeHtml($id),
$id == $row->getData('category_id') ? ' selected="selected"' : '',
$this->escapeHtml($name)
);
}
$html .= '</select>';
return $html;
}
I need to add into the category dropdown, which is subcategories displaying under root category name.
Can anyone help me please.
Thank You.
I fixed the issue using following:
protected function _renderOptions(Varien_Object $row)
{
$categories = $this->getColumn()->getOptions();
$parentIdArray = array();
foreach ($categories as $id => $name) {
$category = Mage::getModel('catalog/category')->load($id);
$parentId = $category->getParentId();
if($parentId == 1){
$parentIdArray[] = $id;
}
}
$html = sprintf('<select class="category_select" name="mapping[%s]">', $row->getData('reference_id'));
$html .= '<option value=""></option>';
foreach ($parentIdArray as $parentId) {
$parentCategory = Mage::getModel('catalog/category')->load($parentId);
$name = $parentCategory->getName();
$html .= sprintf(
'<optgroup label="'.$name.'">
<option value="%s"%s>%s</option>
</optgroup>',
$this->escapeHtml($parentId),
$parentId == $row->getData('category_id') ? ' selected="selected"' : '',
$this->escapeHtml($name)
);
$categories = Mage::getModel('catalog/category')->getCategories($parentId);
$subCategories = $this->get_categories($categories);
foreach ($subCategories as $id => $name) {
$html .= sprintf(
'<option style="padding-left: 10px;" value="%s"%s>%s</option>',
$this->escapeHtml($id),
$id == $row->getData('category_id') ? ' selected="selected"' : '',
$this->escapeHtml($name)
);
}
}
$html .= '</select>';
return $html;
}
public function get_categories($categories) {
$subCategories = array();
foreach($categories as $category) {
$subCategory = Mage::getModel('catalog/category')->load($category->getId());
$subCategories[$subCategory->getId()] = $subCategory->getUrlPath();
if($category->hasChildren()) {
$children = Mage::getModel('catalog/category')->getCategories($category->getId());
$childrenCategories = $this->get_categories($children);
foreach ($childrenCategories as $id => $name) {
$subCategories[$id] = $name;
}
}
}
return $subCategories;
}

shop by brand name in magento

This is brand.php file having directory local/Vilpjsc/brand/Block.I want the brand name with logo(image) sholud display on home page .But I am getting the error i.e Fatal error: Call to a member function getCollection() on a non-object in /var/www/magentodemo/app/code/local/Vilpjsc/Brand/Block/Brand.php on line 25
<?php
class Vilpjsc_Brand_Block_Brand extends Mage_Core_Block_Template {
public function _prepareLayout() {
return parent::_prepareLayout();
}
//Get Collection from Brand Controller
public function getBrand() {
if (!$this->hasData('brand')) {
$this->setData('brand', Mage::registry('brand'));
}
return $this->getData('brand');
}
//Get Brand Featured Collection
public function getFeaturedBrand() {
return $this->getBrand()->getCollection()->setPageSize(12)->addFilter('status', '1');
}
public function resizeImage($img,$width=100, $height=60) {
if (!file_exists("./media/brand/resized"))
mkdir("./media/brand/resized", 0777);
$imageResized = Mage::getBaseDir('media') . DS . "brand" . DS . "resized" . DS . $width."x".$height.$img;
if (!file_exists($imageResized) && file_exists("./media/brand/" . $img)) {
$imageObj = new Varien_Image("./media/brand/" . $img);
$imageObj->constrainOnly(TRUE);
$imageObj->keepAspectRatio(TRUE);
$imageObj->keepFrame(FALSE);
$imageObj->resize($width, $height);
$imageObj->save($imageResized);
}
$newImageUrl = Mage::getBaseUrl('media')."brand/resized/".$width."x".$height.$img;
return $newImageUrl;
}
public function getNormalBrand() {
return $this->getBrand()->getCollection()->addFilter('status', '0');
}
public function getBrandChar() {
extract($this->getBrandKey());
return $this->getBrand()->getCollection()->saveBrandcollection($char);
}
//Create Alphabet navigation
public function navigation() {
extract($this->getBrandKey());
$alphas = range('A', 'Z');
$navigation = "";
$digitClass = "";
foreach ($alphas as $key) {
if ($char == $key)
$class_key = "current_char";
else
$class_key="";
$navigation .= "<li class='key_item " . $class_key . "'><a href='" . Mage::getBaseUrl() . "brand/index/index/brand_key/" . $key . "'>" . $key . "</a></li>";
}
if ($char == "digit") {
$digitClass = "current_char";
} elseif ($char == "all")
$allClass = "current_char";
$navigation = "<li class='key_item " . $digitClass . "'><a href='" . Mage::getBaseUrl() . "brand/index/index/brand_key/digit'>#</a></li>" .
$navigation;
return $navigation;
}
//Get request for brand block
public function getBrandKey() {
$brand_key = Mage::registry('brand_key');
if ($brand_key) {
$char = $brand_key->getParam('brand_key') ? $brand_key->getParam('brand_key') : 'A';
$option_id = $brand_key->getParam('option_id') ? $brand_key->getParam('option_id') : '1';
$brand = $brand_key->getParam('brand') ? $brand_key->getParam('brand') : 'Nike';
} else {
$char = 'digit';
$option_id = '544';
}
return compact('char', 'option_id', 'brand');
}
}

Resources