i'm developing a web platform in codeigniter (first time with CI) to calculate quotes for a growing number of different products.
The problem i'm facing is that each of my products have different sets of 10+ options but I want to save this data to just one table in my database. I have previously used a different table for each product allowing the table structure to represent the different sets of options however this isn't very scalable with our growing product range.
After some research it appears one solution would be using the 'serialize' function to store all of my post data (from the quote form) for each product in one column and then unserialize when I want to use this data...
Is serializing the data the best approach and would anyone be able to provide a simple example to show how to handle the insert from a form submission / retrieving the data?
Thanks very much in advance
EDIT: Searching will only ever on a product type or unique id. My thoughts were to have a table like 'id, product_type, product_options' with the product_options containing the serialized data?
EDIT 2: Taking an EAV approach seems like a good shout. I'm used to querying and returning a single result object to be passed into the view ($query->quote_ref, $query->quote_date for example). Could anyone point me in the right direction on how to use the single quote's data when the query would return multiple rows, one for each attribute?
As you mention you would not need to search on the serialized data, then yes this approach will be fine. I would opt for json_encode as the data is more human readable in this form.
Then code will depend on your DAL but a basic example would be:
$productOptions=array($_POST['option1'], $_POST['option2']);//etc will need to validate data
$databaseMapper->product_type='product type';
//your product_options column is suitable sized varchar
$databaseMapper->product_options=json_encode($productOptions);
$databaseMapper->save();
To retrieve:
$databaseMapper->loadById(20);
//$productOptions is a standard php array
$productOptions = json_decode($databaseMapper->product_options, true);
EDIT re displaying in your view.
This is codeignitor specific (whereas the above is not).
Based on code from here: http://ellislab.com/codeigniter/user-guide/general/views.html
In your controller:
//code similar to above to retrieve product options data, ideally contained within a model
$data['productOptions']=$productOptions;
$this->load->view('content', $data);
in your view:
<ul>
<?php foreach ($productOptions as $option):?>
<li><?php echo $option;?></li>
<?php endforeach;?>
</ul>
Related
I have some doubts about how to propose the structure of a database, the dilema is if I have to store serialized data or it's better to make a table with relational fields.
An example of that is to supose that application:
Customers have fees to pay
These fees are generated based on the date of the first payment and the payment deadline
Imagine an online course of 600 euros and that you define as the date of the first payment 2022-01-01 and payment deadline 2022-03-01, there we would have 3 installments of 200 euros
I am currently proposing a structure in which we have:
A table "clients"
A table "courses"
A table "course_has_inscriptions"
Well, inside the table "course_has_inscriptions", we have the fields:
id
course_id
client_id
issue_date
deadline_date
next_payment
quotes [I WANT TO SAVE AN ARRAY OF SERIALIZED QUOTAS HERE]
The array looks like this:
$quotas = [];
$quotas ['2022-01'] ['value'] = 200
$quotas ['2022-02'] ['value'] = 200
$quotas ['2022-03'] ['value'] = 200
Are there any advantages if instead of doing so I do it inside another relational table of the type "quotes" where I store:
id
inscription_id
quote
value
Thank you
Always choose the easier option, which is another table. This is easier because the data is easier to access. If you serialize the data you would have to retrieve it and unserialize before you can use it, if you use another table you can simply retrieve and use it. You can actually use the power of MySQL queries.
Think about it. For instance, later you will make a table containing the payments and you want to check whether people paid what they needed to pay. If you serialize the data you cannot do that with MySQL, you are forced to do it in PHP, which is not what you want.
People, who have encountered this problem many times, have summarized their experience in a couple of rules. They call this data normalization.
I'm trying to get used to relational active record but things look too complicated for now.
If it's not difficult please point me in right direction.
I have 4 tables.
Users
userID[pk],userName
Cars
car_id[pk],userID[fk to Users],car_nickname,make_id[fk to Makes],model_id[fk to Models]
Makes
make_id[pk],make_name
Models
model_id[pk],model_name,make_id[fk to Makes]
Now input data is userName,make_name,model_name and task is to get car_nickname from Cars table.
Is this possible using relations or should I do it step by step checking makes,models,users for IDs and then puting all IDs into Cars to get car_nickname ?
You can yous only one Model with reliations to all tables. And create one _form.php in the Views, where there will only fields you need.
Some halpfull information. if you save some data into some table and you need saved data id, you caN use $newid = $model->getPrimaryKey(); and assign to new variable , wich will save into other tables.
I think this is short way to solve problem
Is there a way to call field rows in a URL without using the column name??
So I currently have a posting site where users can select category or subcategories of choice from drop downs, how it's currently setup my site outputs links to the categories chosen such as..
topics.php?category=Food&sub_cat=Pies
topics.php?sub_cat=Pies
This allows users to go to either one of the links, or both
topics.php?category=Food&sub_cat=Pies
To give more functionality I am looking at adding textboxes instead of drop downs, the problem is users will more than likely enter the data in different boxes than other users, ie.
User 1. catbox: Food subcatbox: Pies
User 2. catbox: Pies subcatbox: Food
So in this case my current URL system won't return accurate results, so my question is would there be a way where "category" or "subcategory" could be replaced and just put the results together without them being listed in 2-5 different fields therefore not returning all the results that = to that value? "food" or "pie" in this example.
topics.php?xxx=Food&xxx=Pies
or
topics.php?xxx=Pies&xxx=Food
Looking at So homepage if you click "php" it will put php in the URL, click mysql and it will put "php+mysql" that sort of thing.
you can use parent child method in your database.your table would be like this
id - parent_id - category_name - depth
when you want to insert a data to your table it's depth will be one plus it's parent depth
when someone post to your page you first take query witch of the inputs has most depth then that will be your subcategory.
Calling field rows via parameters in your URL may be a very bad idea. It's a perfect way to allow a massive SQL injection attack. So, the answer is probably "yes, but HOLY MOLY PLEASE DON'T!"
Now it may be that your code is parsing these out on the back end and protecting them via any of a variety of methods, I can't tell from the amount of code posted.
i'm using custom fields in wordpress to store meta values. some custom fields have multiple values. i'm retrieving the array with "get post meta" which returns an array, as expected. but it seems the different items are ordered without any logic. some show up in the order they were entered, other's in opposite order, some total chaos.. what could i be missing?
i can't change the way the items are stored anymore.. there is too many entries in the database and the values show up perfect in the editing area but are stored in different order within the array.
This may or may not be possible. Wordpress is a bit like free energy machines - it violates a few useful concepts.
All your metadata is stored in the postmeta table. This table has a few fields: meta ID, post ID, meta key, value. Every time you add a meta, you add a row to this table. Every time you update a meta, however, you do not change the row order.
get_post_meta usually returns rows in the same order, so I am guessing you are doing some sort of sorting somewhere. Could we see some code? If it's always in ascending or descending but never as a random mix, you have a sort() lost somewhere.
I just recently had this same issue, here's what I found:
When you use get_post_meta(), WordPress checks if the meta data for the given object (post) has alreadby been loaded; if it has, then it's on the object cache (volatile, apc, memcached, etc) and load it's from there.
If the meta data it's not on the object cache, then it loads all the meta data for that object through update_meta_cache(), which queries the database without an ORDER BY
If your storage engine it's MyISAM, the results will be returned on a random order every time you update the meta data, but when using InnoDB the order in which the results are returned seems to be consistent (at least on my tests)
I have read other answers on this (or at least near to this) subject but I couldn't get a clear view of it so I'm asking for help again.
I have a complex dynamic HTML form that I would like to submit to database using PHP. The form is split into multiple tabs and in each tab I got checkboxes that trigger other parts of the form. Example: at a point in my form I got a checkbox group that has options of: "hotel" and "restaurant". If I check hotels, I get another part of the form displayed, specific for "hotels". Same thing for "restaurant". So it's very dynamic here and I don't know which would be the best approach for storing every form field in database. Because it could contain 15 fields or 20, depending on the selection. Any example would be appreciated as I'm not that advanced with database design.
Thank you!
So it's very dynamic here and I don't
know which would be the best approach
for storing every form field in
database.
I apologise if I have misunderstood you here but I believe that you should design the database according to the data and not the form. It is difficult to comment without knowing the exact details of your situation so here is an example:
If you usually dump all the data from a form into a single table, but because sometimes this will involve submitting 5 values and other times this will involve submitting 10 and so you are unsure how many columns your table should have, then I think the problem is in the database design.
Work out what pieces of data are dependent on other pieces of data. For example, you mention checking "hotel" might open up more fields specific to that choice. Let's assume this involves things like "en-suite", "bed type" etc. Then you should have 3 tables, a registration table (assuming the user is using the form to buy these services), a hotel table and a registration_hotel table. The registration table will record a number of details specific to the registration only such as the customer's name and a unique id number. The hotel table will hold information specific to the hotel only, such as how many rooms have en-suite. The registration_hotel table will hold details specific to that registration at that hotel. You might want a column of type bool to record whether the user requested "en-suite".
When submitting the form, check which pieces the user entered with if(isset($_POST['hotel']) && !empty($_POST['hotel'])). Then only send stuff to the registration_hotel table if that condition is true.
If this design results in making too many separate calls to the database, you might want to look into transactions which will help you to manage the speed and security of these calls.
If you can post in a specific example of something you don't know how to do, that would be useful.
You didn't specify how you can manage this dynamic form. Can you edit it's PHP/HTML source? One great thing would be if you can label your different variables like hotel[], restaurant[], etc.
If your submitted form is clear enough (i mean semantically correctly structured) you can store the whole submitted form serialized.
Note: this method only working when you don't need to search for specific items in your database.
Edit: maybe i'm misunderstood your problem.
You can create a 'metadata' table like this:
form_id | option_name | option_value
---------------------------------------
1 | hotel | true
1 | restaurant | false