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 Link to comment https://forums.phpfreaks.com/topic/263980-updating-database-records-with-php/ 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 Link to comment https://forums.phpfreaks.com/topic/263980-updating-database-records-with-php/#findComment-1352867 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 Link to comment https://forums.phpfreaks.com/topic/263980-updating-database-records-with-php/#findComment-1352868 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> Link to comment https://forums.phpfreaks.com/topic/263980-updating-database-records-with-php/#findComment-1352870 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 Link to comment https://forums.phpfreaks.com/topic/263980-updating-database-records-with-php/#findComment-1352890 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> Link to comment https://forums.phpfreaks.com/topic/263980-updating-database-records-with-php/#findComment-1352895 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 Link to comment https://forums.phpfreaks.com/topic/263980-updating-database-records-with-php/#findComment-1352978 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'] ... Link to comment https://forums.phpfreaks.com/topic/263980-updating-database-records-with-php/#findComment-1352997 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.