Page 1 of 1

multi select lists and php

Posted: Sat Oct 02, 2010 11:03 pm
by Jenkins13
Hi guys. I'm a newbie at PHP programming and I've been playing around with using multi select lists to load external files. My situation is as a practice in using PHP, I've created 3 recipe .txt files. These three files can be loaded through a multi select list. Eg: I have a cheese sandwich recipe file and the cheese sandwich option in the multi select list. When the user clicks on cheese sandwich and then hits 'Submit', it loads and returns the external file.

Anyway I've figured out how to use it with using the 'Switch' function, just switching through to each appropriate file. But I've realised while this might be okay for 3-5 recipe choices, what if I had 20-40 recipes instead? It would be incredibly tedious to write all that code for each option, plus it would just slow everything down too. Not good.

I know it has something to do with putting the recipe choices in an array, I've played around with that too but haven't been very successful. Thanks guys :)

Re: multi select lists and php

Posted: Sat Oct 02, 2010 11:18 pm
by John Cartwright
Perhaps your input values could correspond to the recipe file. That way, you won't need to hardcode anything.

I.e.,

Code: Select all

//load file, such as /recipes/recipe-applepie.txt';
$recipefile = '/path/to/recipies/recipe-'. $_POST['recipe'] .'.txt';

if (file_exists($recipefile)) {
   echo file_get_contents($recipefile);
} else {
   echo 'Recipe not found';
}
Otherwise, you could store a list of recipe files in an array, and reference it through the recipe name,

I.e.,

Code: Select all

$recipes = array(
   'applepie' => '/pies/apple-pie.txt',
   'blueberrypie' => '/pies/blueberry-pie.txt',
   'bananasplit' => '/desserts/banana-split.txt'
);

if (isset($recipies[$_POST['recipe']])) {
   echo file_get_contents($recipies[$_POST['recipe']]);
} else {
   echo 'Recipe not found';
}