Skip navigation

Category Archives: Programming

So the other day, I was bored outta  my mind. I was playing sudoku on my ipod. I said to myself, like I usually do, there has got to be a better way to solve a sudoku puzzle. Then an LED light bulb(to conserve energy) lit up in my head, I will write a program to do it. Here we are. The repo is up on github.

It solves by:

  1. going through the puzzle.
  2. if it is an empty cell,  it checks corresponding row, column and block while eliminating possibilities.
  3. if it is an occupied cell, it checks neighboring rows and columns. it looks for cells with same value and see if there is an empty cell in neighboring blocks that can hold that value.
  4. it goes through these until the puzzle is solved or if we don’t solve a single cell in 5 consecutive run.
  5. if it’s not solved at this point, we use backtracking(highly inefficient but works to some degree).
  • it works by bascailly going through all the possible values.
  • guessing one cell and base the guess of the next cell on the previous one.
  • if we have no possible guesses, we go back one cell and guess again.
  • repeat this until we have successfully guess all the remaining cells.

    To-do:

    • find a better way to solve it

    opencart has built in seo friendly url but that’s for products only as far as i can tell. and you have to actually type out the terms. so i took it upon myself to make it friendlier and more automagic.

    this is for opencart v1.4.8b

    the format of url we are trying to achieve is:

    /product/:product_id/:product_title

    so it would look something like /product/1/girls-gone-wild

    change catalog/model/tool/seo_url.php

    UPDATED: includes all urls now and added file for download (Catalog.rar (673))

    1. public function rewrite($link)
    2. {
    3. if ($this->config->get(‘config_seo_url’))
    4. {
    5. $url_data = parse_url(str_replace(‘&’, ‘&’, $link));
    6.  
    7. $url = ;
    8.  
    9. $data = array();
    10.  
    11. parse_str($url_data[‘query’], $data);
    12.  
    13. switch($data[‘route’])
    14. {
    15. case ‘product/product’:
    16. $this->load->model(‘catalog/product’);
    17. $product = $this->model_catalog_product->getProduct($data[‘product_id’]);
    18. //get product categories
    19. $path = ;
    20. if(!isset($this->request->get[‘path’]) || empty($this->request->get[‘path’]))
    21. {
    22. $categories = $this->model_catalog_product->getCategories($product[‘product_id’]);
    23. if(!empty($categories))
    24. {
    25. $path = $this->model_catalog_product->getPath($categories[’0′][‘category_id’]);
    26. $path = substr($path,-1,1)==‘_’?substr($path,0,strlen($path)-1):$path;
    27. }
    28. }
    29. else
    30. {
    31. $path = $this->request->get[‘path’];
    32. }
    33. $url = $this->config->get(‘config_url’).‘product/’.$data[‘product_id’].‘/’.(!empty($path)?$path.‘/’:).$this->toSlug($product[‘name’]);
    34. break;
    35. case ‘product/category’:
    36. $this->load->model(‘catalog/category’);
    37. $this->load->model(‘catalog/product’);
    38. $categoryId = explode(‘_’,$data[‘path’]);
    39. $path = ;
    40. if(count($categoryId) == 1)
    41. {
    42. $path = $this->model_catalog_product->getPathUp($categoryId[’0′]);
    43. $path = substr($path,-1,1)==‘_’?substr($path,0,strlen($path)-1):$path;
    44. }
    45.  
    46. $categoryId = $categoryId[(count($categoryId)-1)];
    47. $category = $this->model_catalog_category->getCategory($categoryId);
    48. $url = $this->config->get(‘config_url’).‘category/’.($path?$path:$data[‘path’]).‘/’.$this->toSlug($category[‘name’]);
    49. break;
    50. case ‘information/information’:
    51. $this->load->model(‘catalog/information’);
    52. $info = $this->model_catalog_information->getInformation($data[‘information_id’]);
    53. $url = $this->config->get(‘config_url’).‘information/’.$data[‘information_id’].‘/’.$this->toSlug($info[‘title’]);
    54. break;
    55. default:
    56. $route = explode(‘/’,$data[‘route’]);
    57. $url = $this->config->get(‘config_url’).$data[‘route’];
    58.  
    59. break;
    60. }
    61. if(!empty($url))
    62. {
    63.  
    64. unset($data[‘route’],$data[‘path’],$data[‘product_id’],$data[‘information_id’]);
    65. $query = ;
    66.  
    67. if ($data)
    68. {
    69. foreach ($data as $key => $value)
    70. {
    71. $query .= ‘&’ . $key . ‘=’ . $value;
    72. }
    73.  
    74. if ($query)
    75. {
    76. $query = ‘?’ . trim($query, ‘&’);
    77. }
    78. }
    79. return $url.$query;
    80. }
    81. return $link;
    82.  
    83. } else
    84. {
    85. return $link;
    86. }
    87. }
    88.  
    89. public function toSlug($name)
    90. {
    91. $name = str_replace("’", "", $name);
    92. $name = str_replace(‘"’, "", $name);
    93. $name = strtolower($name);
    94. $name = preg_replace("/&#?[a-z0-9]+;/i","",$name);
    95. $name = preg_replace(‘/[^a-zA-Z0-9-]/’, ‘-’, $name);
    96. $name = preg_replace(‘/-+/’, "-", $name);
    97. return $name;
    98. }

    then change catalog/controller/common/seo_url.php

    1. public function index()
    2. {
    3. if (isset($this->request->get[‘_route_’]))
    4. {
    5. $parts = explode(‘/’, $this->request->get[‘_route_’]);
    6. $routes = array(‘category’,‘product’,‘information’,‘account’,‘common’,‘checkout’);
    7.  
    8. if(in_array($parts[0], $routes))
    9. {
    10. switch($parts[0])
    11. {
    12. case ‘category’:
    13. $this->request->get[‘path’] = $parts[1];
    14. break;
    15. case ‘product’:
    16. $this->request->get[‘product_id’] = $parts[1];
    17. $this->request->get[‘path’] = $parts[2];
    18. break;
    19. case ‘information’:
    20. $this->request->get[‘information_id’] = $parts[1];
    21. break;
    22. default:
    23. $this->request->get[‘route’] = $this->request->get[‘_route_’];
    24. break;
    25. }
    26. }
    27. else
    28. {
    29. foreach ($parts as $part)
    30. {
    31. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = ‘" . $this->db->escape($part) . "’");
    32.  
    33. if ($query->num_rows)
    34. {
    35. $url = explode(‘=’, $query->row[‘query’]);
    36.  
    37. if ($url[0] == ‘product_id’)
    38. {
    39. $this->request->get[‘product_id’] = $url[1];
    40. }
    41.  
    42. if ($url[0] == ‘category_id’)
    43. {
    44. if (!isset($this->request->get[‘path’]))
    45. {
    46. $this->request->get[‘path’] = $url[1];
    47. } else
    48. {
    49. $this->request->get[‘path’] .= ‘_’ . $url[1];
    50. }
    51. }
    52.  
    53. if ($url[0] == ‘manufacturer_id’)
    54. {
    55. $this->request->get[‘manufacturer_id’] = $url[1];
    56. }
    57.  
    58. if ($url[0] == ‘information_id’)
    59. {
    60. $this->request->get[‘information_id’] = $url[1];
    61. }
    62. } else
    63. {
    64. $this->request->get[‘route’] = ‘error/not_found’;
    65. }
    66. }
    67. }
    68.  
    69. if (isset($this->request->get[‘product_id’]))
    70. {
    71. $this->request->get[‘route’] = ‘product/product’;
    72. } elseif (isset($this->request->get[‘path’]))
    73. {
    74. $this->request->get[‘route’] = ‘product/category’;
    75. } elseif (isset($this->request->get[‘manufacturer_id’]))
    76. {
    77. $this->request->get[‘route’] = ‘product/manufacturer’;
    78. } elseif (isset($this->request->get[‘information_id’]))
    79. {
    80. $this->request->get[‘route’] = ‘information/information’;
    81. }
    82.  
    83. if (isset($this->request->get[‘route’]))
    84. {
    85. return $this->forward($this->request->get[‘route’]);
    86. }
    87. }
    88. }

    Since cakephp 1.3, build-in css compression has stopped working. at least for me. however, it’s very easy to get it working again.

    Step 1:

    grab csspp from here and put it in your vendor directory so it looks something like this (app/vendors/csspp/csspp.php)

    Step 2:

    in your core.php file in app/config, uncomment Configure::write(‘Asset.filter.css’, ‘css.php’);

    Step 3:

    in your css.php under app/webroot,  modify make_clean_css function so that it looks something like this

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    function make_clean_css($path, $name)
    {
    App::import('Vendor', 'csspp' . DS . 'csspp');
    $data = file_get_contents($path);
    $csspp = new csspp($name,'');
    $output = $csspp->process();
    $ratio = 100 - (round(strlen($output) / strlen($data), 3) * 100);
    $output = " /* file: $name, ratio: $ratio% */ " . $output;
    return $output;
    }

    and that should be it. if it doesnt work for some reason,

    change  line 82 in your app/webroot/css.php to

    if (file_exists($cachepath) && 0)

    so you won’t be looking at cached file when u debug.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
    $('#form').validate({
    
    errorLabelContainer:'#errorContainer',
    showErrors: function(errorMap, errorList) {
    if(errorList.length)
    {
    $('#errorContainer').html(errorList[0]['message']);
    }
    },
    highlight:function(element,errorClass){
    $(element).parent('td').addClass('error');
    },
    unhighlight:function(element,errorClass){
    $(element).parent('td').removeClass('error');
    }
    })

    jQuery validate plugin is pretty awsome. However it doesn’t do everything. That’s why the plugin have the addMethod function. And I need to add an ajax duplicate check for an email input. Here’s the method i wrote. There might be a better way. I would love to know.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
    jQuery.validator.addMethod("checkForDupeEmail", function(value, element, param){
    
    var ajaxFunc = $.ajax({
    async:false,   //we have to set it to false, it does not return a value before we even complete the request.
    
    data:'email='+($.trim(value)),
    type:'POST',
    url:'/employees/ajax_checkDupe',
    dataType:'text',
    });
    
    //here we check the response
    
    
    if(ajaxFunc.responseText == 1)
    return true;
    else
    return false;
    });