How would you save the quote products in a database? Serialize data?
http://cl.ly/6419969e30cd26e2f32a
They can create however many rows they want... It's for a quote system. Create a quote with however many products. I need to save X Product At X Qty and X Price foreach one.
Serialize in the only thing I could come up with, but I hate unserializing so very much. I didn't know if there was a quicker/more efficient way.
I also have all this information to store with / link with it http://cl.ly/40cc9fea47932b3740b2
PHP (Kohana) > mySQL
Thanks for any input you may have.
I would split the items on a quote into a separate child table:
Quote QuoteItem Item
========= =========== ============
QuoteID QuoteID ItemID
Subject ItemID Description
ValidUntil Quantity Cost
Address Price
Create one entry in the quote table, and then for each item on that quote create an entry in the QuoteItem table with the corresponding QuoteID.
Related
I'm trying to find unaccounted for numbers within a substantially large SQL dataset and facing some difficulty sorting.
By default the data for column reads
'Brochure1: Brochure2: Brochure3:...Brochure(k-1): Brochure(k):'
where k stands in for the number of brochures a unique id is eligible for.
Now the issue arises as the brochures are accounted for a sample updated data would read
'Brochure1: 00001 Brochure2: 00002 Brochure3: 00003....'
How does one query out the missing numbers, if in the range of number of say 00001-88888 some haven't been accounted next to Brochure(X):
The right way:
You should change the structure of your database. If you care about performance, you should follow the good practices of relational databases, so as first comment under your question said: normalize. Instead of placing information about brochures in one column of the table, it's much faster and more clear solution to create another table, that will describe relations between brochures and your-first-table-name
<your-first-table-name>_id | brochure_id
----------------------------+---------------
1 | 00002
1 | 00038
1 | 00281
2 | 28192
2 | 00293
... | ...
Not mention, if possible - you should treat brochure_id as integer, so using 12 instead of 0012.
The difference here is, that now you can make efficient and simple queries, to find out how many brochures one ID from your first table has, or what ID any brochure belongs to. If for some reason you need to keep the ordinal number of every single brochure you can add a column to the above table, like brochure_number.
What you want to achieve (not recommended): I think the fastest way to achieve your objective without changing the db structure, is to get the value of your brochures column, and then process it with your script. You really don't want to create a SQL statement to parse this kind of data. In PHP that wolud look something like this:
// Let's assume you already have your `brochures` column value in variable $brochures
$bs = str_replace(": ", ":", $brochures);
$bs = explode(" ", $bs);
$brochures = array();
foreach($bs as $b)
$brochures[substr($b, 8, 1)] = substr($b, strpos($b, ":")+1, 5);
// Now you have $brochures array with keys representing the brochure number,
// and values representing the ID of brochure.
if(isset($brochures['3'])){
// that row has a defined Brochure3
}else{
// ...
}
The employees list page on my rails app has a requirement of showing links for each of the starting characters in employee names, to facilitate quickly searching for users by a letter.
The link list should include only those letters which have names starting from them. Eg: If the table contains 3 names - Joe User, Jill User, and Example User - only two links E and J should be displayed.
Wondering what's the most efficient way to do this.
As a first attempt, I added a class method in the Employee class as follows:
def self.list_of_starting_characters
array = []
('A'..'Z').to_a.each do |char|
array << char unless self.where("name like '#{char}%'").count.zero?
end
array
end
This gets called on every render of the view; so changed it to use a class variable as follows:
##starting_characters_list = []
def self.list_of_starting_characters
return ##starting_characters_list unless ##starting_characters_list.empty?
array = []
('A'..'Z').to_a.each do |char|
array << char unless self.where("name like '#{char}%'").count.zero?
end
##starting_characters_list = array
end
Now it is called only once per session. Are there better ways to accomplish this?
One other option I am considering is to store the list of starting characters in a separate table and update it when the employee data is modified, but worried it might be too much of a hassle, to keep the separate table in sync with the main table.
The following should work for you:
first_letters = self.pluck(:name).group_by{ |name| name[0].upcase }.keys
It grabs all User's names, group them by the first letter of the name and get the only the keys of the hash generated by the group_by.
I have data as below:
BinaryName Licens_details
-------------------------------------------------------
usr/sbin/if_up BSD-2C:63, BSD-2C:1576, BSD-3C:4, BSD-3C:214,
BSD-3C:241, BSD-3C:248, BSD-3C:259, BSD-4C:62,
BSD-4C:137, BSD-4C:154, BSD-4C:164, BSD-V:3,
NOTE:4, NOTE:5, NOTE:15
-----------------------------------------------------------------------------
usr/sbin/random BSD-3C:4, BSD-3C:214, BSD-4C:61, BSD-4C:154,
NOTE:4, NOTE:5, NOTE:15, ZLIB:18, ZLIB:55
---------------------------------------------------------------------------
usr/bin/vcapture-test BSD-4C:154, NOTE:2, NOTE:4, NOTE:5, NOTE:15
-----------------------------------------------------------------------------------
License_details data is of pattern :
Each license_id represents full notice text in another table, copright_info id represented by integer is index into other big table with copyright info, author, acknowledgement details etc.
The question is, how do I design query to get both license id, and copyright id since they are very random, not serial.
One way is to manually see every id and design a query for every row of data.
But this is quite takes more effort as we more than 300 pages of data.
Another way is to have that data in excel, and parse by perl script.
The perl script becomes a table column. I mean every row of table should execute a perl script to get list of license ids, and copyright ids.
Any help will be greatly appreciated.
I have a Products table, an Imprint table, a Manufacturer table and an ingredients table - along with other tables.
I am currently generating information and use what is called "file_name" as the id (it is a FDA assigned 36-40 digit id that goes with each product). What is happening now is people enter of few letters and the autocomplete dropdown provides the exact spelling. When they click SUBMIT and it brings up all entries that match. If they select a generic product it also includes the "brand" product - if they enter a brand it also includes the generic.
The current display is as follows:
Sold by: Sold using Available Chemical name Data based
name Since: on company
report submitted:
C----- Health Lortab 2011-01-13 Hydrocodone Bitartrate etc 2010-12-07
R—Distributors Hydrocodone 2010-02-18 Hydrocodone Bitartrate etc 2009-12-17
Bitartrate
And Acetaminophen
C-- Health Vicodin 1983-01-07 Hydrocodone Bitartrate etc 2009-11-03
R—Distributors Hydrocodone 2010-07-30 Hydrocodone Bitartrate etc 2010-12-28
Bitartrate
And Acetaminophen
This is working fine. I will be adding a check box on the left which will allow up to 3 of the products to be chosen for comparison and additional information.
THE NEXT STEP:
After they check 1, 2 or 3 items, I want to display like this:
"ALL of the versions of the product you checked contain the following ingredients:"
(Those ingredients common to all chosen products).
ACETAMINOPHEN, CELLULOSE, CORN, CROSPOVIDONE, HYDROCODONE BITARTRATE, MICROCRYSTALLINE, STARCH and STEARIC ACID
(Show the ingredients in each product NOT held in common by all.
End result in COLUMNS)
“IN addition, EACH of the products you chose have the following ingredients:"
COLUMN 1(Product 1) COLUMN 2 (Product 2) COLUMN 3 ( Product 3)
COPOVIDONE MAGNESIUM STEARATE CROSCARMELLOSE SODIUM
CROSCARMELLOSE SODIUM POVIDONE D&C YELLOW NO. 10
D&C RED NO. 27 SILICON DIOXIDE FD&C BLUE NO. 1
D&C RED NO. 30 POVIDONE
HYDRATED SILICA SILICON DIOXIDE
MAGNESIUM STEARATE SUCROSE
End of display
Ingredient table: there are 20,000 rows,46 columns. Each row is a different drug and contains the id, file-name and then the ingredients, Each drug (row) has a different combination and number of ingredients. Unused fields are marked "Null";:
Ingredient columns in each row:
id, file_name, 0_gred, 1_gred, 2_gred, 3_gred, 4_gred, 5_gred, 6_gred, 7_gred, 8_gred, 9_gred, 10_gred, 11_gred, 12_gred, 13_gred, 14_gred, 15_gred, 16_gred, 17_gred, 18_gred, 19_gred, 20_gred (etc. up to 43_gred)
The question is how and what approach to use to get the format I need for the ingredients. I have experience in developing registration systems, I am fairly knowledgeable in PHP and am starting to get use to PDO. My Sql experience is minimal and is basically on a "need to know" basis.
I have wondered whether I should focus on a monster of a MySQL query or more on the php side. I thought about a query giving the ingredients common to all 3 and then subtracting that result from each individual drug list to get Part II but that appears to be quite advanced mysql – especially since I need the data to switch from row to COLUMN layout. Any help?
Another idea was to do a query that concatenated the ingredients of each row and then doing array procedures on the php side. Problems, I am having a hard time finding the right code to pull the ingredients out of each row since the number of “NULL” fields compared to “used” fields varies with each row. (How to count null COLUMNS in PDO query? I’ve tried and get the full count of columns in the table.)
To me, this is a braintwister with several steps. I’m looking for the “magic” MySql Code (if it exists) and or suggestions as to what approach (using php, mysql, PDO) you would pursue.
Your interest/help is appreciated!!
Laura
I figured out a php way to do what I needed. I'm sure there is a faster MySql way. If you know of one please share! Laura
First, I set the Mysql empty values to default to "no_ingre".
Then I did the following code 3 times for the 3 choices:
SELECT 'all the ingredient fields WHERE id=$name_of_id'
$result=$stmt->fetch(PDO::FETCH_ASSOC);
foreach($result as $key => $value) {
if ($value == "no_ingre") unset($result[$key]);
}
FIND THE ingredients COMMON TO ALL:
$same_detect= array_intersect($result, $result2, $result3);
$p_same=(implode(", ", $same_detect));
echo "<br />The below ingredients are in all of the chosen products<br />";
echo $p_same;
To FIND THE ONES NOT COMMON TO ALL: Do the below code 3 times (for each ingredient array):
foreach ($result as $item) {
if (!in_array($item, $same_detect, true)) {
echo ("<tr><td>" . $item . "</td></tr>"); }
I'm looking for the best way to design a shopping cart. I'm at a crossroads with how to handle a product that has multiple options being color and size. The main issue being inventory management for the colors and sizes.
Currently on admin panel:
textfield for color
textfield for quantity
User seperates colors by comma along with quantity so color & quantity match when I explode/implode them into arrays, thus allowing me to manage the quantity whenever something is purchased by their keys after doing an array search for the color.
Right now I just have one table holding upc/name/color/quanity/price etc...
Should I be using some type of foreign key and having tables for color/quantity on their own?
This is more of a design question and not a show me exactly how to do it question as I'm just trying to learn the most optimum way to manage a database.
Thanks!
I'm not quit sure I understand your question but I think you are looking for suggestions on how to data model this and how to represent arrays of data from this model as strings that can be manipulated browser-side.
For the data model, it sounds like you need a 5 tables:
1) Product, 2) Color, 3) Size, 4) an associative table between Product and Color, 5) an associative table between Product and Size. Tables 4 and 5 implement the many-to-many relationships between products and the different colors offered, and between products and the different size offered.
Then you can settle on a standard way of representing shopping cart items as strings. Say:
<productId>,<qty>,<colorId>,<sizeId>
Arrays of these shopping cart items would be semicolon separated.
Looking for a way to represent the color and size choices for a particular product? I often use this type of query to retrieve choices as a comma-separated list which are then easy to deal with client-side:
-- =============================================
-- Author: Joe Blo
-- Create date: Jan 1, 2010
-- Description: Returns list of color choices for
-- a product in CSV format
-- =============================================
CREATE FUNCTION [dbo].[fn_GetProductColorsCSV]
(
#pProductUPC VARCHAR(30)
)
RETURNS VARCHAR(MAX)
AS
BEGIN
-- Declare the return variable here
DECLARE #Result VARCHAR(MAX)
-- Add the T-SQL statements to compute the return value here
SELECT #Result = COALESCE(#Result + ',', '') + CAST(C.[ColorId] AS varchar)
FROM dbo.[ProductColorJunction] PCJ
INNER JOIN dbo.Color C ON C.[ColorId] = PCJ.Color
WHERE PCJ.ProductUPC = #pProductUPC
-- Return the result of the function
RETURN #Result
END
Why not start by exploring how open source shopping carts have handled similar tasks. osCommerce is one that comes to mind.
Use 3NF
http://www.troubleshooters.com/littstip/ltnorm.html
http://en.wikipedia.org/wiki/Third_normal_form