snow Posted September 12, 2007 Share Posted September 12, 2007 Hi to put it simply - I need to be able to store an array in a PHP function and call it whenever I need it. I currently am using this code but it is NOT working. My function in menu-data.php: <?php function myarray() { $days = array ( "Monday"=>array ( "Chicken Burger", "Veal Schnitzel", "Onion Rings", "Hot Potato" ) ); } ?> And now I try to call it in my other web page (enter.php): <?php include("menu-data.php"); myarray(); ?> Now I need to use that array later on for forms and such in enter.php. But it won't let me. I have tested the function with something simple like echo "hello"; and it works fine, but for some reason what I have above does not. If anyone can help me that would be so appreciated. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/68989-need-help-with-including-a-function-please-new-at-php-uni-assignment/ Share on other sites More sharing options...
xyn Posted September 12, 2007 Share Posted September 12, 2007 1. use print_r($myarray); to display your array. 2. to get your data will be liek this... <?php $myarray[] = array('Mon', 'Tue', 'Wed'); # Days $myarray[] = array('Jan', 'Feb', 'Mar'); # Month $array = implode(",", $myarray); #makes array then to get monday would be... echo($array[0][1]); # 0 = days array; 1 = field in days array echo($array[0][2]); # wed echo($array[1][0]); # Jan ?> EDIT: The above code was an exampel of not using a function. however to use your function just return the arrays, etc. <?php function my_array() { $myarray[] = array('Mon', 'Tue', 'Wed'); # Days $myarray[] = array('Jan', 'Feb', 'Mar'); # Month $return implode(",", $myarray); #returns arrays } $array = my_array(); echo($array[0][1]); # 0 = days array; 1 = field in days array echo($array[0][2]); # wed echo($array[1][0]); # Jan ?> Quote Link to comment https://forums.phpfreaks.com/topic/68989-need-help-with-including-a-function-please-new-at-php-uni-assignment/#findComment-346779 Share on other sites More sharing options...
snow Posted September 12, 2007 Author Share Posted September 12, 2007 Thanks for your quick response. I somewhat follow the second example you have given. I have followed the function part the same as you have, but the second I'm not really sure what you have done. What is the code to call that function on a different web page that you have? Sorry I'm very noob at this. Quote Link to comment https://forums.phpfreaks.com/topic/68989-need-help-with-including-a-function-please-new-at-php-uni-assignment/#findComment-346780 Share on other sites More sharing options...
xyn Posted September 12, 2007 Share Posted September 12, 2007 Create a page called "function.php" function my_array() { $myarray[] = array('Mon', 'Tue', 'Wed'); # Days $myarray[] = array('Jan', 'Feb', 'Mar'); # Month $return implode(",", $myarray); #returns arrays } Say you have a page called show_it.php; use: include_once("function.php"); show_it.php <? include_once("function.php"); $array = my_array(); echo($array[0][1]); # 0 = days array; 1 = field in days array echo($array[0][2]); # wed echo($array[1][0]); # Jan Quote Link to comment https://forums.phpfreaks.com/topic/68989-need-help-with-including-a-function-please-new-at-php-uni-assignment/#findComment-346784 Share on other sites More sharing options...
snow Posted September 12, 2007 Author Share Posted September 12, 2007 Hmm... see that doesn't work because my next line of code in using your example "show-it.php" is: foreach ($days as $day => $meals) { echo "<input type=\"radio\" name=\"day\" value=\"" . $day . "\">" . $day; echo "<br />"; } Which loops through my multidimensional array and spits out a radio button for each main day, so people can choose which day they want to dine. Quote Link to comment https://forums.phpfreaks.com/topic/68989-need-help-with-including-a-function-please-new-at-php-uni-assignment/#findComment-346793 Share on other sites More sharing options...
xyn Posted September 12, 2007 Share Posted September 12, 2007 try: <? $array = my_array(); $days = $array[0]; # Fetches every day in this array foreach ($days as $day => $meals) { echo "<input type=\"radio\" name=\"day\" value=\"" . $day . "\">" . $day; echo "<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/68989-need-help-with-including-a-function-please-new-at-php-uni-assignment/#findComment-346798 Share on other sites More sharing options...
snow Posted September 12, 2007 Author Share Posted September 12, 2007 Ok I'll print out everything I have: menu-data.php (functions): <?php function my_array() { $days = array ( "Monday"=>array ( "Chicken Burger", "Veal Schnitzel", "Onion Rings", "Hot Potato" ), "Tuesday"=>array ( "Steak Sandwich", "Chicken Pasta", "Pork Chops", "Taco's" ), "Wednesday"=>array ( "Chicken and Chips", "Beef Casserole", "Meatballs", "Lasagne" ), "Thursday"=>array ( "Fish and Chips", "Tuna Pattie", "Chicko Roll", "Hot Dog" ), "Friday"=>array ( "Nachos", "Tuna Patties", "Maccaroni Cheese", "Bacon and Eggs" ) ); $return implode(",", $myarray); } ?> enter.php (the form): <?php include_once("menu-data.php"); $array = my_array(); $days = $array[0]; // Loop through and display each day as a radio button. foreach ($days as $day => $meals) { echo "<input type=\"radio\" name=\"day\" value=\"" . $day . "\">" . $day; echo "<br />"; } ?> This doesn't work, and what further complicates things is that I need another page that again uses the array stored in the menu-data.php from before but prints everything out like this: <?php echo "<h1><u>Menu</u></h1>"; foreach ($days as $day => $meals) { echo "<li><b>" . $day . "</b><ul>"; foreach ($meals as $meal) { echo "<li>" . $meal . "</li>"; } echo "</ul></li>"; } ?> This prints out: - Day - Meal 1 - Meal 2 etc... Quote Link to comment https://forums.phpfreaks.com/topic/68989-need-help-with-including-a-function-please-new-at-php-uni-assignment/#findComment-346804 Share on other sites More sharing options...
xyn Posted September 12, 2007 Share Posted September 12, 2007 OK. so you want a form when submitted with the DAY to display the DAY they select with the MEALS for that day selected? ie: Wednestday -Chicken and Chips -Beef Casserole -Meatballs -Lasagne etc? Quote Link to comment https://forums.phpfreaks.com/topic/68989-need-help-with-including-a-function-please-new-at-php-uni-assignment/#findComment-346807 Share on other sites More sharing options...
snow Posted September 12, 2007 Author Share Posted September 12, 2007 This is the part I'm currently working on taken from my assignment, this is obviously the best way I can explain what I need to do to you: Stage 1 - A Menu Stored in Arrays You are to create a file called print_menu.php, which should display a menu in the following format: Monday Hamburger Steak Sandwitch Veal Prince Orloff Pork Hock with Wood Fungus Tuesday Chickenburger Chiko Roll Chicken and Chips Beggar's Chicken You should display a menu for each of the weekdays (Monday to Friday). You should invent your own courses (you don't need to use those from the example above). The most important thing is that you should use PHP loops to generate the menu, from data stored in arrays. One approach to this is to use an array keyed by weekday (string) and having elements that are themselves arrays of courses (strings). So, an array of arrays of strings, indexed by strings! Refer to the lecture material on "Multidimensional arrays" for some guidance. As in that lecture material, you will need to use a loop inside another loop to print out the menu. The outer loop should print the day, then get the inner loop to print the menu for that day. Use the ability to view the HTML source of this assignment to find out the HTML tags you need to use to get the indented listing. Stage 2 - Form for selection of day and other details You are to create a form in a file called enter.php, allowing the use to enter the follwing: i) day of the week they want to dine; ii) choice between dining in and pick-up for take away; iii)time they want to arrive (times between 1700 and 2200, to the nearest half hour). You should use radio button form elements for these inputs. The available options in the day of the week input should be generated by iterating through the array from stage 1, which you should copy from print_menu.php to enter.php. The ACTION attribute for the form should at this stage just be a page that displays the choices the user has made. Stage 3 - Sharing the Array (Database) One problem is that you've now got two copies of the array, one in print_menu.php and one in enter.php, and if you make changes to one copy, you need to remember to chage the other copy in the exact same way. To fix this, create a file called menu_data.php and copy the array into it. Then, delete the copies of the array from print_menu.php and enter.php and insert a "require" function call where the array was. The require function inserts the contents of a named file into the file containing the call. You should use it to insert the menu_data.php file. One you have got both print_menu.php and enter.php working again, you should add an extra day (Saturday) to the array in menu_data.php and check that it appear in both print_menu.php and enter.php. Quote Link to comment https://forums.phpfreaks.com/topic/68989-need-help-with-including-a-function-please-new-at-php-uni-assignment/#findComment-346809 Share on other sites More sharing options...
snow Posted September 12, 2007 Author Share Posted September 12, 2007 And your last response, sorry, is what I need to do after the stuff I posted above! Quote Link to comment https://forums.phpfreaks.com/topic/68989-need-help-with-including-a-function-please-new-at-php-uni-assignment/#findComment-346811 Share on other sites More sharing options...
xyn Posted September 12, 2007 Share Posted September 12, 2007 $arr = my_array(); print_r($arr[ucwords(monday)]); # Monday print_r($arr[ucwords(tuesday)]); # Tuesday Quote Link to comment https://forums.phpfreaks.com/topic/68989-need-help-with-including-a-function-please-new-at-php-uni-assignment/#findComment-346815 Share on other sites More sharing options...
Barand Posted September 12, 2007 Share Posted September 12, 2007 Change the function return. <php function my_array() { $days = array ( "Monday"=>array ( "Chicken Burger", "Veal Schnitzel", "Onion Rings", "Hot Potato" ), "Tuesday"=>array ( "Steak Sandwich", "Chicken Pasta", "Pork Chops", "Taco's" ), "Wednesday"=>array ( "Chicken and Chips", "Beef Casserole", "Meatballs", "Lasagne" ), "Thursday"=>array ( "Fish and Chips", "Tuna Pattie", "Chicko Roll", "Hot Dog" ), "Friday"=>array ( "Nachos", "Tuna Patties", "Maccaroni Cheese", "Bacon and Eggs" ) ); return $days; } ?> ?> Quote Link to comment https://forums.phpfreaks.com/topic/68989-need-help-with-including-a-function-please-new-at-php-uni-assignment/#findComment-346883 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.