homepage Welcome to WebmasterWorld Guest from 54.242.241.20
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
(help) Show content only if.
Show content if number of entries is more than x-number
fredfletcher



 
Msg#: 4457540 posted 8:16 pm on May 24, 2012 (gmt 0)

Hello, I have a question if someone can help, I would appreciate it very much.

I would like to show content or echo a line of text, but only if the amount of entries equal 5 or more, for example in my data file.

Example data file content which is semi-colon seperated, if it makes a difference:

entry1;
entry2;
entry3;
entry4;


Code I came up with, but I know it won't work...

$file = "./data_file.txt";

if
{
$lines = count(file($file));
}

echo "Sorry, there are only $lines entries. 5 entries are needed to show the content below.";

else
{

echo "Hurray, there is $lines entries. Content is available to view.";

}

Something to that affect. Either with a "less than" or "not equal" statement.

Thank you.

 

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4457540 posted 8:50 pm on May 24, 2012 (gmt 0)

You know it won't? It should. :-) You probably have a path problem. This works if it's in the same directory.

<?php
$count = count(file('fivelines.txt'));
echo "Line count: $count";
?>

But what you should do is 1) use the full server path to the file, especially if it exists in some other directory, and 2) check for it's existence.


<?php
$myfile = $_SERVER['DOCUMENT_ROOT'] . '/some-directory/fivelines.txt';
if (! is_file($myfile)) { echo "$myfile doesn't exist"; }
else {
$count = count(file($myfile));
echo "Line count: $count";
}
?>


So if you want a conditional,


<?php
$myfile = $_SERVER['DOCUMENT_ROOT'] . '/some-directory/fivelines.txt';
if (! is_file($myfile)) { echo "$myfile doesn't exist"; }
else {
$count = count(file($myfile));
if ($count >= 5) { echo "Line count: $count"; }
else { echo "Didn't make the grade. Less than 5."; }
}
?>

fredfletcher



 
Msg#: 4457540 posted 9:11 pm on May 24, 2012 (gmt 0)

Thanks Rocknbil, your code(s) worked really well! I wasn't far off. Cheers, much appreciated! :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved