Jump to content

print names


mra

Recommended Posts

[quote author=mra link=topic=117418.msg479024#msg479024 date=1165322052]
hi ive got a code but its not working it meant to pick out 3 names with their drinks out of 20 names and drinks but it doesnt seem to print out this is wat ive done so far
<?php
$page = file(‘drinkz.txt’);
print "Jim, Peter, Abigail"
[/quote]

What you have so far is going to pull the text of "drinkz.txt" in as an array, but then, you're not doing anything with it. What is the structure of your "drinkz.txt" file? If we know that, we'll be able to better help you pull what you're after. A print statement simply prints out what is in your string, so you'll get "Jim, Peter, Abigail" out on the screen.
Link to comment
https://forums.phpfreaks.com/topic/29515-print-names/#findComment-135428
Share on other sites

ok here is something you could use
[code]<?php
// read file into an array
$lines = file('drinks.txt');
// create array for new data
$data=array();
// loop through each line
foreach($lines as $val){
// break up each line by the comma
$line = explode(",", $val);
// make name the key and the drink the value
$data[$line[0]] = $line[1];
}
// now you have an array called data now you can use is any way you like
foreach($data as $name => $drink){
echo "$name: $drink<br>\n";
}
?>[/code]

Ray
Link to comment
https://forums.phpfreaks.com/topic/29515-print-names/#findComment-135458
Share on other sites

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.