Forum Moderators: coopster

Message Too Old, No Replies

Only displaying "section" when there are elements inside.

Using arrays from text document

         

tokey666

8:29 pm on Aug 23, 2010 (gmt 0)

10+ Year Member



Hey all, this is my first real PHP script! I am proud of myself to this point. haha. I am reading in from a text file, creating an array out of the data to display on the appropriate pages.

The data looks like this:

This is a sentence. -- PERSON'S NAME -- RESORT NAME
This is a sentence 2. -- PERSON'S NAME 2 -- RESORT NAME 2


Here is my code:

$resortname = "Chunky Monkey";

$f_contents = file_get_contents("testimonials.txt"); //get the entire file
$line = explode("\n",$f_contents); //explode (create an array) seperated by new line elements
$ntotalresorts = count($line); //gives me a TOTAL count.. maybe for later use
shuffle($line); //suffles up the array to randomize the contents
$neachresort = 0;
$firstSection = true;
foreach ($line as $str ){ //go through each array
$rawtestimonial = explode("--",$str); //chop up each line of the file, exploding at -- marks
$arrTestimonial = array_map('trim', $rawtestimonial); //TRIM IT, get rid of white space before and after each element
unset($str);

if ($arrTestimonial[2] == $resortname) { //Display only testimonials from a certain resort name
if ($firstSection) $firstSection = true; else echo '<br/>';
echo '<div class="rateyearheader">' . Giggity . '</div><br/>';

echo "<div class=\"bubble\">\n<div class=\"rounded\">\n<blockquote><p>" . $arrTestimonial[0] ."</p></blockquote></div> \n";
echo "<cite class=\"rounded\"><div><strong>" . $arrTestimonial[1] . "</strong> staying at " . $arrTestimonial[2] . "</div></cite>\n</div> \n\n";

echo $neachresort;
}
}



Basically, you can see where I am trying to display a "Title" called Giggity, ONLY ONCE before the results display. However, it doesn't seem to be working. Basically, I want this script to display testimonials and a title ONLY if that certain resort HAS testimonials, so the page will not display the title of "Giggity" and nothing is below it.

Oh and feel free to tell me know to OPTIMIZE my code. haha. I know it must be rough. haha.

Thanks for the help everyone!

tokey666

1:04 am on Aug 24, 2010 (gmt 0)

10+ Year Member



Welp! nevermind! This code did the trick instead of what I had.

$neachresort = $neachresort + 1;
if ($neachresort == 1) {
echo "ONLY ONE";
}