2015-10-07 30 views
1

這是我的輸出到我的分號的文本文件數據庫PHP我while語句是關閉的增量不工作

MSG2;This is the first test;William; 
MSG2;This is the second test;William; 
MSG2;This is the third test;William; 
MSG1;This is the third test;William; 

我正在拍攝的是這個...

MSG 1;This is the first test;William; 
MSG 2;This is the second test;William; 
MSG 3;This is the third test;William; 
MSG 4;This is the third test;William; 

這裏我的變量

<?php 
$x = 1; 

while($x < 1) { 
    $x++; 
} 
if(isset($_POST['field1']) && isset($_POST['field2'])) { 
$data = 'MSG' . $x . ';' . $_POST['field1'] . ';' . $_POST['field2'] . ';' ."\n"; 
$ret = file_put_contents('data.txt', $data, FILE_APPEND | LOCK_EX); 
if($ret === false) { 
    die('There was an error writing this file'); 
} 
else { 
    echo "$ret bytes written to file"; 
} 
} 
else { 
die('no post data to process'); 
}   
?> 

我想了解num ++的概念。我該如何解決我的問題?謝謝。

+0

這就是所謂的增量運營商,它提出了使用它的電流後的$一個* NUM值值* – e4c5

+0

'$ x = 1; while($ x <1){ $ x ++; }由於'$ x'開始時爲'1',並且您的循環在'$ x <1'時運行,所以永遠不會運行 –

+0

您可能會發現'for'循環在這裏更有用:http:// php.net/manual/en/control-structures.for.php – Eraph

回答

1

它看起來像你的循環是關閉

$x = 1; 

while($x < 1) { 
    $x++; 
} 

所以,第一件事情就是你的while僅環做一件事情。您的代碼的其餘部分是以外的此循環。所以這個循環對你的代碼的其餘部分是沒有意義的。

其次,你設置$x等於1,然後說如果$x小於1,加1到$x,直到它等於1 ...它已經是。

你應該做的是這樣的(僞代碼,因爲你有什麼可遍歷)

$x = 1; 
while(some actual condition to loop over) { 
    $data = 'MSG' . $x . ';'; 
    $x++; 
} 
+0

感謝Machavity,這完全解釋了它是如何工作的...感謝一百萬。 – Malanno