Jump to content

help with if statements


chwebdesigns

Recommended Posts

Hi,

I think I need an if statement for this, but i have no idea on how to write it. I have a news file that adds into a *.txt file. (with some other PHP) and I have broken the code down like this:

 $lines = file("newsadd.txt");
$lines = array_reverse($lines);
$pieces = explode("|", $lines); 

I then want it to look if $pieces[5] is equal to "other" then to show all of the ones which have pieces 5 as "other" (this is the category)

I hope you can understand this,

From Cal

PS. I am a PHP NOVICE

Link to comment
https://forums.phpfreaks.com/topic/71562-help-with-if-statements/
Share on other sites

php.net does not have a part of if statements so here is the break down

 

you write

if(Statements) {

//Do this

}

 

in the () of the if you provide statemetns, arguements etc like if 1=1 or if $a = $b or $b != 27 and you can do muliple using the operators && || to say and this or that.  so you could say

<?php
if($line[5] == $line[4]){
//We have a match
}

how would you only echo the ones that the if statement has passed for example:

<?php if($pieces[5] == "other"){ echo **********}

Because there are other categories too for example important, events, results ect.... and on this code I only want the OTHER news items to appear. I hope this makes sense

 

TAR

From Callum

What I have is a news page. I add the news to this page using a form, and it saves it into a .txt file. On each line there is the next piece of news, and each line contains the following information ($pieces):

Name of Person Who Posted It, Date of Post, Title of Post, News Article, Category.

This is how I work out the $pieces

<?php $lines = file("newsadd.txt");
$lines = array_reverse($lines);
$pieces = explode("|", $lines);?>

 

In this code $pieces[5] = Category. For example one category is "other", what I want is the PHP code to look and find all of the pieces of news in the "other" category and echo them.

Cheers

i think you want something like this

 

<?php
$lines = file("newsadd.txt");
$lines = array_reverse($lines);
foreach ($lines as $line)
{
$pieces = explode("|", $line);
if($pieces[5] == "other")
{
	echo htmlspecialchars($line)."<br />\n";
//}else{
//	echo "";
}
}
?>

In this code $pieces[5] = Category. For example one category is "other", what I want is the PHP code to look and find all of the pieces of news in the "other" category and echo them.

 

<?php
$data_loc = 5; // The location of the data on each line, for instance 5

$lines = file("newsadd.txt");
$lines = array_reverse($lines);
foreach ($lines as $line) {
$pieces = explode("|", $line);
echo htmlspecialchars($pieces[$data_loc])."<br />\n";
}
?>

try this

 

<?php
$lines = file("newsadd.txt");
$lines = array_reverse($lines);
foreach ($lines as $line)
{
$pieces = explode("|", $line);
if(strtolower($pieces[5]) == "other")
{
	echo htmlspecialchars($line)."<br />\n";
}else{
	echo "DEBUG:".$pieces[5]."<br>";
}
}
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.