I’m working on a test site that presents real estate like information on a search. Alongside images of the houses there are descriptions brought in through directory access. I have all the lines working correctly, except for the “Description:” line. In the text files each category is separated by line, except the “Description:” category runs after the title line and into multiple lines up to the end of the file. I am using a concatenated fgets to get the info, but in my results the output is showing the complete house information correctly including the “Description:” line, but on each following result the house information repeats what it previously printed and then the rest of the next file. If I get rid of the concatenation and just do a regular:
$pos = stripos($line, 'Description:');
if($pos !== false)
{
$description = substr($line, 12);
$description = trim($description);
}
Then I end up with only the “Description:” printing without the text of the description section.
This is an example of the imported text file:
City: OceanCove
Price: $950,000
Bedrooms: 5
Baths: 3
Footage: 3000 sq. ft.
Realtor: Shirley Urkiddeng
Grabber: Fantastic Home with a Fantastic View!
Description:
You will never get tired of watching the sunset
from your living room sofa or the sunrise
from your back porch with a view overlooking
the gorgeous coral canyon. Once in a lifetime
opportunity!
Here is my code (in the “Description” section there is some commented out other trials that I did:
<?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;
}
}
}
?>
New Code:
$pos = stripos($line, 'Description:');
if($pos !== false)
{
$descriptionFlag = "Y";
}
if($descriptionFlag=='Y')
{
if(!feof($fp))
{
$description .=$line."<br />\n";
if(feof($fp))
{
break;
}
}
}
Since
Descriptionmay contain more than one line, you have to loop over the lines until you’re done with theDescriptionfield. If you have multiple items in the same file you’ll probably want to search for the nextCityin order to bail-out. Something like: