dreampho Posted June 11, 2012 Share Posted June 11, 2012 Hi. I need some advice on how would be best to achieve the below: I have a list of database entries. Each entry is a row in the database, and has an entry_id assigned to it. I am loading each entry into a unordered list item and using jQuery ui to drag and drop their order. In the database I have a column in the same table as the entries, named sort_order. I need to update the sort_order column. I think the best way would be to submit a form which would send the entry_id and sort_order to a php script that then updates the database for each row. The problem is I am not sure how I should send the data for multiple rows at once, and then not sure how to enter the sort_order for each row separately. I would be very grateful if someone can give me some advice on this. Thank you Quote Link to comment Share on other sites More sharing options...
Barand Posted June 11, 2012 Share Posted June 11, 2012 Name the elements like this <form> <input type='text' name='sortorder[$id]' value='$sortorder' /> etc ... </for> The values will be posted as an array with ID as the key Quote Link to comment Share on other sites More sharing options...
dreampho Posted June 11, 2012 Author Share Posted June 11, 2012 Thanks Barand. I see, so each list item will have a form input which has something like: <input type="text" name="sort_order[$entry_id]" value="$sort_order" /> My next question would be, is there a way to assign an order. So the first li item is 1, then the next is 2, and so on? Thank you Quote Link to comment Share on other sites More sharing options...
Barand Posted June 11, 2012 Share Posted June 11, 2012 HTML ordered list <ol> <li> a list item</li> <li> another</li> </ol> Quote Link to comment Share on other sites More sharing options...
dreampho Posted June 11, 2012 Author Share Posted June 11, 2012 Thanks Barand. Now that I have the details, and am posting to php script, I have opened a connection to the database, but am not entirely sure how to post the array information. Could you possibly give me an example? Thank you Quote Link to comment Share on other sites More sharing options...
Barand Posted June 11, 2012 Share Posted June 11, 2012 simple example <?php if (isset($_POST['sort_order'])) { foreach ($_POST['sort_order'] as $id => $sortorder) { $id = intval($id); $sortorder = intval($sortorder); // update record using $id and $sortorder here } } ?> <form method="post"> <ol> <li><input type="text" name="sort_order[1]" value="2" /></li> <li><input type="text" name="sort_order[2]" value="3" /></li> <li><input type="text" name="sort_order[3]" value="1" /></li> </ol> <input type="submit" name="btnSubmit" value="Submit"> </form> Quote Link to comment Share on other sites More sharing options...
dreampho Posted June 11, 2012 Author Share Posted June 11, 2012 Thank you Barand. I have been trying to get this to work all afternoon, but my lack of knowledge is showing. For my foreach statment I am getting an error: Warning: Invalid argument supplied for foreach() in /home/chrisdav/public_html/template/process-sortable.php on line 12 foreach ($_GET['listItem'] as $sortorder => $id) { $id = intval($id); $sortorder = intval($sortorder); mysql_query("UPDATE exp_channel_data SET field_id_90 = $sortorder WHERE entry_id = $id"); } Can you see what I have done wrong, and possibly explain what it is to me? Many thanks Quote Link to comment Share on other sites More sharing options...
Barand Posted June 11, 2012 Share Posted June 11, 2012 Input names had id as the index <input type="text" name="sort_order[$entry_id]" value="$sort_order" /> When using foreach to traverse an array the syntax is foreach (array as key => value) therefore you need foreach ($_GET['listItem'] as $id => $sortorder) since the array key will be the id and the value will be the sortorder Is your form method GET or POST? If POST you need foreach($_POST['listitem'] ... 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.