我只使用PHP大约一个星期(即使我已经使用ASP 12年了,我绝不是专家)所以请耐心等待我...我想转换我的经典ASP网站到PHP。到目前为止,我已经能够找到大部分问题的答案,但我仍然坚持这个问题。
我正在寻找与ASP中使用的.MoveNext函数相当的PHP。
在这个例子中,我有一个包含大约450条记录的表,虽然我希望它们全部显示,但我只想要每列100条。它可能不是很好的代码,但它适用于我。
set rsSpecial = Server.CreateObject("ADODB.recordset")
'Grab all the Supporters Names and Website URLs Order by Name
rsSpecial.Open "SELECT sName, sURL FROM gktwspcf ORDER BY sName ASC", conn
if not rsSpecial.EOF then
sSpecialFriends = sSpecialFriends & "<table width=" & Chr(34) & "98%" & Chr(34) & ">" & vbCrLf
Do
i=0
sSpecialFriends = sSpecialFriends & "<td valign=" & Chr(34) & "top" & Chr(34) & ">" & vbCrLf
sSpecialFriends = sSpecialFriends & "<p>" & vbCrLf
Do
if rsSpecial("sURL") <> "" Then
sSpecialFriends = sSpecialFriends & "<a href='"& Replace(rsSpecial("sURL"), "&", "&") & "' target=" & Chr(34) & "_blank" & Chr(34) & ">"
sSpecialFriends = sSpecialFriends & "" & Replace(rsSpecial("sName"), "&", "&") & ""
sSpecialFriends = sSpecialFriends & "</a>"
sSpecialFriends = sSpecialFriends & "<br>" & vbCrLF
'Otherwise just list their name
else
sSpecialFriends = sSpecialFriends & "" & Replace(rsSpecial("sName"), "&", "&") & ""
sSpecialFriends = sSpecialFriends & "<br>" & vbCrLF
end if
i=i+1
rsSpecial.MoveNext
Loop Until i=100 or rsSpecial.EOF
sSpecialFriends = sSpecialFriends & "</td>" & vbCrLf
Loop Until rsSpecial.EOF
sSpecialFriends = sSpecialFriends & "</table>" & vbCrLf
end if
rsSpecial.Close
Set rsSpecial=Nothing
我似乎找不到如何在PHP中重新创建嵌套循环的好例子,我可以移动到下一条记录。这就是我到目前为止 - 它显示每条记录,100次,100列宽。 我想我需要找到一个地方插入PHP等效的MoveNext。
$result = mysql_query ("SELECT sName, sURL FROM gktw_spcFriends ORDER BY sName ASC", $con) or die(mysql_error());
$sSpecialFriends = "";
if (mysql_num_rows($result))
$row = mysql_fetch_array($result);
{
$sSpecialFriends = $sSpecialFriends."<table width=".Chr(34)."98%".Chr(34).">" ."\n";
do
{
$i=0;
$sSpecialFriends = $sSpecialFriends."<td valign=".Chr(34)."top".Chr(34).">" ."\n";
$sSpecialFriends = $sSpecialFriends."<p>" ."\n";
do
{
//Check for URL
if ($row['sURL'] != "")
{
$sSpecialFriends = $sSpecialFriends . "<a href='". str_replace("&", "&", $row['sURL']) . "' target=" . Chr(34) . "_blank" . Chr(34) . ">"."\n";
$sSpecialFriends = $sSpecialFriends . "" . str_replace("&", "&", $row['sName']) . "" ."\n";
$sSpecialFriends = $sSpecialFriends . "</a>" ."\n";
$sSpecialFriends = $sSpecialFriends . "<br>" ."\n";
}
//Otherwise just list their name
else
{
$sSpecialFriends = $sSpecialFriends . "" . str_replace("&", "&", $row['sName']) . "" ."\n";
$sSpecialFriends = $sSpecialFriends . "<br>" ."\n";
}
$i=$i+1;
}
while ($i<100);
$sSpecialFriends = $sSpecialFriends . "</td>" ."\n";
}
while($row = mysql_fetch_array($result));
$sSpecialFriends = $sSpecialFriends . "</table>" ."\n";
}
mysql_close($con);
所以,如果有人能帮助我使用PHP等同于ASP的MoveNext和/或实现我想要完成的目标的最佳方式。
感谢您的帮助。
答案 0 :(得分:1)
欢迎来到StackOverflow,Matt。除非您使用的是ADODB库,否则PHP实际上并不是那样的。以下PHP脚本循环遍历MOVENEXT之类的记录:
// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM example")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>Age</th> </tr>";
// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array($result)) {
echo "<tr><td>";
echo $row['name'];
echo "</td><td>";
echo $row['age'];
echo "</td></tr>";
}
echo "</table>";
mysql_close($dbConn);
但是,如果您使用的是ADODB库,仍然可以像这样使用MOVENEXT和EOF:
$rs = $db->Execute($sql);
if ($rs)
while (!$rs->EOF) {
ProcessArray($rs->fields);
$rs->MoveNext();
}
我建议使用顶级版本,更简单。