The structure of its database foresees a relationship between two tables: the colors are recorded in a table (if I understand correctly, the value of the color in hexadecimal), while the clothes data is in another. The clothes table makes a reference to the color table by saving the color code in a column that is a foreign key.
In this case, when inserting a new garment in the bank, what enters the table of garments is not the value of the color, but the code of the color as it appears in the other table (in the column cd_cor
). To get the clothing data and its color in a single query, you use a JOIN
, as demonstrated in some other questions here on the site (for example, here and here).
What I’m finding a little strange is that it doesn’t make much sense to use a separate color table if your site allows you to choose any color rather than offer a limited number of options. If you had options like "navy blue" or "snow white", it makes sense to use the color table. But if you want to record the haxadecimal code of color, and accept any color among the 16+ million possible in that notation, it would be more worthwhile to have a text column directly in the table of clothing, and eliminate the relationship between the tables.
Original response, written before the questioner posts the structure of the database
I would recommend changing the data type in your table, and saving as text. To do this in Postgres, you use the ALTER TABLE
. For example:
ALTER TABLE minhatabela
ALTER COLUMN nomedacoluna TYPE char(7);
However, if you really want to save the data as a number, PHP has the function hexdec
that makes the conversion easily:
$preto = hexdec("#000000"); // 0
$branco = hexdec("#FFFFFF"); // 16777215
$azul = hexdec("#0000FF"); // 255
$verde = hexdec("#00FF00"); // 65280
$vermelho = hexdec("#FF0000"); // 16711680
You will probably need to convert back to hexadecimal. It has the function dechex
for this, but you will need to put the #
and zeros left manually. For example, only the dechex
:
$azul = dechex(255); // "FF" e não "#0000FF"
One of the ways to fix it:
"#" . substr("000000" . dechex(255), -6); // "#0000FF"
Why don’t you make the column VARCHAR?
– Sergio
I have tried and I can’t. From error saying that the entry and invalidates.
– Indaiara Ribeiro
How did you try to change the type of column in the bank? Yes. Anyway, if you want it is also possible to convert the color to number and write as int even. What language you use on the server?
– bfavaretto
Hello use Postgres. I would like to know yes how I do. Why I try to change the type in the same bank and not the
– Indaiara Ribeiro
To using php, html and a bit of js
– Indaiara Ribeiro