Reorder pictures in a webgallery - php

I have a webgallery (made with laravel) and would like to add the possibility to reorder the images... Now, I have thought of several approaches but for every aproach i find that there should be a better way of doing it.
the gallery does not use javascript, so ones changes have been made it needs to be sumbitted and reloaded to reflect the changes
The main difficulties are:
how to store the order in the database? an additional Integer column?
how to add a picture "between" two others?
how to handle it at a frontend level?
So far the best ideas I had are these:
a column with integers, order by clause on this column. Frontend: a move up and a move down button.
problems of this solution: it needs a refresh after each single movement. it needs to identify the previous/next picture and swap the number with that one. To move a single pic from the end of the gallery to the top it takes forever.
a column with integers, automatically prefilled in steps of 100, order by this column + upload time in case of same numbers, Frontend: a textbox where the user can specify the integers for each picture and a submit button.
problems of this solution: does not look very professional. solves all the problems of the previous solution
same as previous solution but with double values to be able to insert pictures without limits.
They all dont seem the real deal.. Any sugestion on how to do it properly is welcome. thanks

I have done that kind of sorting in OpenCart products list (custom backend design)
Sort order was additional column order INT(11) in database
We had 3 input fields: up/down/custom
Where custom was dropdown of all indexes from 1 to max-items.
All inputs does the same:
Take new order value and shift all elements except itself. Up or down shift depends if you move element to front or to back of current position
UPDATE order FROM products SET order = :newOrder WHERE id = :currentItemId
if ($newOrder > $oldOrder)
UPDATE order FROM products SET order + 1 WHERE order >= :newOrder AND id != :currentItemId
else
UPDATE order FROM products SET order - 1 WHERE order <= :newOrder AND id != :currentItemId
Inserting does the same update, just first query becomes INSERT INTO
To get rid of ugly refresh of page on every action we do Ajax requests and re-sorted DOM with jQuery

Related

MySQL editable sort index in CakePHP

I am trying to implement a way to sort elements in a list based on the order they were added from oldest to newest. This would be easy to do in MySQL using an ORDER BY on a created DATETIME. The problem is I then want to be able to move elements up and down the list and have that order saved in the database, but also make sure that new elements get put on the end of the list.
I have thought of using an INT index and just increment that for each new item that is added to the list. Then when an existing item is moved up or down in the list, swap the index numbers. Does this sound like the best way to achieve this result? If not, would anyone be able to provide some insight of a better way. Thanks
So I ended up solving this by using an INT field in the MySQL table called order. When new items are added, the order field is just incremented from the previous largest order in the table. When items are moved up or down in order, all other items orders are shifted up or down accordingly so that the order numbers are always continuous. On delete, orders are also shifted down.

Combining multiple rows or results with the same title to form drop down menus with PHP and MySQL

So I am picking up a project that was quit halfway through by the last guy so that I could get some more practice with PHP and databases. I have run into a problem, and I am sure it is common enough that there is a standard solution, but I am unable to find one.
The db I am working with has 4,600, so reorganizing is out of the question. It is a db of liquers for a wholesaler. Here is what the results page looks like currently:
What I am trying to set it up so the results are returned in list form, with only one title and dropdown menus for the different sizes/prices of products that looks like this:
The problem is that there are multiple entries in the db for each product. In this example there are 3, while some have 1, and some have 2.
I am really not sure how to go about this, and any help would be greatly appreciated. Thank you.
I'm not sure about the PHP syntax, but pseudocode here's what you could do:
allProductsReturnedFromMySQL = QueryYourDatabaseForAllProducts()
Hashtable[productId, List[productSizes]] dropDownsByProduct;
Hashtable[productId, commonProductInformation] uniqueProducts;
foreach (product in allProductsReturnedFromMySQL) {
if product.productId not in uniqueProducts
then add it with the product information that does not vary
if product.productId not in dropDownsByProduct
then add it with an empty list
append the size of this product to the corresponding list in dropDownsByProduct
}
After that little bit of logic you'll have all your unique products with the common properties for each one, and a way to fetch the corresponding sizes drop down. If you wanted to do this purely in SQL to minimize the data that's transferred, you could do something like this:
-- this would get you your products
select distinct id, property1, property2 from product
-- this would get you your drop downs by product
select id, size from product order by id
You can then build the same drop down hashtable by iterating through the second result set.
I'm not sure if this is the best way, but I've always approached this by altering the query so that it is sorted by product name. Then as you iterate through the rows, check to see if the product name matches the one you just processed. If it's the same, then this row is a different size of the same project.

Take snapshot of DB table row?

I want to take a snap shot of a row in a MySQL table.
The reason being, if someone buys a product. I want to take a snapshot of that product to store for the order.
It needs to be a snapshot to maintain data integrity. If I just assign the product to the order, if the product changes in the future the order will show those changes. For example if the price changes, the order will now load the new data and say that it sold the product at its new price rather than what the price was when the order was placed. So a snapshot needs to be assigned to the order instead.
The way I did this in the past was having 2 tables, one for products, and one for snapshots of products. The snapshot had every column as the regular table plus extra colums like order_id
I had a script to take a snapshot that automatically looked at the fields in the regular table and tried to do an insert into the same fields into the snapshot table.
The biggest problem with that approach is that, if I added a column to the regular table and forgot to add the same column to the snapshot table; the script would try to insert data into a non existent field and fail.
I also disliked the idea of having 2 tables that were nearly identical. I think maybe figuring out a way to use one table for both purposes might be better.
So I am wondering if there is a known method I am unaware of to solve this issue?
My previous project used no framework but my next one will be using CakePHP if that matters.
I think the best way of handling this would be to roll the "snapshot" information into an orders_products table. So if you have an order, store the total price, tax, etc. information in a single row on the orders table and reference that order_id on your orders_products table. On your orders_products table, you can have order_id, product_id, price, quantity, discount and whatever else you need.
Seems like your previous is fine. But that you just need to do more testing to ensure that you don't forget to add the new fields to the snapshot table. Seems like a basic test that would be easy to do. The other alternative is to just use a big text field, and store the snapshot as XML. This will let you store the snapshot regardless of if the schema changes. Depending on how much you want to query this data, it may or may not work for you.
Also you may not want to store every field, as it may just take up extra space. For instance, if you have the location of the image file of the item, you may not want to store that, as it may not be important at a later date. You could try querying information_schema to query which fields are in the snapshot table, and only copy the available fields.

PHP> Sort query results by name while letting each letter be on the top sometimes

I'm currently working on a site that will display a list of online shops,
Each shop will be stored on my database and I'll be using PHP to select and display them.
But since those shops will pay me, I want to let each shop to be on the top of the list sometimes,
(for example if the shop name starts with a "Z", they will probably complain for being on the bottom of the list all the time, so I want to keep it fair).
So I thought about letting each letter be on the top of the list for an hour, but i have no idea how to do that..
Is that even possible?
Thanks in advance!
I'd show a separate box and call it "today's pick" or something with just one shop in it. That way you can push the shops starting with "Z" to the top once in a while and at the same time keep the user experience of a list of shops which is sorted normally.
Then use the database to save which shop has been in the "today's pick"-box how many times to get them all up there equally.
There's no sane way (that I'm aware of) to handle this directly in SQL without adding a "priority" field to your schema (although it's possible, it would be convoluted at best). That said, here are two suggestions:
Modify your schema
Simply add a "priority" field to the relevant schema and sort by priority, name (or whatever the default is). You will of course need to reset the priority field every hour, but this is a fairly trivial task.
Handle it in PHP
Carry out the query as per usual.
Grab all the results into an array.
Re-prioritise as required based on the current hour. (You'll need to array_splice the item(s) you want to bump out of the array and then array_unshift them to the top.)
Output based on the array.
This will of course become more convoluted/less efficient if you need to handle pagination, but the basic idea is the same.
A nice solution would be to add another column to the database with the shop names, and call it something like "last_shown" then when you show this shop, update the column with a timestamp, and each select do something like:
"SELECT name,link FROM shops ORDER BY last_shown DESC"
then in php you could check
<?php
if($row['last_shown']+3600 > now()){
//run select but in ASC order
//update the new row's column to the current timestamp
}
?>
that way it will only update once an hour, but otherwise it will keep selecting the shop at the top of the list for the hour
sorry it's a bit of a mess i just typed this out quickly at work
You can add
1) a extra column as shown_times in schema
2) order by shown_times asc
3) & as a shop is shown you would +1
or
Another solution :
You can even use ORDER BY RAND()

PHP/SQL Cycle items every x minutes

I have a friend who runs an online auction website. He currently has a featured items section on the homepage that he wants to have cycle an item every X amount of minute. The site runs off a MySQL database which I haven't actually seen yet.
The current code that he is using is a big, long messy Javascript code that is causing all kinds of errors.
The items would have to cycle in order and when they get to the end, go back and repeat again.
What would be the best approach to take to this using PHP?
EDIT: I mean from a backend SQL perspective. Not the UI.
Thanks
Ben
Assuming you have a separate table for the featured items (probably has an item ID referencing the main items table and maybe other info)... In this table, add a last_featured column to represent the time the item was last shown. From there, you can manipulate your queries to get a rotating list of featured items.
It might look something like this (as a weird pseudocode mix between PHP & MYSQL):
// select the most recent item in the list, considered the current item
$item = SELECT * FROM featured_items ORDER BY last_featured DESC LIMIT 1;
if ($item['last_featured'] > X_minutes_ago) {
// select the oldest item in the list, based on when it was last featured
$item = SELECT * FROM featured_items ORDER BY last_featured ASC LIMIT 1;
// update the selected item so it shows as the current item next request
UPDATE featured_items SET last_featured=NOW() WHERE item_id = $item['item_id'];
}
Note that this requires 3 call to the database... There may be a more efficient way to accomplish this.

Categories