Jump to content

Updating database records with PHP


dreampho

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.