3

私は検索に関する情報のような不動産を提示するテストサイトに取り組んでいます。家の画像と並んで、ディレクトリアクセスを通じて持ち込まれた説明があります。「Description:」行を除いて、すべての行が正しく機能しています。テキストファイルでは、「Description:」カテゴリがタイトル行の後にあり、ファイルの最後まで複数の行になっていることを除いて、各カテゴリは行で区切られています。連結されたfgetsを使用して情報を取得していますが、私の結果では、出力には「Description:」行を含む完全な家の情報が正しく表示されていますが、次の各結果で、家の情報は以前に印刷されたものを繰り返し、その後、次のファイル。連結を取り除き、通常の操作を行う場合:

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

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

そうすると、説明セクションのテキストなしで「Description:」だけが印刷されてしまいます。

これは、インポートされたテキストファイルの例です。

市:OceanCove
価格:$ 950,000
寝室:5
バス:3
映像:3000
sq。ft。不動産業者:Shirley 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;
                }

            }
        }
4

2 に答える 2

1

Description複数の行が含まれている可能性があるため、フィールドが完了するまで行をループする必要がありますDescription。同じファイルに複数のアイテムがある場合は、City救済するために次のアイテムを検索することをお勧めします。何かのようなもの:

if($descriptionFlag=='Y') {
  while ($line = fgets($handle, 8192)) {
      $line = trim($line);
      if($line === 'City')
        break;
      $description = $description . " " . $line;
  }
  $description = $description . "<br />\n";
}
于 2012-08-02T19:30:19.597 に答える
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";
}
于 2012-08-02T20:30:34.227 に答える