Answer the question
In order to leave comments, you need to log in
Cookies and button state: how to improve this code?
There are sort buttons. By price and by name, in both cases in two directions. The task is to display two buttons by default, and when you click on one of them, a reverse sort button appears in its place. For example, 2 descending sort buttons are displayed, and when you click on one, an ascending sort button appears in its place.
I did it through cookies. First, when the button is clicked, a js script is launched that hides the selector by ID and sets the cookie. In the php code, through if else, the variables are assigned the value "none", which is passed to the button as follows: in style="display:<? echo $style1 ?>".
The ambush is that instead of 2 buttons it displays three. Click on the price: 2 buttons "By name" appear nearby, click on "By name"
What else to change - already worn out.
function hideshow(x, y) {
document.getElementById(x).style.display = "none";
document.getElementById(y).style.display = "";
document.cookie = "hide="+ x;
document.cookie = "show="+ y;
}
<?
if ($_COOKIE['hide'] == 'a') {
$style1 = "none";
} elseif ($_COOKIE['hide'] == 'b') {
$style2 = "none";
} elseif ($_COOKIE['hide'] == 'c') {
$style3 = "none";
} elseif ($_COOKIE['hide'] == 'd') {
$style4 = "none";
}
?>
<span style="display:<? echo $style1 ?>" id="a" rel="name" class="sort-by asc" onclick="return hideshow('a', 'b')">По названию <i class="fa fa-long-arrow-up"></i></span>
<span style="display:<? echo $style2 ?>" style="display:none" id="b" rel="name" class="sort-by desc" onclick="return hideshow('b', 'a')">По названию <i class="fa fa-long-arrow-down"></i></span>
<span style="display:<? echo $style3 ?>" id="c" rel="catalog_price_15" class="sort-by asc" onclick="return hideshow('c', 'd')">По цене<i class="fa fa-long-arrow-up"></i></span>
<span style="display:<? echo $style4 ?>" style="display:none" id="d" rel="catalog_price_15" class="sort-by desc" onclick="return hideshow('d', 'c')">По цене<i class="fa fa-long-arrow-down"></i></span>
Answer the question
In order to leave comments, you need to log in
- PHP is superfluous here
- instead of cookies, LocalStorage is appropriate here
- why? It is simply easier to work with it, and it will not "burn out" by itself.
There are a couple of options for how to solve this:
1. Simple - store the current HTML in LS with all the buttons as is, update at the end of the hideshow function, when the page starts, replace the buttons with that what is in LS, or do nothing if they are not there
2. Difficult - create a model with an array of buttons, store it in a serialized form in LS, update it when clicked, process buttons according to the model when the page starts
Contacts and specializations should be placed in separate tables, there can be many of them in one enterprise.
To implement a minimum schedule, you need a table with the id of the enterprise address (different points may have different schedules), the day of the week (INT), the start and end of work on these days (TIME), the start and end of the lunch break (TIME). Plus a similar table of exceptions, where instead of the day of the week there is a date field (DATE).
Did I do everything right, or are there too many attributes in the "enterprise" entity and need to be separated? As an option to make an additional entity "Contacts_enterprises", where to put the address, phone numbers, time and days of work ...depends on the queries to be used
Is my DB in 3NF (third normal form) at the moment, and if not, how do I do it?no
How do normal Homo sapiens implement fields like "work_hours" and "work time"? Do you need to do separate opening and closing times, or can you fit everything in 1 field just as a string?json field indicating each day of the week and the work schedule for it
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question