2

嗨,我正在尝试找出我的 while 语句运行了多少次,但我不知道如何执行此操作,我需要知道这一点的原因是查看已将多少行输出到我的屏幕,然后只显示一条消息找到号码后显示在屏幕上。我的代码很长。抱歉英语不好提前谢谢。

<?php
            try {
                    $serverName = "127.0.0.0.0";
                    $connectionInfo = array( "Database"=>"database", "UID"=>"uid", "PWD"=>"pwd");
                    $conn = sqlsrv_connect( $serverName, $connectionInfo );
                    if( $conn === false ) 
                    {
                        die( print_r( sqlsrv_errors(), true));
                    }
                    $sql = "SELECT TOP 10 [company]
                            ,[partnum]
                            ,[description]
                            FROM [database].[uid].[table]
                            WHERE Part.partnum LIKE ? or Part.description LIKE ?";          
                    /* Set parameter values. */
                    $params = array(  "%" . str_replace(" ","%",$_POST["part"] ). "%", "%" . str_replace(" ","%",$_POST["part"] ) . "%");

                    $x = true;
                    /*echo print_r($params, true);*/
                    $stmt = sqlsrv_query( $conn, $sql, $params );

                    if( $stmt === false) 
                    {
                        die( print_r( sqlsrv_errors(), true) );
                    }

                    while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) 
                    {

                        if($x == true)
                        {               
                        echo"<form action=\"locations.php\" method=\"post\">";
                            echo"<input type=\"hidden\" name=\"part\" id=\"3\" value=\"".$row['partnum']."\">";
                            echo"<input type=\"hidden\"  name=\"lon1\" id=\"1\" value=\"".$_POST["lon1"]."\">";
                            echo"<input  type=\"hidden\" name=\"lat1\" id=\"2\" value=\"".$_POST["lat1"]."\">";
                        echo"<button type=\"submit\">";
                        echo "<div style=\"font-family:verdana;font-weight:bold;color:#3083FF;font-size:20px;width:100%;text-align:center;margin:0; padding:0;\">";
                                echo $row['partnum']."<br/>";
                        echo "</div>";  
                                echo"<img style=\"width:50%;\"; src=\"productimages/".$row['partnum'].".jpg\" alt=\"Save icon\" onError=\"this.src='productimages/noimage.jpg'\"/>";
                        echo "<div style=\"font-family:verdana;color:#3083FF;font-size:20px;width:100%;text-align:center;margin:0; padding:0;\">";
                                echo $row['description'];
                        echo "</div>";
                        echo"</button>";
                        echo"</form>";

                            }   

                    }

                    sqlsrv_free_stmt( $stmt);
                }   catch (Exception $e) 
                {
                    echo 'Caught exception: ',  $e->getMessage(), "\n";
                }   
            ?>  
4

6 回答 6

2
$i = 0;

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) 
{

    if($x == true)
    {               
    echo"<form action=\"locations.php\" method=\"post\">";
        echo"<input type=\"hidden\" name=\"part\" id=\"3\" value=\"".$row['partnum']."\">";
        echo"<input type=\"hidden\"  name=\"lon1\" id=\"1\" value=\"".$_POST["lon1"]."\">";
        echo"<input  type=\"hidden\" name=\"lat1\" id=\"2\" value=\"".$_POST["lat1"]."\">";
    echo"<button type=\"submit\">";
    echo "<div style=\"font-family:verdana;font-weight:bold;color:#3083FF;font-size:20px;width:100%;text-align:center;margin:0; padding:0;\">";
            echo $row['partnum']."<br/>";
    echo "</div>";  
            echo"<img style=\"width:50%;\"; src=\"productimages/".$row['partnum'].".jpg\" alt=\"Save icon\" onError=\"this.src='productimages/noimage.jpg'\"/>";
    echo "<div style=\"font-family:verdana;color:#3083FF;font-size:20px;width:100%;text-align:center;margin:0; padding:0;\">";
            echo $row['description'];
    echo "</div>";
    echo"</button>";
    echo"</form>";

        }   
    $i++;
}

echo $i;
于 2013-10-10T13:20:57.240 回答
1

You can put a variable in while. It increments its value for every loop (you can use instead of while statement the for statement, so you've got free a counter variable).

If you need to know every result of iteration, please put at the end of the loop an output command to show on your screen the result you want.

I hope I accomplish your task. Have a nice day ;)

于 2013-10-10T13:11:29.743 回答
0
$x = 0;

while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{
  $x++; //$x is now how many times this loop has run.
        //eg when it's set to 2 this is the 2nd time
        //the while loop has ran

  // Rest of your code here
}

这就是你需要的。$x将在每个循环上加一

于 2013-10-10T13:25:25.247 回答
0

只需在 while 循环 like($inc=0;) 之前声明一个变量,并在 while 循环 like($inc++;) 内递增它。现在您可以使用此变量来获取 while 循环运行的计数。

于 2013-10-10T13:16:42.580 回答
0

用这个:

$a=0

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) 
{
      $a=$a+1;
}
echo "you have {$a} records"
于 2013-10-10T13:17:15.193 回答
0
                $whileCounter = 1;

                while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) 
                {

                    if($x == true)
                    {               
                    echo"<form action=\"locations.php\" method=\"post\">";
                        echo"<input type=\"hidden\" name=\"part\" id=\"3\" value=\"".$row['partnum']."\">";
                        echo"<input type=\"hidden\"  name=\"lon1\" id=\"1\" value=\"".$_POST["lon1"]."\">";
                        echo"<input  type=\"hidden\" name=\"lat1\" id=\"2\" value=\"".$_POST["lat1"]."\">";
                    echo"<button type=\"submit\">";
                    echo "<div style=\"font-family:verdana;font-weight:bold;color:#3083FF;font-size:20px;width:100%;text-align:center;margin:0; padding:0;\">";
                            echo $row['partnum']."<br/>";
                    echo "</div>";  
                            echo"<img style=\"width:50%;\"; src=\"productimages/".$row['partnum'].".jpg\" alt=\"Save icon\" onError=\"this.src='productimages/noimage.jpg'\"/>";
                    echo "<div style=\"font-family:verdana;color:#3083FF;font-size:20px;width:100%;text-align:center;margin:0; padding:0;\">";
                            echo $row['description'];
                    echo "</div>";
                    echo"</button>";
                    echo"</form>";
                    $whileCounter++;
                    echo $whileCounter;
                        }   
                }

如果您希望计数器从停止的地方开始以查看几个 while 循环后的总计数,只需输入:

$whileCounter = 1;

作为顶部的声明,而不是在每次调用的代码中。

于 2013-10-10T13:18:20.727 回答