chwebdesigns Posted October 2, 2007 Share Posted October 2, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/71562-help-with-if-statements/ Share on other sites More sharing options...
cooldude832 Posted October 2, 2007 Share Posted October 2, 2007 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 } Quote Link to comment https://forums.phpfreaks.com/topic/71562-help-with-if-statements/#findComment-360301 Share on other sites More sharing options...
chwebdesigns Posted October 2, 2007 Author Share Posted October 2, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/71562-help-with-if-statements/#findComment-360310 Share on other sites More sharing options...
chwebdesigns Posted October 6, 2007 Author Share Posted October 6, 2007 has anybody got any ideaS? Quote Link to comment https://forums.phpfreaks.com/topic/71562-help-with-if-statements/#findComment-363326 Share on other sites More sharing options...
MadTechie Posted October 6, 2007 Share Posted October 6, 2007 no idea what you mean.. <?php if($pieces[5] == "other") { echo "other found"; }else{ echo "no other"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71562-help-with-if-statements/#findComment-363329 Share on other sites More sharing options...
chwebdesigns Posted October 6, 2007 Author Share Posted October 6, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/71562-help-with-if-statements/#findComment-363336 Share on other sites More sharing options...
MadTechie Posted October 6, 2007 Share Posted October 6, 2007 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 ""; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71562-help-with-if-statements/#findComment-363347 Share on other sites More sharing options...
chwebdesigns Posted October 7, 2007 Author Share Posted October 7, 2007 That still doesn't work, all I am getting is a empty line, and there is "OTHER" items in the news txt file Quote Link to comment https://forums.phpfreaks.com/topic/71562-help-with-if-statements/#findComment-363996 Share on other sites More sharing options...
BlueSkyIS Posted October 7, 2007 Share Posted October 7, 2007 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71562-help-with-if-statements/#findComment-364025 Share on other sites More sharing options...
chwebdesigns Posted October 7, 2007 Author Share Posted October 7, 2007 now, that doesn't filter out the items in the "OTHER" category it just "echos" all of them, Quote Link to comment https://forums.phpfreaks.com/topic/71562-help-with-if-statements/#findComment-364095 Share on other sites More sharing options...
MadTechie Posted October 8, 2007 Share Posted October 8, 2007 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>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71562-help-with-if-statements/#findComment-364481 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.