Reputation: 15925
Does anyone know how to write the following PHP in ASP.NET(VB.NET)?
foreach ($_GET['listItem'] as $position => $item) :
$sql[] = "UPDATE `table` SET `position` = $position WHERE `id` = $item";
endforeach;
The data in $_GET['listItem'] looks like this:
item[]=1&item[]=2&item[]=3
Upvotes: 1
Views: 1071
Reputation: 8787
I know a little PHP but I'm assuming that $position is a zero-based index. If it's not, then you would swap the two lines of code that declare the position variable. Comments begin with the ' character.
'Request.Params accesses parameters from form posts, query string, etc.
Dim pairs = Request.Params("listItem").Split("&")
For i As Integer = 0 To pairs.Length - 1
Dim token = pairs(i)
Dim position = i.ToString()
'string position = (i + 1).ToString();
Dim id = Convert.ToInt32(token.Split("=")(1))
Dim sql = "UPDATE table SET position = " + position + " WHERE [id] = " + id.ToString()
'This line does the same thing, a bit more eloquently and efficiently
'Dim sql = String.Format("UPDATE table SET position = {0} WHERE [id] = {1}", position, id.ToString())
Next
I did this in C# first because I did not notice that you said VB.net. So here's the C# version. Thought I might as well leave it in. I made this a little wordy to demonstrate some of the nuances of C# and for clarity.
// Request.Params accesses parameters from form posts, query string, etc.
string[] pairs = Request.Params["listItem"].Split('&');
for (int i = 0; i < pairs.Length; i++)
{
string token = pairs[i];
string position = i.ToString();
// string position = (i + 1).ToString();
int id = Convert.ToInt32(token.Split('=')[1]);
string sql = "UPDATE table SET position = " + position + " WHERE [id] = " + id;
// This line does the same thing, a bit more eloquently and efficiently
//string sql = String.Format("UPDATE table SET position = {0} WHERE [id] = {1}", position, id.ToString());
}
Upvotes: 1