Reputation: 801
I'm a webdeveloper and at the moment I'm using a very annoying and i think to difficult method for my cms for the following;
I'm making a gallery and I want to give the user the choice how to order/sort the images. so he can change the order of showing them (http://jqueryui.com/demos/sortable/). but what to do next; how to safe this order in the database? at the moment I have a extra table with the id's, in which order they have to appear on the website. way to difficult thinking I guess.
who knows a better method? this is hard to maintain aswel. I'm working with codeigniter, and jquery.
Upvotes: 0
Views: 182
Reputation: 2605
Add one extra field [photo_ord] to your Database table for Image_Order
Create Image List
<li class="left" id="someId">
<a href="#" >
<img src="image path here" alt="error" id="image_id_here" class="img_thumb"/>
</a>
</li>
Function handler (It Will called when any image will move)
<a href="#" onclick="change_order()" class="btn_1">Save Order</a>
Function will make ajax call for Update Image-Order
function change_order()
{
var urlst="";
var co=1;
$('.img_thumb').each(function()
{
urlst=urlst+this.id+'#'+co+'*';
co++;
});
$.ajax(
{
type:"POST",
url:"xhr_change_image_order.php",
data:"orader="+urlst,
success:function(data)
{
if(data=="changed")
alert("Image order changed.");
},
error:function()
{
alert('error occure');
}
});
}
File : xhr_change_image_order.php (will Update Database)
<?php
$url = $_POST['orader'];
$photo_arr = explode("*", $url);
foreach ($photo_arr as $k => $ph) {
if (strlen($ph) > 1)
{
// Update Query
// $arr[0] will contain image id
// $arr[1] will contain image order
UPDATE `photo` SET `photo_ord` = $arr[1] WHERE `photo_id` = $arr[0];
}
}
echo "changed";
?>
After that when create Gallery Simply fetch data using ORDER BY clause
SELECT * FROM photo ORDER BY photo_ord asc or DESC
Upvotes: 1