Re: How to write this correctly?
| From: | Brian V Bonini | Date: | Sat, 06 Mar 2004 14:34:26 +0000 |
| Subject: | Re: How to write this correctly? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-179647@lists.php.net to get a copy of this message | ||
On Sat, 2004-03-06 at 08:50, Labunski wrote:
> // or example I have some link:
>
> <a href='index.php?action=people'>About people</a>
>
> // so this function will output the content of people.txt file:
>
> if ($action=="people"){
> function output() {
> $file = file("data/people.txt");
> foreach($file as $value ) {
> $output .= "$value<br>";
> }
> return $output;
> }
> $content = output();
> }else{
> error ();
> }
>
> // How can I improve this function so, that it will work automaticly with
> other links (below) too.
>
> <a href='index.php?action=industry'>Factories</a>
> <a href='index.php?action=art'>Beautiful pictures</a>
> <a href='index.php?action=animals'>Zoo</a>
> <a href='index.php?action=contact'>Contact us</a>
>
> P.S.
> I have tried to improve If tag and the name of .txt file this way:
> if ($action=="$action"){
> function output() {
> $file = file("data/".$action.".txt");
>
> .. but the syntax is incorrect.
> What to do?
function output($data_file)
{
$file = file("data/$data_file.txt");
foreach($file as $value ) {
$output .= "$value<br>";
}
return $output;
}
switch($action) {
case "people":
output('people');
break;
case "industry":
output('industry');
break;
case "art"
output('art')
break;
case "animals":
output('animal');
break;
case "contact":
output('contact');
break;
}
--
Brian V Bonini <b-bonini@cox.net>