techker Posted March 26, 2016 Share Posted March 26, 2016 Hey guy's i made a small catalog style for my body's shop,in the admin he inserts and deletes products. but he would like to order the products the way he wants to,not by Id or ASC desc... so i added a field order inserts 1 to 10 lets say. so how can i do it so first there is not replicates and second when i do my query how can i make it order by numbering 1 to 10.. Quote Link to comment Share on other sites More sharing options...
Barand Posted March 26, 2016 Share Posted March 26, 2016 To prevent duplicate values, put a UNIQUE index on the field. Use "ORDER BY" in the query to sort by the field. Quote Link to comment Share on other sites More sharing options...
techker Posted March 26, 2016 Author Share Posted March 26, 2016 but how can i warn the user ? Quote Link to comment Share on other sites More sharing options...
Barand Posted March 26, 2016 Share Posted March 26, 2016 If you get a duplicate key error , tell the user. Quote Link to comment Share on other sites More sharing options...
techker Posted March 26, 2016 Author Share Posted March 26, 2016 there should be a way to warn before he submits?cause if he makes like 3 errors it can take a long time to add a single product.. maybe a select box that shows only the numbers not taken? Quote Link to comment Share on other sites More sharing options...
Barand Posted March 26, 2016 Share Posted March 26, 2016 Good idea! Do it that way then. Quote Link to comment Share on other sites More sharing options...
techker Posted March 26, 2016 Author Share Posted March 26, 2016 can you correct this? $num= $_POST['num']; $CheckNum = mysql_query("SELECT Order from Products WHERE Order = '$num'"); while($test = mysql_fetch_array($CheckNum)) if ($test ) { echo "Is not valide"; } Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted March 26, 2016 Share Posted March 26, 2016 (edited) when you insert new item(s) starting at position number x, all current items having a position number > x would need to have their position number UPDATEed to be their current value + the number of items that are being inserted. an insert of new data would involve an UPDATE for the position number field for some of the current items, followed by an INSERT for the new data with it's now vacated position number(s). Edited March 26, 2016 by mac_gyver Quote Link to comment Share on other sites More sharing options...
techker Posted March 26, 2016 Author Share Posted March 26, 2016 true i can always use update.. how can i auto increment a field starting from 1.. and if you delete the product it needs to reset the fields..cause if i delete the order 3 there will be missing a 3 in the order.. Quote Link to comment Share on other sites More sharing options...
benanamen Posted March 26, 2016 Share Posted March 26, 2016 (edited) can you correct this? $num= $_POST['num']; $CheckNum = mysql_query("SELECT Order from Products WHERE Order = '$num'"); while($test = mysql_fetch_array($CheckNum)) if ($test ) { echo "Is not valide"; } The correction you need is to stop using obsolete code that has been completely removed from the current php version. Use PDO. Your current code is also ripe for an SQL Injection attack. You never ever send user supplied data directly to the database. https://phpdelusions.net/pdo Edited March 26, 2016 by benanamen Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted March 27, 2016 Share Posted March 27, 2016 Maintaing a perfect sequence of integers will be painful, both for you and the admin. Why don't you decouple the technical implementation from the user interface? The admin could, for example, arrange the products with simple drag-and-drop and doesn't have to care at all about how exactly the ranks are stored. Then you're free to choose any implementation which is technically suitable without having to worry about nice-looking numbers. To avoid re-arranging products all the time, you could use large gaps or even foating numbers instead of integers. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 27, 2016 Share Posted March 27, 2016 Maintaing a perfect sequence of integers will be painful, both for you and the admin. Why don't you decouple the technical implementation from the user interface? The admin could, for example, arrange the products with simple drag-and-drop and doesn't have to care at all about how exactly the ranks are stored. Then you're free to choose any implementation which is technically suitable without having to worry about nice-looking numbers. To avoid re-arranging products all the time, you could use large gaps or even foating numbers instead of integers. I was going to suggest the same thing. But, I think this example is a closer fit to what the OP is requesting. It includes two methods (serialize and toArray) that can be used to get the order of the elements to be sent to the server to update the sort order. Quote Link to comment Share on other sites More sharing options...
techker Posted March 27, 2016 Author Share Posted March 27, 2016 ya i think i need to site down with my body on this..lol the drag and drop is a good idea..just need to make a database that will store the order that he sets it with an id. then extract by order by.. Hey thx for the help guys! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.