Re: How to write this correctly?

From: Date: Sun, 07 Mar 2004 02:14:32 +0000
Subject: Re: How to write this correctly?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-179675@lists.php.net to get a copy of this message
Hi, Saturday, March 6, 2004, 11:50:04 PM, you wrote: L> // or example I have some link: L> <a href='index.php?action=people'>About people</a> L> // so this function will output the content of people.txt file: L> if ($action=="people"){ L> function output() { L> $file = file("data/people.txt"); L> foreach($file as $value ) { L> $output .= "$value<br>"; L> } L> return $output; L> } L> $content = output(); L> }else{ L> error (); L> } L> // How can I improve this function so, that it will work automaticly with L> other links (below) too. L> <a href='index.php?action=industry'>Factories</a> L> <a href='index.php?action=art'>Beautiful pictures</a> L> <a href='index.php?action=animals'>Zoo</a> L> <a href='index.php?action=contact'>Contact us</a> L> P.S. L> I have tried to improve If tag and the name of .txt file this way: L> if ($action=="$action"){ L> function output() { L> $file = file("data/".$action.".txt"); L> .. but the syntax is incorrect. L> What to do? L> Thanks, L> Roman do something like this: function output($action){ $output = ''; //if no file we return an empty string $fname = 'data/'.$action.'.txt'; if(file_exists($fname){ $file = file($fname); foreach($file as $value ) { $output .= "$value<br>"; } } return $output; } if(isset($_REQUEST['action'])){ $out = output($_REQUEST['action']); } -- regards, Tom

« previous php.general (#179675) next »