in view I can fetch all categories:
<?php foreach ($this->parent_categories as $category): ?>
<?php endforeach ?>
Now controller:
$data['parent_categories'] = $this->category_model->get_parent_categories_tree($category);
and finally model category:
//get parent categories tree
public function get_parent_categories_tree($category, $only_visible = true)
{
if (empty($category)) {
return array();
}
//get cached data
$key = "parent_categories_tree_all";
if ($only_visible == true) {
$key = "parent_categories_tree";
}
$key = $key . "_lang_" . $this->selected_lang->id;
$result_cache = get_cached_data($this, $key, "st");
if (!empty($result_cache)) {
if (!empty($result_cache[$category->id])) {
return $result_cache[$category->id];
}
} else {
$result_cache = array();
}
$parent_tree = $category->parent_tree;
$ids = array();
$str_sort = "";
if (!empty($parent_tree)) {
$array = explode(',', $parent_tree);
if (!empty($array)) {
foreach ($array as $item) {
array_push($ids, intval($item));
if ($str_sort == "") {
$str_sort = intval($item);
} else {
$str_sort .= "," . intval($item);
}
}
}
}
if (!in_array($category->id, $ids)) {
array_push($ids, $category->id);
if ($str_sort == "") {
$str_sort = $category->id;
} else {
$str_sort .= "," . $category->id;
}
}
$this->build_query($this->selected_lang->id, true);
$this->db->where_in('categories.id', $ids, FALSE);
if ($only_visible == true) {
$this->db->where('categories.visibility', 1);
}
$this->db->order_by('FIELD(id, ' . $str_sort . ')');
$result = $this->db->get('categories')->result();
$result_cache[$category->id] = $result;
set_cache_data($this, $key, $result_cache, "st");
return $result;
}
but the problem is currently I fetch with this code all categories without limit. How to get first 10 records?
I try:
$result = $this->db->get('categories', 10)->result();
But this not work and still fetch all categories. Can anyone help me resolve this issue? Maybe I do something wrong. Thanks for check.
Related
In Controller, I want to save data but when I tried with foreach loop, it just saves last records
foreach (Form_821_item::group_items as $item) {
foreach ($item['code'] as $code) {
$items = IntersectingList::where('trial_id', $form_821->trial_balance_id)->where('account_code', 'like', $code . '%')->get();
if ($items) {
foreach ($items as $item) {
$account_name = $item->account_name;
$account_code = $item->account_code;
if ($item->total_balance_debit != 0) {
$financial_statements = $item->total_balance_debit;
$total_cash += $item->total_balance_debit;
} else {
$financial_statements = $item->total_balance_credit;
$total_cash += $item->total_balance_credit;
}
}
}
}
$data = new Form_821_item();
$data->form_821_id = $form_821->id;
$data->account_name = $account_name;
$data->account_code = $account_code;
$data->financial_statement = $financial_statements;
$data->save();
}
Any more advice?
You need to put your insert code inside the foreach
foreach (Form_821_item::group_items as $item) {
foreach ($item['code'] as $code) {
$items = IntersectingList::where('trial_id', $form_821->trial_balance_id)->where('account_code', 'like', $code . '%')->get();
if ($items) {
foreach ($items as $item) {
$account_name = $item->account_name;
$account_code = $item->account_code;
if ($item->total_balance_debit != 0) {
$financial_statements = $item->total_balance_debit;
$total_cash += $item->total_balance_debit;
} else {
$financial_statements = $item->total_balance_credit;
$total_cash += $item->total_balance_credit;
}
$data = new Form_821_item();
$data->form_821_id = $form_821->id;
$data->account_name = $account_name;
$data->account_code = $account_code;
$data->financial_statement = $financial_statements;
$data->save();
}
}
}
}
I have 600.000 data and I strain the values.
I strain some values in my database. I have 10 criterion about strain data.
So , When i strain the 600.000 Data from 10 criterion my mysql server is very slow.How can I make it fast ? This is my source code about strain.
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
ini_set('memory_limit', '256M');
function Check($Value,$DataBase)
{
$QueryString = "SELECT UserID FROM users where ".$Value;
$query = $DataBase->query($QueryString, PDO::FETCH_ASSOC);
//echo $Sorgu;
if ($query->rowCount()) {
foreach ($query as $row) {
return true;
}
}
return false;
}
$Array = $_POST["Data"];
$Array = json_decode($Array, true);
$Keys = ' ';
$Values = ' ';
$GSM1 = array();
$GSM2 = array();
$Phone1 = array();
$Phone2 = array();
$Fax1 = array();
$Fax2= array();
$UserCode = array();
$Identitiy = array();
$Empty = array();
$EmptyCheck = true;
foreach ($Array as $Row) {
$EmptyCheck = true;
$Keys = ' ';
$Values = ' ';
foreach ($Row as $key => $val) {
if ($key == 'UserGsm1') {
if ($val != null AND is_numeric($val) AND Check('UserGsm1=' . $val,$db)) {
$EmptyCheck = false;
$Gsm1[] = $Row;
break;
}
} else if ($key == 'UserGsm2') {
if ($val != null AND is_numeric($val) AND Check('UserGsm2=' . $val,$db)) {
$EmptyCheck = false;
$Gsm2[] = $Row;
break;
}
} else if ($key == 'UserPhone1') {
if ($val != null AND is_numeric($val) AND Check('UserPhone1=' . $val,$db)) {
$EmptyCheck = false;
$Phone1[] = $Row;
break;
}
} else if ($key == 'UserPhone2') {
if ($val != null AND is_numeric($val) AND Check('UserPhone2=' . $val,$db)) {
$EmptyCheck = false;
$Phone2[] = $Row;
break;
}
} else if ($key == 'UserFax1') {
if ($val != null AND is_numeric($val) AND Check('UserFax1=' . $val,$db)) {
$EmptyCheck = false;
$Fax1[] = $Row;
break;
}
} else if ($key == 'UserFax2') {
if ($val != null AND is_numeric($val) AND Check('UserFax2=' . $val,$db)) {
$EmptyCheck = false;
$Fax2[] = $Row;
break;
}
} else if ($key == 'UserCode') {
if ($val != null AND is_numeric($val) AND Check('UserCode=' . $val,$db)) {
$EmptyCheck = false;
$UserCode[] = $Row;
break;
}
} else if ($key == 'UserIdentitiy') {
if ($val != null AND is_numeric($val) AND Check('UserIdentitiy=' . $val,$db)) {
$EmptyCheck = false;
$Identitiy[] = $Row;
break;
}
}
$Keys = $Keys . ',' . $key;
$Values = $Values . ',' . $val;
//echo $key.' : '.$val;
//echo '<br>';
}
if ($EmptyCheck == true) {
$Empty[] = $Row;
}
}
I have a situation showed in the PHP code below, and I want to make a recursive function called check_recursive().
I have made the check_recursive() function below, but I want a recursive function, if it is possible.
Thank You!
$menu = '[{"id":"3|case_studies","children":[{"id":"2|case_studies","children":[{"id":"1|custom_links","children":[{"id":"2|faqe"}]}]}]},{"id":"11|klientet","children":[{"id":"8|klientet","children":[{"id":"7|klientet"}]}]},{"id":"9|klientet","children":[{"id":"10|klientet"}]},{"id":"4|klientet"}]';
$old_menu = json_decode($menu, true);
$new_menu = $this->check_recursive($old_menu);
function check_recursive($old_menu)
{
$i = 0;
$new_menu = [];
foreach ($old_menu as $menu_item)
{
if($name = $this->check_menu($menu_item['id']))
{
$new_menu[$i]['id'] = $menu_item['id'] . '|' . $name;
if(isset($menu_item['children']))
{
$e = 0;
foreach ($menu_item['children'] as $menu_item)
{
if($name = $this->check_menu($menu_item['id']))
{
$new_menu[$i]['children'][$e]['id'] = $menu_item['id'] . '|' . $name;
if(isset($menu_item['children']))
{
$y = 0;
foreach ($menu_item['children'] as $menu_item)
{
if($name = $this->check_menu($menu_item['id']))
{
$new_menu[$i]['children'][$e]['children'][$y]['id'] = $menu_item['id'] . '|' . $name;
if(isset($menu_item['children']))
{
$a = 0;
foreach ($menu_item['children'] as $menu_item)
{
if($name = $this->check_menu($menu_item['id']))
{
$new_menu[$i]['children'][$e]['children'][$y]['children'][$a]['id'] = $menu_item['id'] . '|' . $name;
}
$a++;
}
}
}
$y++;
}
}
}
$e++;
}
}
}
$i++;
}
return $new_menu;
}
function check_menu($string){
//Check if string exists in database
if($string){
return 'String exists';
}
return false;
}
I came up with this :
$menu = '[{"id":"3|case_studies","children":[{"id":"2|case_studies","children":[{"id":"1|custom_links","children":[{"id":"2|faqe"}]}]}]},{"id":"11|klientet","children":[{"id":"8|klientet","children":[{"id":"7|klientet"}]}]},{"id":"9|klientet","children":[{"id":"10|klientet"}]},{"id":"4|klientet"}]';
$old_menu = json_decode($menu, true);
$new_menu = check_recursive($old_menu);
function check_recursive($old_menu)
{
$new_menu = array();
foreach ($old_menu as $item) {
$name = check_menu($item['id']);
if($name){
$new_item = array(
'id' => $item['id'] . '|' . $name,
);
if(isset($item['children'])){
$new_item['children'] = check_recursive($item['children']);
}
$new_menu[] = $new_item;
}
}
return $new_menu;
}
function check_menu($string)
{
//Check if string exists in database
if ($string) {
return 'String exists';
}
return false;
}
Let me know if it suits your needs.
I have Already Created The Categories in Magento by using the custom script,
but issue is when I again run the same URL my script creates another set of same category. as i want to update the existing category and insert the new categories if exist in the csv.
My csv structure is:
Category 1,Description,Category 2,Description,Category 3,Description,Category 4,Description
1,COSTUMES,1,ADULTS,1,MENS
1,COSTUMES,1,ADULTS,1,MENS,2,ASIAN
1,COSTUMES,1,ADULTS,1,MENS,3,BIKER
1,COSTUMES,1,ADULTS,1,MENS,1,CAPES & ROBES
1,COSTUMES,1,ADULTS,1,MENS,5,CAVE PEOPLE
Thanking All of You in Advance For Your Answer
Below is my code to create multi level categories:
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$count = 0;
$file = fopen('export/web categories.csv', 'r');
function odd($var)
{
$odd = array();
foreach ($var as $k => $v) {
if ($k % 2 !== 0) {
$odd[$k] = $v;
}
}
return $odd;
}
function even($var)
{
$even = array();
foreach ($var as $k => $v) {
if ($k % 2 == 0) {
$even[$k] = $v;
}
}
return $even;
}
function strcount($str,$sy){
$ch= explode($sy, $str);
return count($ch);
}
$pos=0;
$row_config= array();
$test=array();
$catgoryID=array();
$parID = 1;
while (($line = fgetcsv($file)) !== FALSE) {
if(!in_array($valtest,$row_config)){
if($count == 0){
$count++;
continue;
}
$count++;
$filterd_data=array_filter($line);
$odd_cell_data=odd($filterd_data);
$even_cell_data=even($filterd_data);
$config=array();
$string='';
$intialID=$even_cell_data[0];
foreach($odd_cell_data as $key=>$val){
if(!in_array($val,$config)){
$config[] = $val;
$data['general']['name'] =$val;
$data['general']['meta_title'] = "";
$data['general']['meta_description'] = "";
$data['general']['is_active'] = "";
$data['general']['url_key'] = "";
$data['general']['is_anchor'] = 0;
$data['general']['position'] =1 ;
$storeId = 0;
$string .=$val.'~';
if(!array_key_exists($string, $row_config)){
$catID=createCategory($data, $storeId);
$catgoryID[$string]=$catID;
$row_config[$string]=$parID;
} else {
$parID =$row_config[$string] ;
$row_config[$string]=$row_config[$string];
}
if( strcount($string,'~')==2){
$parID=1;
}
$int[$string]=$parID;
assignCat($catgoryID[$string],$parID);
$parID = $catgoryID[$string];
sleep(0.5);
unset($data);
}
}
}
}
//This Function is used for create each category
function createCategory($data, $storeId) {
$category = Mage::getModel('catalog/category');
$category->setStoreId($storeId);
if (is_array($data)) {
$category->addData($data['general']);
if (!$category->getId()) {
$parentId = $data['category']['parent'];
if (!$parentId) {
if ($storeId) {
$parentId = Mage::app()->getStore($storeId)->getRootCategoryId();
} else {
$parentId = Mage_Catalog_Model_Category::TREE_ROOT_ID;
}
}
$parentCategory = Mage::getModel('catalog/category')->load($parentId);
$category->setPath($parentCategory->getPath());
} if ($useDefaults = $data['use_default']) {
foreach ($useDefaults as $attributeCode) {
$category->setData($attributeCode, null);
}
}
$category->setAttributeSetId($category->getDefaultAttributeSetId());
if (isset($data['category_products']) && !$category->getProductsReadonly()) {
$products = array();
parse_str($data['category_products'], $products);
$category->setPostedProducts($products);
} try {
$category->save();
$category = Mage::getModel('catalog/category')->load($category->getId());
$category->setPosition($data['general']['position']);
$category->addData($data['general']);
$category->save();
echo "Suceeded <br /> ";
} catch (Exception $e) {
echo "Failed <br />";
}
}
return $category->getId();
}
//This Function is used for moving category under respective parent
function assignCat($id, $parent){
$category = Mage::getModel( 'catalog/category' )->load($id);
Mage::unregister('category');
Mage::unregister('current_category');
Mage::register('category', $category);
Mage::register('current_category', $category);
$category->move($parent);
return;
}
Just got this code working for 2 levels of my menu. But I want it to work with unlimited levels.
Do any of you guys have an idea where to start?
Right now if I type in more levels in the database it says "Undefined index Linnk & Label Line 29", and the new parent that has been entered does not show up.
$sql = "SELECT id, label, link_url, parent_id FROM dyn_menu ORDER BY parent_id, id ASC";
$items = mysql_query($sql);
while ($obj = mysql_fetch_object($items)) {
if ($obj->parent_id == 0) {
$parent_menu[$obj->id]['label'] = $obj->label;
$parent_menu[$obj->id]['link'] = $obj->link_url;
} else {
$sub_menu[$obj->id]['parent'] = $obj->parent_id;
$sub_menu[$obj->id]['label'] = $obj->label;
$sub_menu[$obj->id]['link'] = $obj->link_url;
if (!isset($parent_menu[$obj->parent_id]['count'])) {
$parent_menu[$obj->parent_id]['count'] = 0;
}
$parent_menu[$obj->parent_id]['count']++;
}
}
mysql_free_result($items);
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");
In order to do this, you'll want to use a recursive function to handle your array -> list transformation.
Here's an example:
<?php
function arrToUl($arr) {
$out = '<ul>';
foreach($arr as $key=>$val) {
$out .= '<li>';
$out .= is_array($val) ? arrToUl($val) : $val;
$out .= '</li>';
}
return $out . '</ul>';
}
$arr = array('firstval','secondval',array('firstval2','secondval2',array('firstval3','secondval3',array('firstval4','secondval4',array('firstval5','secondval5')))));
echo arrToUl($arr);
Output in a jsfiddle.