2013-10-01 60 views
0

我想顯示文件目錄的結果。它確實顯示了結果,但是,這一切都在我不打算髮生的一行中。我希望它在特定部分顯示結果。不能正確顯示文件目錄中的結果

我得到這個錯誤例如

Position ID: P0007 IT CEO Hello 01/10/13 full time fixed term Post --- 


Notice: Undefined offset: 1 in /home/students/accounts//hit3323/www/htdocs/jobassign1b/searchjobprocess.php on line 56 
Title: 



Notice: Undefined offset: 2 in /home/students/accounts//hit3323/www/htdocs/jobassign1b/searchjobprocess.php on line 59 
Description: 


Notice: Undefined offset: 3 in /home/students/accounts//hit3323/www/htdocs/jobassign1b/searchjobprocess.php on line 62 

我想例如

位置ID:P0007 標題:IT CEO 描述:你好

這是PHP代碼,有什麼建議?謝謝你提前

<?php 

if(!empty($_GET["jobtitle"])) 
{ 
umask(0007);          
$directory = "../../data/jobposts";    // path to directory 
if(!is_dir($directory)) 
{ 
    mkdir($directory, 02770); 
} 

$data = $directory. "/jobs.txt";    // checking file in the directory 
$opening = fopen($data, "r");     // opening the file in reading mode 

$dirFile = file_get_contents($data); 
if(strpos($dirFile, $_GET['jobtitle']) === false) // checking the user file exist or not 
    { 

    echo "<p>Job Title does not exist in the file</p>"; 
    echo '<a href="searchstatusform.php">Search another Job Title</a><br />'; 
    echo '<a href="index.php">Return to homepage</a>'; 

    } 

    else 

    { 

    $array = file($data); 
    $jobString = $_GET['jobtitle']; 
     foreach($array as $key => $value) 
     { 
     $get = strpos($value, $jobString); // getting the key value of the array containing status record    
     if ($get !== false) 
      { 
       $file2 = $key; 
       $array2 = $array[$file2]; 
       $newArray = explode("\t", $array2); //using explode and \t to create a new line 
       $i = 0; 
       echo "<h1> Job Vacancy Information</h1>"; 
       echo "<p>Position ID: $newArray[$i]</p>";  //displaying the results 
       $i++; 

       echo "<p>Title: $newArray[$i]</p><br />"; 
       $i++; 

       echo "<p>Description: $newArray[$i]</p>"; 
       $i++; 

       echo "<p>Closing Date: $newArray[$i]</p>"; 
       $i++; 

       echo "<p>Position: $newArray[$i]</p>"; 
       $i++; 

       echo "<p>Application by: $newArray[$i]</p>"; 
       $i++; 

       echo "<p>Location: $newArray[$i]</p>"; 
       $i++; 

       for($i;$i<(count($newArray)-1);$i++) 
       { 
       echo"<p id=p>$newArray[$i]</p>"; 
       } 
       echo "</ul>"; 
       echo '<a href="searchjobform.php">Search again a new status</a>'; 
       echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; 
       echo '<a href="index.php">Return to homepage</a></p>'; 
       } 
      } 
     } 
    } 
    else 
    { 
     echo "<p>Enter a valid job to search</p>"; 
    } 
?> 
+0

聲明通常是你的代碼做一些你沒有考慮一個標誌。開始查看生成通知的行通常是一個好主意,如果看起來正確 - 查看使用變量的代碼(最近的任務通常是您要查找的內容)。如果這看起來還可以,那麼print_r/var_dump的變量就像Greg在下面提出的那樣。 – illuzive

+0

[參考 - 這個錯誤在PHP中意味着什麼?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – mleko

回答

1

它的說,$ i指向的元素不存在。

你用php學得很快的一件事是在嬰兒步驟中編寫代碼,所以你需要確保的第一件事是$ newArray包含你認爲它的作用。

嘗試輸出$ newArray到屏幕上以查看數組中的實際內容,聽起來像是空的。您分配$ newArray後立刻把這個行:

print_r($newArray); 

而且,不知道爲什麼你用$打擾我++時,你可以用你希望出現,比如元素的數量$代替我。 ...

echo "<p>Position ID: $newArray[0]</p>";  //displaying the results 
echo "<p>Title: $newArray[1]</p><br />"; 
echo "<p>Description: $newArray[2]</p>"; 
echo "<p>Closing Date: $newArray[3]</p>"; 

發佈$ newArray的輸出,然後我們可以計算出接下來的代碼。

歡呼

格雷格Ĵ