Welcome Guest, Not a member yet? Register   Sign In
Match features with products
#1

[eluser]Lykos22[/eluser]
I'd like some help please. I 'd like to apply some features to products. Here's my database structure so far:
Code:
table: Products
product_id (pk)
product_name
category_id
price
description
etc etc
-----------------------------------------
table: Categories
category_id(pk)
category_name
-----------------------------------------
table: Features
feature_id(pk)
feature_name // e.g. RAM, Proccessor, Optical-Zoom, Memory
------------------------------------------
table: Features_to_Products
id(pk)
product_id  // e.g. 1, 1, 2, 2
feature_id // e.g. 1, 2, 3, 4
value // e.g. 4gb, Intel I5 3ghz, 12mpx, 512mb

So for example an Apple iMac (id 1), in laptops category, should have features RAM, Proccessor with values 4gb, Intel I5 3ghz
and product Nikon Digital Camera (id 2), in digital cameras category, should have features Optical-Zoom, Memory with values 12mpx, 512mb.

The admin menu I've created looks like this
menu:
- Categories (create new, editing, deleting)
- Products (create new, editing, deleting product and apply feature-values)
- Features (create new, editing, deleting features only, values applied through products)

So in my products view I have created two forms. In the first one can edit product fields (name, price, description etc etc) and in the second one to edit feature-values of specific product.

So far, I 've tried to fetch all my features and loop through them in the 2nd form. So next to each feature name I have an input field that will have get the feature value. Something like this:
Code:
<tbody>
&lt;?php foreach ($features as $feature): ?&gt;
<tr>
<td>
&lt;?php echo form_checkbox('feature_id[]', $feature->feature_id); ?&gt;
&lt;?php echo $feature->feature_name; ?&gt;
</td>
<td>&lt;input type="text" name="feature_value[]" value="&lt;?php echo set_value('feature_value[]', //$feature-&gt;feature_value); ?&gt;" /&gt;&lt;/td>
</tr>
&lt;?php endforeach; ?&gt;
</tbody>

But I think the problem with this approach is that it tends to show to many (and unnecessary) features and maybe a little bit mess, instead what else I tried to do is to display the features according to the type of product. So in order to create a new or edit an existing laptop or desktop like imac, I should only display features like ram, proccessor, hard-disk etc etc (zoom and photo-memory not needed), but i'm not quite sure how I can do this.

Any help would be appreciated and also if there are any other better suggetions please let me know.
#2

[eluser]Stefan Hueg[/eluser]
You can create a Category_Features table with the following fields:
Code:
table: Category_Features
category_id
feature_id

...create a unique index for category_id and feature_id and when you search for the features of a category, just select the corresponding rows from Category_Features and join your Features-table.

As a sidenote: You dont need an ID for your Features_to_Products as it is just an extended pivot-table Smile
#3

[eluser]Lykos22[/eluser]
Thanks for the reply, in fact I was already started doing that! Smile
Two question though:

1. [quote author="Stefan Hueg" date="1375148284"]As a sidenote: You dont need an ID for your Features_to_Products as it is just an extended pivot-table Smile[/quote]
The id to Features_to_Products table was just to have something in primary key, not really necessary in my code. Isn't a table required to have a pk??

2. I tried to study the database model of some cms shops like open-cart, prestashop etc but their model seems too complicated and although very useful tends to be slow. This solution seemed to me more correct instead of having all features together, as i mentioned before, or something else, but is it the best? I mean how should a more experienced developer would handle that matching features with products??
#4

[eluser]Stefan Hueg[/eluser]
[quote author="Lykos22" date="1375168164"]
The id to Features_to_Products table was just to have something in primary key, not really necessary in my code. Isn't a table required to have a pk?? [/quote]

A primary key is not always necessary, instead you should at least have a unique key.

[quote author="Lykos22" date="1375168164"]
2. I tried to study the database model of some cms shops like open-cart, prestashop etc but their model seems too complicated and although very useful tends to be slow. This solution seemed to me more correct instead of having all features together, as i mentioned before, or something else, but is it the best? I mean how should a more experienced developer would handle that matching features with products??
[/quote]

Your database structure is good for what you want to achieve. I would go in this direction Smile
#5

[eluser]Lykos22[/eluser]
Ok, thanks for the reply! I'd like some more feedback for the code structure if its possible please.

I have a Product controller for the admin panel (application/controllers/admin/product) like this:
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product extends Admin_Controller
{

function __construct(){
  parent::__construct();
  $this->load->model(array('product_model','product_version_model','category_model'));
}

public function index(){
  // fetch all products
  $data['products'] = $this->product_model->get_all();
  // load the view
  $this->load->view('admin/products/index', $data);
}

public function manage($id = NULL){
  $this->load->model(array('brand_model','feature_model'));

  $data['product'] = (isset($id)) ? $this->product_model->get($id) : $this->product_model->make_new() ;
  $data['version'] = (isset($id)) ? $this->product_version_model->get_by('product_id', $id) : $this->product_version_model->make_new() ;
   $data['categ'] = $this->category_model->with_parents();
  $data['brand'] = $this->brand_model->get_key_value('brand_id','brand_name');
  $data['features'] = $this->feature_model->get_by('category_id',???); // pass the category of the product

   if ( isset($_POST['save_product']) ) { // submit btn from first form
   $this->form_validation->set_rules($this->product_model->rules);
   if ($this->form_validation->run() === true) {
   // get post inputs and store them in database
   $data = $this->product_model->input_posts(array('product_name', 'brand_id', 'category_id', 'general_description','visible'));
   $this->product_model->save($data, $id);

   $result = array('status' => 200, 'message' => 'Data stored in database successfully');
  } else {
   // validation failed
    $result = array('status' => 400, 'reason' => validation_errors());
  }
   if ( $this->input->is_ajax_request() ) {
    echo json_encode($result);
         exit;
   }
   redirect('admin/product');
   }

   if ( isset($_POST['save_version']) ) { // submit btn from 2nd form
   $this->form_validation->set_rules($this->product_version_model->rules);
   if ($this->form_validation->run() === true) {
   // get post inputs and store them in database
   $data = $this->product_version_model->input_posts(array('barcode', 'price', 'stock', 'version_description'));
   $this->product_version_model->save($data, $id);

   $result = array('status' => 200, 'message' => 'Data stored in database successfully');
  } else {
   // validation failed
    $result = array('status' => 400, 'reason' => validation_errors());
  }
   if ( $this->input->is_ajax_request() ) {
    echo json_encode($result);
         exit;
   }
   redirect('admin/product');
   }

  $this->load->view('admin/products/manage', $data);
}
}
?&gt;

1. Is it a good practice to load many models in a single controller, so I can edit the data of a speciffic product, or creating a new one (manage() function)?

2. As you can see in my manage() in case there's an id(product_id) I'm fetching the data of a specific product.
Code:
$data['product'] = (isset($id)) ? $this->product_model->get($id) :
How can I pass the category_id of the product in
Code:
$data['features'] = $this->feature_model->get_by('category_id',???); // pass the category of the product
In order to show in my view all features of the specific category?

3. Refering toFat models - skinny controllers, can my controller be more thin by pushing some of my code in the model(s) (like the section of form validations and storing the data in the database)?




Theme © iAndrew 2016 - Forum software by © MyBB