Displaying a single element from an array

Hi everyone. Noob here.

I have a site I’m working on. There will be a news section. The idea is, the client will have access to a text file, and can edit it however they see fit. So I created a text file. For the sake of humor, it’s called balls.txt. For testing purposes, it looks like:

TEST

body

TEST

body

TEST

body

with the “TEST” lines being the editable news entries for the client.

My code in my html looks like this:

<?php
   $file = fopen("balls.txt","r");
   $balls = fread($file,filesize("balls.txt"));
   fclose($file);
            
   $ary = explode("<!--break-->",$balls);
            
   foreach ($ary as $value) {
      ?>
      <div class="text"><? echo $value; ?></div>
      <?  
   }            
?> 

and it obviously displays:

TEST
body
TEST
body
TEST
body

Now, I want to differentiate between TEST and body text, and make TEST a header or something, like

in the html. How can I go about singling out a single element in the array? I was reading, and it seems the file() function could help with this, but I read that breaks up a text file into an array, but line by line. What if I have the body text being several paragraphs long?

I’d really appreciate any help. Thanks everyone. :)

Your current script looks like a step in the right direction as opposed to using file(). The change is in the foreach() loop. Make it for:

for ($i = 0; $i < count($ary); $i += 2) { echo "<div class='head'>".$ary[$i]."</div>"; echo "<div class='text'>".$ary[$i + 1]."</div>"; }

Something like that is probably what you’re looking for.

Excellent, that works! Thanks Zyppora!
The line=spacing is spread out a ton, however. Wonder why it did that.

Sponsor our Newsletter | Privacy Policy | Terms of Service