开发者

Is this conversion correct?

I've tried converting this ASP code to PHP, and would like to know if it is correct:

ASP:

Set emp = Server.CreateObject("Scripting.Dictionary")

EM_GENERAL=0
EM_AUDIO=1
EM_VIDEO=2 

emp.Add EM_GENERAL, "General"
emp.Add EM_AUDIO, "Audio"
emp.Add EM_VIDEO, "Video"

For each em in Emp
Response.Write "<option value=" + CStr(em)
If em = CInt(IT_FIELD) Then
    Response.Write " selected"
End If
Response.Write ">"
Response.Write Emp.Item(em)
Next

PHP:

$EM_GENERAL=0;
$EM_AUDIO=1;
$EM_VIDEO=2;

$emp 开发者_C百科= array();
$emp[$EM_GENERAL] = "General";
$emp[$EM_AUDIO] = "Audio";
$emp[$EM_VIDEO] = "Video";

foreach ($emp as $em) {
  echo "<option value=" + ($em);
  if ($em == intval($IT_FIELD)) {
    echo " selected";
  }
  echo ">";
  echo $em;
}


In VB, the For Each loop iterates over the Dictionary keys, not the values, so in your ASP code, em is the numeric key of your emp entries. In PHP, foreach iterates over the values, so $em is the value, not the key. Your if ($em == intval($IT_FIELD)) check will not work as expected. Fortunately the php foreach loop also supports the syntax foreach ($array as $key => $value), where $value would be equivalent to Dictionary.Item(key).

Try this instead:

foreach ($emp as $em => $value) {
  echo "<option value=" + ($em);
  if ($em == intval($IT_FIELD)) {
    echo " selected";
  }
  echo ">";
  echo $value;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜