0

Im writing web application based on database with many tables with many relations.

For example i have table with [Items], which has many relations.

Each [Item] has one [Group]

Each [Item] has multiple [Ingredients]

Each [Ingredient] has one [Type]

My code is going out of control. I wrote "edit_item_record" form that is filled with fields like "name" or "id", but i have problems with dropdowns and sub-items.

I dont know how to create form that has everything related with [Item] on one page (Name, Group, Ingredients). Submitting form like this may need multiple inserts or updates on many tables. How to handle this?

Does anyone saw any tutorial for CodeIgniter that describes how to design application bigger than blog with 1 table and 2 fields?

tereško
  • 58,060
  • 25
  • 98
  • 150
Kamil
  • 13,363
  • 24
  • 88
  • 183
  • The requirement is not clear here. Please provide clear table structure and relationship and the action that you want to perform. – Nish Oct 30 '12 at 10:23
  • My question was in general. My table structure is too complicated and i dont want to waste your time. Just need some example... – Kamil Oct 30 '12 at 10:49

1 Answers1

1

You're not going to get this kind of thing through a tutorial, you need to take what you learned in the blog type tutorials and apply it to your situation. In my application simply adding a product can affect as many as 7 tables.

This is more a question about workflow than anything. You need to sit down with a pen and a pad and map out how things work, how your tables relate and the flow to add the data. Before you start coding you need to understand how to add data, which tables require information from others before you can insert etc. Take for example a simple product, you have basic data in the product table and another table for product images. Obviously in this case you'd need to insert the product table first to get an id to add to the images table. Yes you are going to end up with multiple inserts, in fact when adding a new member my routine inserts to one table twice due to the way the data works.

Take your time and map it out. This is a situation where no one tutorial is going to help, it's about you looking at your tables and data flow logically and putting it together.

As to putting all the data on one form that is the easy part. Simply pull the data in your controller using a query with joins. Something along these lines.

Controller:

$data['products'] = $this->product->getAllProductDetails($productId);

Model:

$this->db->where('productId',$productId);
$this->db->join('product_images','product_images.productId=products.productId');
$this->db->join('manufacturers','manufacturers.manufacturerId=products.productId');
$this->db->get('products');

The above would pull all the product, product images and manufacturer information into the variable $products for you to use in your view. Then on your save button you would have another controller function that would individually call saves (insert or update, personally I have a save function that decides which is required) for each of those tables

Rick Calder
  • 18,310
  • 3
  • 24
  • 41
  • I wrote get_all_data function in my model, that returns data in another format. It returns object with tree structure, not simple array... I did this to make view code simple (foreach [Item] with foreach [subitem] inside). – Kamil Oct 30 '12 at 10:51
  • Im curious how you display data with relations in view. Can you put some example? – Kamil Oct 30 '12 at 11:06
  • Not sure what you mean? The get all details query would put all the relevant data into a single array (or object). Displaying it is just a matter of picking the right data from the array. $products['productName'] or whatever it's called in your tables. – Rick Calder Oct 30 '12 at 11:09
  • I mean how you organized loops, are you skipping duplicate values in first colum when it contains data from parent table that has many child rows etc. – Kamil Oct 30 '12 at 11:39
  • 1
    I tend to have multiple pages for this type of thing. So from the looks of your description I would have one page that lists the items with the option to view/edit an individual item. Then if my user chooses to edit that option it takes them to a view that loads just the details for that one item. Picture it like an Ebay page, a list of auctions then detail pages for each auction. That's the only practical way to handle things like this in my opinion. Having multiple edit to multiple items with multiple sub items on one page is confusing to write and confusing for the end user. – Rick Calder Oct 30 '12 at 11:43
  • Thanks for helping me in decision making. So i will create separate views to add, delete, modify record, and separate similar group of views for sub-records. – Kamil Oct 30 '12 at 12:03
  • Glad I could help, planning is the toughest part of getting an app right. – Rick Calder Oct 30 '12 at 12:09