2012-10-15 99 views
0

我想要一個由php循環生成的ID列,我嘗試了下面的代碼。使用PHP循環創建ID列

此代碼正在工作,但它跳過第一行。對於例如如果有153行,則只顯示152行,因爲它跳過第一行並從第二行開始編號。

$i = 0; 
$result = mssql_query ($sql); 
$cell = mssql_fetch_array($result); 
while ($i <= $cell & $cell = mssql_fetch_array($result)) 

{ 
$i = $i + 1; 
echo "<tr><td>".$i."</td>"; 
echo "<td>".$cell[0]."</td>"; 
echo "<td>".$cell[1]."</td>"; 
echo "<td>".$cell[2]."</td>"; 
echo "<td>".$cell[3]."</td>"; 
echo "<td>".$cell[4]."</td>"; 
echo "<td>".$cell[5]."</td>"; 
echo "</tr>"; 

} 
+0

'$ i <= $ cell'?在循環的第一個「循環」中,「$ cell」甚至沒有被定義。如果這還不夠,'$ cell'是一個數組,'$ i'是一個整數,你爲什麼要比較它們? –

+0

嘗試刪除此行'$ record = mssql_fetch_array($ result);' –

+0

對不起編輯它應該是$ cell – user1449596

回答

4
$i = 0; 
$result = mssql_query ($sql); 
//$record = mssql_fetch_array($result); - you don't need this 
while ($cell = mssql_fetch_array($result)) //you dont need $i <= $cell 

{ 
$i++; //$i = $i + 1; 
echo "<tr><td>".$i."</td>"; 
echo "<td>".$cell[0]."</td>"; 
echo "<td>".$cell[1]."</td>"; 
echo "<td>".$cell[2]."</td>"; 
echo "<td>".$cell[3]."</td>"; 
echo "<td>".$cell[4]."</td>"; 
echo "<td>".$cell[5]."</td>"; 
echo "</tr>"; 

} 
+2

這甚至是回聲「​​」。++ $ i。「」; – Hast

+0

@Ofir Baruch謝謝你的代碼工作。我馬上會接受它.. – user1449596

1

我猜你可能想的& &,而不是在這裏&:

while ($i <= $cell & $cell = mssql_fetch_array($result)) 
5
$cell = mssql_fetch_array($result); 
     ^^^^^^^^^^^^^^^^^ 
    fetches the first record 

while ($i <= $cell & $cell = mssql_fetch_array($result)) 
          ^^^^^^^^^^^^^^^^^ 
         fetches the second record 

你永遠輸出您要提取的第一個記錄。
不知道你想用$i這個條件來做什麼。