multi select lists and php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jenkins13
Forum Newbie
Posts: 1
Joined: Mon Sep 27, 2010 12:18 am

multi select lists and php

Post 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 :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: multi select lists and php

Post 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';
}
Post Reply