Re: How to write this correctly?
| From: | joel boonstra | Date: | Sat, 06 Mar 2004 21:17:57 +0000 |
| Subject: | Re: How to write this correctly? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-179658@lists.php.net to get a copy of this message | ||
On Sat, Mar 06, 2004 at 09:34:26AM -0500, Brian V Bonini wrote:
> > 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;
> }
Hrm... why the switch() statement? I can see the need for validating
user-submitted data, but to make it a little more flexible, maybe
something like this:
<?php
function output($data_file)
{
# this doesn't change
}
$valid_actions = array('people', 'industry', 'art',
'animal', 'contact');
$action = $_GET['action'];
if (in_array($action, $valid_actions)) {
output($action)
}
else {
# handle error somehow
}
?>
To add more valid actions, you can just extend the array of valid
actions, rather than adding clauses to the switch statement.
joel
--
[ joel boonstra | gospelcom.net ]