Create and download CSV in PHP
Consider the possibility of quickly creating a CSV file with automatic file download. Consider the formation, separators and header for the ability to download the file.
All storage options will be correct, but each has its pros and cons, as well as the possibility and impossibility of using at certain points.
Consider each of the options :
1. In the base. Storage of data about the basket in the database is allowed if there is a unique customer identifier. Often this method is used for authorized users logged into their account.
This method has the ability to store the user's basket with any time interval. But this method is not suitable if the user is not authorized.
2. In Cookies . This method of storage is also possible in the use of long-term storage of the basket. Its advantage is that cookies can be used both for a logged in user and for an unlogged one.
The disadvantage of this method is in the memory that is allocated for one cell from cookies , namely 4kb . But this figure may differ depending on the browser - this is another minus.
If the store is wholesale and the basket can contain more than a dozen products, and besides, if you need to store a large amount of information, then this method will not work.
You can somehow build a "bicycle" for sharing cookies. For example, one cookie - one position of the product, and then somehow catch them and form a whole basket, but it's not worth it .
3. In session . In my opinion, this method is the most impractical. By default, the server keeps the client session for 1440 seconds , if during this time the client closed the browser and did not have time to place an order, then the entire basket will be lost.
Again, you can invent something super complicated to extend the life of the session, increase its storage on the server. But this is fraught with consequences. By extending the lifetime of the session, we cut off the frequency of the garbage collector, thereby making the storage of unnecessary information on the server.
Optimal long-term basket storage :
In my experience, the best way to store a shopping cart is to use a " base+cookie " combination. Such a bundle will work with all the pluses of cookies and bases.
1. In cookies, we create a unique identifier with the desired storage time, for example, a month.
2. We create a table in the database with the following fields: identifier (described above), user id (if authorized), last update time, and the cart data itself.
3. We write a minimal controller that works with this.
I'll show you clearly, but how to implement it on your site is up to you.
Let's say the table structure is :
CREATE TABLE `cart_table` (
`cart_key` varchar(50) CHARACTER SET utf8 NOT NULL,
`user_id` int(11) NOT NULL,
`last_update` int(11) NOT NULL,
`cart_data` text CHARACTER SET utf8 NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Schematic work code :
<?php
/**
* The function pulls data from the cart table
*/
function getCartData () {
/*Run initCartCookie()*/
$key = initCartCookie();
/*Look for the cart_key entry by key*/
/*Next, the necessary processing info*/
}
/**
* The function writes data to the cart
*/
function setCartData () {
/*Run initCartCookie()*/
$key = initCartCookie();
/* We look for the cart_key entry by key, if not, we create it */
/*Next, the necessary information on processing*/
}
/**
* If the identifier already exists, return it, if not, create and return
*/
function initCartCookie() {
if (isset($_COOKIE['cart_key']) && trim($_COOKIE['cart_key'])) {
return strip_tags(trim($_COOKIE['cart_key']));
} else {
$key = base64_encode(time() . '-' . rand(10,99). '-' . rand(10,99));
setcookie("cart_key", $key, (time()+(86400 * 30)));
return $key;
}
}
As a result , we will have to come up with the logic of the basket when authorizing the user, we have a large data storage in the database using the cookie key (unique key), we have a long storage period for the basket. I believe that the "base + cookies" combination is the right way to store the basket, but it all depends, of course, on the logic of each site.
Consider the possibility of quickly creating a CSV file with automatic file download. Consider the formation, separators and header for the ability to download the file.
Latest templates from ImageCMS 4.12, authorization is required to download.
Creating a key for imageCMS 4.9-4.12.1 Pro and Pre. You must be logged in to receive it.
When running the script from the console, there is no DOCUMENT_ROOT in $_SERVER. Let's try to get around this in our own ways, catch the file directory.
After developing any tool in PHP, the question becomes how resourceful the created code is and what elements it still affects. Let's take a look at xhprof installation, configuration, and how to work with xhprof.
Let's analyze the possibility of combining conditions in a query by groups in Propel. Consider an example of filtering by fields using ->condition() and ->combine().
Faced the problem of authorization 1s on the site. For some reason, the server did not accept the PHP_AUTH_USER and PHP_AUTH_PW parameters from it. Let's take a closer look at the solution and bypassing these parameters in $_SERVER .
The purpose of the article was to join (join) a table not declared in the schema (schema.xml) in propel2. Apparently a rare case or simply not enough documentation for this Propel ORM.