Question

Hi I have the following code

function dropDownMenuMobile($parentId,$siteId,$root=false)
{
    $menu = "";
    $sql = "EXEC spGenerateDropdownMenuMobile ".$parentId.",".$siteId;
    $stmt = sqlsrv_query($GLOBALS['conn'],$sql);
    sqlsrv_fetch($stmt);
    if($stmt && sqlsrv_has_rows($stmt) === true)
    {
        $menu .= "<ul>";
            while($rs = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC))
            {
                $urlLink = str_replace("<br />"," ",$rs[2]);
                $urlLink = replaceIllegalChars($urlLink);
                $actualLink = (strlen($rs[7]) > 2) ? $rs[7] : $rs[0]."/".makeSEurl($urlLink);
                $liclass = ($rs["5"] > 0)?"expand":"";
                $menu .= "<li class=\"".$liclass."\">";
                    $menu .= "<a href=\"/".$actualLink."\">".$rs[2]."</a>";
                    $menu .= ($rs[5] > 0) ? dropDownMenuMobile($rs[0],$siteId,"") : "";
                $menu .= "</li>";
            }
        $menu .= "</ul>";
    }
    return $menu;
}

it runs in numerous other places, but for some reason in the while loop it seems to be missing the first result every time its called.

Was it helpful?

Solution

I think it is because you run

sqlsrv_fetch($stmt);

Which fetches first result and after you run

sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC)

which continues fetching second result.

Just remove first fetch.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top