Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Question about controller "category" and id in URL
  • Hello! Help! :)
    For example: I have in shop many many categories. I do not understand how to make right code in controller "category"
    http://www.myshop.com/index.php/category/laptops/
    http://www.myshop.com/index.php/category/moble-phones/ How work in that case controller? Like public function laptop and public function mobile-phone and etc? If I will have more than 100 category and 100 subcategory how in that case?
    What I should write? Use "switch case" function? :)
    And other question,
    http://www.myshop.com/index.php/category/laptops/dell/DELL-INSPIRON-1545-54535223
    DELL-INSPIRON-1545 - title in db
    54535223- this is Id in database.
    If I delete title - controller should understand URL http://www.myshop.com/index.php/category/laptops/dell/54535223 to found that pc. How it make? With route? (:any)?
    Examples:
    http://moto.gratka.pl/tresc/volkswagen-polo-6n-1-4-benzyna-1998r-12557819.html
    http://moto.gratka.pl/tresc/12557819.html
  • I solved a problem with ORM. It was a mini mistakes in Model and in action_index
    Little bit later I'll add my code here for everyone.
  • Well,
    you can go with a sollution of router () - it'c catchall for contorller... ( http://fuelphp.com/dev-docs/general/controllers/base.html ) or have a play with routes :)
    http://fuelphp.com/dev-docs/general/routing.html Good luck!
  • Something like this
    Class Controller_Category extends \Controller{
    
    public function action_index($category) {
    
    echo $category;
    
    }
    

    http://domain.com/category/computer will echo computer
  • Thank you! :)
  • A Huzz wrote on Saturday 20th of August 2011:
    Something like this
    Class Controller_Category extends \Controller{
    
    public function action_index($category) {
    
    echo $category;
    
    }
    

    http://domain.com/category/computer will echo computer

    I read docs again, I understood how to make http://domain.com/category/computer url, but do not understood how extract data
    http://domain.com/category/computer/laptop What I should write in code If I want to extract some rows from table?
    I do not understand how it will work.
    for example:
    $data = Model_laptop::find('all'); But if I will write other query like http://domain.com/category/computer/netbooks
    or http://domain.com/category/computer/laptop/dell
    how to extract from url "laptop" or "netbooks"? $data = Model_laptop::find('all', array('where'=>array('brand_name'=>'dell')));? But If I will have more than 200 category / subcategry- I should every category write in code or I can do make "smart" code which can automatically extract from url and write $data = Model_NAME-OF-CATEGORY::find('all'); I do not found any examples.
    'poor and non universal' solution for one variable:
      public function action_index($name = 'index'){
              switch ($name){
                  case 'index':
                      $data = Model_Prod::find('all');
                      break;
                  case 'laptop':
                      $data = Model_Laptop::find('all');
                      break;
                  default:
                      echo "something wrong url Adress";
                      $data = Model_Prod::find('all');
                      break;
              }
    
              $this->response->body = View::factory('welcome/index')->set('data',$data);
        }
    
  • You can do this with one method not hundred...Just pass second or third parameter like this:
    Public function action_category($category,$subcategory,$product){
    Echo $category;
    Echo $subcategory;
    Echo $product;
    }
    
    If you're still not sure then let me know I'll write some proper code for you.. am using my phone right now. Also, read the manual controller section http://www.fuelphp.com/docs/general/controllers/base.html Edited typo / code format
  • Dear Huzzi! Thank you for the answer.
    I understood how to send some variables like $category, $subcategory, $product-name-ID
    But I do not understand how write in code in "Model_QUERY::" these variables.
    Look at this:
    //example
    $data = Model_$CATEGORY::find('all'); // sure that example is not work. query not be Model_$category - but how I should write normal code?
    (Very bad, and not working code)
    public function action_index($category = 'category', $subcategory = 'null', $product-name-ID = 'null')
    {
    
        if(isset($category))
        {
            if(isset($subcategory))
            {
                if(isset($product-name-ID))
                {
                    $data = Model_$product-name-ID::find('all');
                }else{
                $data = Model_$subcategory::find('all');
            }
            }else{
                $data = Model_$category::find('all');
            }
    
        }else{
            //redirect to home page
        }
    }
    
  • Tell me how you have desgined your database, I assumed you had a single table that holds all your categories / sub-categories and another table for the products.
  • I just beginner in Mysql. But I want to make some simple tables:
    Laptops,
    Mobile phone,
    PC components,
    Household appliances and etc. With data.
    Therefore I asked how to extract from URL variables, and insert them in Model_$category and extract data from table.
    I want to make my version of that web site http://goo.gl/PP6a6
  • CREATE TABLE `laptops` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `subject` varchar(45) CHARACTER SET latin1 NOT NULL,
      `price` varchar(45) CHARACTER SET latin1 NOT NULL,
      `photo` varchar(45) CHARACTER SET latin1 NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
    
  • Dmitry Barko wrote on Tuesday 23rd of August 2011:
    CREATE TABLE `laptops` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `subject` varchar(45) CHARACTER SET latin1 NOT NULL,
      `price` varchar(45) CHARACTER SET latin1 NOT NULL,
      `photo` varchar(45) CHARACTER SET latin1 NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
    

    your database design is completely wrong.. you don't need mulitple table for for categories. you need the database structure something like this:
    --
    -- Table structure for table `categories`
    --
    
    CREATE TABLE IF NOT EXISTS `categories` (
      `id` int(4) NOT NULL AUTO_INCREMENT,
      `parent_id` int(4) NOT NULL,
      `name` varchar(100) NOT NULL,
      `seo_name` varchar(100) NOT NULL,
      `description` varchar(250) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
    
    --
    -- Dumping data for table `categories`
    --
    
    INSERT INTO `categories` (`id`, `parent_id`, `name`, `seo_name`, `description`) VALUES
    (1, 0, 'Computer', 'Computer', 'Computers'),
    (2, 1, 'Laptop', 'Laptop', 'Laptop computers'),
    (3, 1, 'Desktop', 'Desktop', 'Desktop computers'),
    (4, 0, 'Software', 'Software', 'Softwares');
    
    -- --------------------------------------------------------
    
    --
    -- Table structure for table `products`
    --
    
    CREATE TABLE IF NOT EXISTS `products` (
      `id` int(4) NOT NULL AUTO_INCREMENT,
      `cat_id` int(4) NOT NULL,
      `name` varchar(150) NOT NULL,
      `seo_name` varchar(150) NOT NULL,
      `description` text NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
    
    --
    -- Dumping data for table `products`
    --
    
    INSERT INTO `products` (`id`, `cat_id`, `name`, `seo_name`, `description`) VALUES
    (1, 2, 'HP Laptop', 'HP-Laptop', 'Dual Core processor\r\n2GB RAM\r\n2TB Hard Disk\r\nFloppy Disk\r\nUSB Drive'),
    (2, 2, 'Dell Laptop', 'Dell-Laptop', 'Dual Core processor\r\n2GB RAM\r\n2TB Hard Disk\r\nFloppy Disk\r\nUSB Drive');
    
    

  • Huzzi! Thank you for your help!
    And now I understand how it will work.
    But I have one little problem with : UnexpectedValueException [ Error ]: Relation "cat_id" was not found in the model. I want to show products from category 'Software'.
    Code:
    http://scrp.at/agW
  • sorry, i use db class not orm.. hope someone else can help you. good luck.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion