I've written dynamic menu with Php and MySql . here is my MySql table structure =>
CREATE TABLE MENU(
id INT(3) NOT NULL AUTO_INCREMENT PRIMARY KEY,
sort_id BIGINT NOT NULL,
title VARCHAR(50) NOT NULL,
etc ...);
and here is html form =>
<input type="text" name="title">
<select name="pos">
<?php
if ($r = $connection->query("SELECT id,sort_id,title from menu order by sort_id ASC")){
$row = $r->fetch_assoc();
/* here with help of php I'm retrieving data from marked table. I need this because want to sort categories with help of this select element , forward or backwards */
echo "<option value='" . $row['id'] . "'>" . $row['title'] . " After</option>";
}
?>
</select>
<input type="submit" name="ok">
How you can see guys, I'm trying to sort menu items with help of select element (with sort_id column), but I can't make up my mind how to manipulate with sort_id numbers in MySql table , how increase or decrease to achieve goal.
Please help , if anyone knows how to do that or have an idea ?
PS. I want to sort menu items not in select form but also in navigation bar which ,I have not written here. for example if first category is named "one" and second "three", I'd like to insert between them category named "two" in menu NOT IN SELECT ELEMENT
UPDATE
here is inserting script
<?php
$main_sort_res = $connection->query("SELECT sort_id FROM menu WHERE id=" . $_POST['pos'] . "");
$sort_num = $main_sort_res->fetch_assoc();
if ($k = $con->query("SELECT id FROM menu WHERE id=" . $_POST['pos'] . " AND sort_id=(SELECT MAX(m_sort_id) FROM main_menu)")){ // here defined if selected element is last or not
if ($k->num_rows==0){ // not last element
$connection->query("INSERT INTO menu(sort_id,title) VALUES(" . ($sort_num['sort_id']+1) . ",NULL,'" . $_POST['title'] . "')")
} else { // last element in select form
$connection->query("INSERT INTO menu(sort_id,title) VALUES(" . ($sort_num['sort_id']+10000) . ",NULL,'" . $_POST['title'] . "')")
}
}
?>
how you see I am inserting in sort_id manually numbers which is pretty bad , but I've no other idea . I become confused when inserting categories many times after from the same element ( I mean from the select element). And reason is that adding value by 1 , but I can't imagine what function would be nice not to match sort_id-s in column

id, and only dosort_idandtitle. My question becomes how issort_iddetermined?idfield manually, but that field is just a placeholder. I would say usingidfor the value is fine because I am assuming you are passing that or inserting it to another table.