我只使用了大约一个星期的PHP (尽管我已经使用ASP 12年了,我绝不是一个专家),所以请容忍我……我正在尝试将我的经典ASP网站转换为PHP。到目前为止,我已经能够找到我的大部分问题的答案,但我被困在这个问题。
我正在寻找一个与我在ASP.NET中使用的.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列宽。--我想我需要找个地方插入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);因此,如果有人可以帮助我使用相当于ASP的MoveNext的PHP和/或完成我正在努力完成的任务的最佳方法。
谢谢你的帮助。
发布于 2012-07-30 16:47:00
欢迎来到StackOverflow,马特。除非您正在使用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(); 
    }我建议使用顶部版本,要简单得多。
https://stackoverflow.com/questions/11703047
复制相似问题