php_joe Posted June 29, 2006 Share Posted June 29, 2006 Ok,I have a file with a table on it:[code]<table><tr><td>Pet</td><td>Owner</td><td>Location</td></tr><tr><td>Cat</td><td>John</td><td>City</td></tr><tr><td>Dog</td><td>Amy</td><td>City</td></tr><tr><td>Dog</td><td>Wayne</td><td>Country</td></tr><tr><td>Cat</td><td>Sarah</td><td>City</td></tr><tr><td>Cat</td><td>Jack</td><td>Country</td></tr></table>[/code]I want to only show the rows with Cats. So far I have:[code]$content = file_get_contents($url); $content[1] = str_replace("<table>", "", $content[1]);$content[1] = str_replace("<tr><td>", "", $content[1]);$content[1] = str_replace("</td></tr>", "", $content[1]);$content[1] = str_replace("</table>", "", $content[1]);$content[1] = str_replace("</td><td>", "|", $content[1]);[/code]If I echo $content[1] I will get:[b]Pet|Owner|LocationCat|John|CityDog|Amy|CityDog|Wayne|CountryCat|Sarah|CityCat|Jack|Country[/b]I obviously want to explode the data at each "|" but how do I loop through each line? I have been trying to figure out [b]foreach[/b] because that seems to be what I need, but I can't seem to figure it out. :(Thanks in advance!Joe Quote Link to comment https://forums.phpfreaks.com/topic/13217-help-with-foreach/ Share on other sites More sharing options...
Buyocat Posted June 29, 2006 Share Posted June 29, 2006 Why don't you store this information in a database instead of a marked up file, that way you can make it take any form you want, instead of having to rip formatting off the previous entry.If a database isn't an option then I have a second solution for you that will get around this formating business. Create a text file somewhere with the information stored like so...Cat|John|CityDog|Amy|CountryetcThen use open the file and get the contents with the file() function. You'll now have an array of strings, where each line is an entry in the array. So then you can eregi each array value for the string "Cat" if found you display that, exploding the string and then putting it into a table format. Otherwise you skip it and move on. This has the added benefit of making it easy to switch to dogs later, or both. It also let's you apply any formatting you want. Quote Link to comment https://forums.phpfreaks.com/topic/13217-help-with-foreach/#findComment-50880 Share on other sites More sharing options...
wildteen88 Posted June 29, 2006 Share Posted June 29, 2006 If you want to get each line seperatly you'll need to use explode, like so:$content[1] = explode("\n", $content[1]);Now each line will be in its own array you'll this:$content[1][x]x being the line number. But rememeber arrays start at zero. SO if you want line 5 you'll need to replace x with 4 like so:$content[1][4] // line5Hope that helps. Also you can use a foreach loop to do the dirty work for you to get each line, like so[code]foreach($content[1] as $key => $value){ echo "\$content[{$key}] = \"$value\"<br />\n";}[/code]Then if you want to get the Pet, Owner and address on their own use the following in your foreach loop instead:[code]foreach($content[1] as $key => $value){ ($pet, $owner, $address) = explode("|", $content[1][$key]); echo $owner . ' has a ' . $pet . ' which lives at ' . $address;}[/code]Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/13217-help-with-foreach/#findComment-50895 Share on other sites More sharing options...
php_joe Posted June 30, 2006 Author Share Posted June 30, 2006 Thanks wildteen88! It worked.And thanks for the suggestion Buyocat. It's always a good idea to make it as simple as possible. Quote Link to comment https://forums.phpfreaks.com/topic/13217-help-with-foreach/#findComment-51077 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.