lorien71 Posted November 27, 2006 Share Posted November 27, 2006 Hi , very new to php and having a little trouble posting so if I am repeating myself because the post that came up with an error message appears I do apologise! I am writing code to read the data in a text file <info.txt> This is a list of people with their name and favourite colours in the format paul,blue jon,red (etc etc)I then have to print out the favourite colour of selected names.I got this far[code]<?php$fave = file('info.txt'); //redirect file content into array$faves_jon=$fave[1]; $faves_bill=$fave[16]; $faves_abby=$fave[18]; //assign name to element from array$favourite_colour = array("$faves_jon","$faves_bill","$faves_abby");// redirect chosen elements to an array called <$favourite_colour>$n=0;while($n<sizeof($favourite_colour)) { $fd=explode(',' ,$favourite_colour[$n]); //using explode to separate the elements of the array print"$fd[0]'s favourite colour is $fd[1]<br>"; $n++; }[/code]This gives the output but seems a little clumsy as I feel I should be finding the elements rather than counting where they are (e.g jon is second in the text list) Secondly I then have to amend this code to count the names of all in the list. I did this by counting the elements in the original array but not the namesHope this makes sense and I haven't worded it too badlyMany thanksLorien71(edited after reading 'how to post' sorry!) Link to comment https://forums.phpfreaks.com/topic/28668-explode-and-code-help-pls/ Share on other sites More sharing options...
Davka Posted November 27, 2006 Share Posted November 27, 2006 Can you post the .txt file? Link to comment https://forums.phpfreaks.com/topic/28668-explode-and-code-help-pls/#findComment-131202 Share on other sites More sharing options...
lorien71 Posted November 27, 2006 Author Share Posted November 27, 2006 [quote author=Davka link=topic=116511.msg474729#msg474729 date=1164666661]Can you post the .txt file?[/quote]hopefully, this will work :)[attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/28668-explode-and-code-help-pls/#findComment-131224 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.