Hi all,
I'm just getting into PHP and loving it so far. I'm building my first CMS and have run into a bit of a stumper. I have categories for products and a table for each. Pretty standard. My issue is coming with a reorder of positions.
I'll use my category table for this example as it's very simple. The table has an auto-incrementing id, a category name and a position. When I add a category I just do a mysql_num_rows on the table and the new category's position is that var + 1. I'm using a nice jQuery drag and drop reorder plugin and no problems there.
What I'm having trouble with is when I delete a category I want to reorder the remaining ones in the table from 1 to whatever. Here's what I've come up with and it doesn't work for me.
// DELETE
if (isset($_POST['delete']) && isset($_POST['cat_id'])) {
$cat_id = $_POST['cat_id'];
$delete = "DELETE FROM categories WHERE cat_id='$cat_id' LIMIT 1";
$result_del = mysql_query($delete, $connection);
confirm_query($result_del);
// Reorder after delete
// get all categories by position
$get_cats = "SELECT * FROM categories ORDER BY position ASC";
$cats = mysql_query($get_cats, $connection);
confirm_query($cats);
// get number of rows in categories
$rows = mysql_num_rows($result);
//get ids of all categories
$get_ids = "SELECT cat_id FROM categories ORDER BY position ASC";
$ids = mysql_query($get_ids, $connection);
confirm_query($ids);
// put id values into array
$id_array = mysql_fetch_array($ids);
// reorder positions
for ($j = 1; $j <= $rows; ++$j) {
$row = mysql_fetch_row($result);
$fetch = $id_array[$j-1];
$query1 = "UPDATE categories SET position='$j' WHERE cat_id='$fetch'";
$result1 = mysql_query($query1, $connection);
confirm_query($result1);
}
}
I've been going over and over this for a couple days and just can't seem to get it. Any help would be really appreciated!