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
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
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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";
}
?>

Link to comment
Share on other sites

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>";
}
}
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.