2012-08-02 34 views
3

我正在测试网站上呈现类似于搜索信息的房地产。除了房屋的图像外,还有通过目录访问引入的描述。除了“Description:”行外,我的所有行都能正常工作。在文本文件中,每个类别按行分隔,除了“说明:”类别在标题行之后运行并且在文件结尾处分为多行。我正在使用连接的fgets来获取信息,但在结果中,输出结果正确地显示了完整的房屋信息,包括“描述:”行,但在每个以下结果中房屋信息重复先前打印的内容,然后下一个文件。如果我摆脱了串联的,只是做了常规:反复出现的线路输出PHP - 连接的fgets

$pos = stripos($line, 'Description:'); 

if($pos !== false) 
{ 
$description = substr($line, 12); 
$description = trim($description); 
} 

然后我结束了只有“描述:”打印而不描述部分的文本。

这是导入的文本文件的例子:

市:OceanCove
价格:$ 950,000
卧室:5个
浴场:3
影片:3000平方英尺
房地产经纪人:雪莉Urkiddeng
抓取器:梦幻般的家园与神奇的景色!
描述:
你永远不会厌倦从你的客厅沙发上观看日落 或从后门廊看日出 ,俯瞰 华丽的珊瑚峡谷。一生只有一次 的机会!

这里是我的代码(以下简称“说明”一节中有一些注释掉了,我没有其他试验:

 <?php 
$findhome = $_POST['findhome']; 

$header = getHeader($findhome); 
print $header; 

getResults($findhome); 

    function getHeader($findhome) 
    { 
if (empty($findhome)) 
    { 
     $header = "<h3>Current Listings: <br />"; 
    } 
    else 
    { 
     $header = "<h3>Current Listings that match: $findhome </h3><br />"; 
    } 

    return $header; 

    } 

    function getResults($findhome) 
    { 


if (empty($findhome)) 
{ 
    $findhome ='ALL'; 
} 

$dirname = 'images'; 

$dirhandle = opendir($dirname); 

if ($dirhandle) 
{ 
    $houseimagesarray = array(); 

    while (false !== ($file = readdir($dirhandle))) 
    { 
     if ($file !='.' && $file !='..') 
     { 
      $first_6 = substr($file,0,6); 

      if($first_6 =='house_') 
      { 
       array_push($houseimagesarray, $file); 
      } 
     } 
    } 
} 

sort($houseimagesarray); 

    $description = ''; 

foreach ($houseimagesarray as $image_filename) 
{ 


//*************************************************** 
//** Function Definitions 
//*************************************************** 

    //Get Image Files 

      $imagename ='images/'.$image_filename; //.jpg files 
      $house_img = "<p><img src='".$imagename."'></p>"; 

      $houseinfo_filename = str_replace ('.jpg', '.txt',   $image_filename); 

      $filename = 'data/'.$houseinfo_filename; //.txt file 

      $fp = fopen($filename, 'r'); 


    //Get Image House Information 


    $show_house = 'Y'; //Set default value 

    while(true) 
    { 
     $line = fgets($fp); 

     if (feof($fp)) 
     { 
      break; 
     } 

     $pos = stripos($line, 'City:'); 

     if ($pos !== false) 
     { 
      $city = substr($line, 5); 
      $city = trim($city); 

      if ($findhome != 'ALL') 
      { 
       $subpos = stripos($city, $findhome); 

       if($subpos === false) 
       { 
        $show_house = 'N'; 
        break; 
       } 
      } 
     } 

     $pos = stripos($line, 'Price:'); 

     if ($pos !==false) 
     { 
      $price = substr($line, 6); 
      $price = trim($price); 
     } 

     $pos = stripos($line, 'Bedrooms:'); 

     if ($pos !== false) 
     { 
      $bedrooms = substr($line, 9); 
      $bedrooms = trim($bedrooms); 
     } 

     $pos = stripos($line, 'Baths:'); 

     if ($pos !== false) 
     { 
      $baths = substr($line, 6); 
      $baths = trim($baths); 
     } 

     $pos = stripos($line, 'Footage:'); 

     if($pos !== false) 
     { 
      $footage = substr($line, 8); 
      $footage = trim($footage); 
     } 

     $pos = stripos($line, 'Realtor:'); 

     if($pos !== false) 
     { 
      $realtor = substr($line, 8); 
      $realtor = trim($realtor); 
     } 

     $pos = stripos($line, 'Grabber:'); 

     if($pos !== false) 
     { 
      $grabber = substr($line, 8); 
      $grabber = trim($grabber); 
     } 

     $pos = stripos($line, 'Description:'); 

     if($pos !== false) 
     { 
      $descriptionFlag = "Y"; 
     } 
     if($descriptionFlag=='Y') 
     { 
      $description .=$line."<br />\n"; 
      //$description =$line."<br />"; 
      //$description = $description.$line."<br />"; 
     } 
    } 

    if ($show_house == 'Y') 
    { 
     print $house_img; 

     print "<h3>".$grabber."</h3><br />"; 
     print "City: ".$city."<br />"; 
     print "Bedrooms: ".$bedrooms."<br />"; 
     print "Baths: ".$baths."<br />"; 
     print "Price: ".$price."<br />"; 
     print "Footage: ".$footage."<br />"; 
     print "Realtor: ".$realtor."<br />"; 

     print $description; 

     } 


     } 
    } 
    ?> 

新代码:

$pos = stripos($line, 'Description:'); 

     if($pos !== false) 
     { 
      $descriptionFlag = "Y"; 
     } 
     if($descriptionFlag=='Y') 
     { 
      if(!feof($fp)) 
      { 
       $description .=$line."<br />\n"; 

       if(feof($fp)) 
       { 
        break; 
       } 

      } 
     } 

回答

1

由于Description可能包含不止一个如果你在同一个文件中有多个项目,你可能需要搜索下一个City以便纾困。例如:

if($descriptionFlag=='Y') { 
    while ($line = fgets($handle, 8192)) { 
     $line = trim($line); 
     if($line === 'City') 
     break; 
     $description = $description . " " . $line; 
    } 
    $description = $description . "<br />\n"; 
} 
+0

没得到那个工作。我想知道是否有办法按照我拥有的方式工作,并让它识别feof?我试着在新代码部分添加了上面的内容。 – Brad 2012-08-02 20:14:48

+0

您添加的代码检查feof是否正常,但它不会循环描述内容。尝试使用我的代码,只改变行:'if($ line ==='City')':if(feof($ fp))' – alfasin 2012-08-02 21:32:25

+0

试试看,“8192”代表什么意思? – Brad 2012-08-02 23:44:09

1

尝试

if($descriptionFlag=='Y') { 
    while ($line = fgets($handle, 8192)) { 
     $line = trim($line); 
     if(substr($line,0,4) === 'City') 
     break; 
     $description = $description . " " . $line; 
    } 
    $description = $description . "<br />\n"; 
}