1

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

1
  • Could you explain the intent, and maybe I can help more, as I know nothing about PHP. Thanks. Commented Jun 11, 2010 at 12:22

1 Answer 1

1

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());
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.