Jump to content

Need help with including a function please! New at php (Uni assignment)


snow

Recommended Posts

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.

Link to comment
Share on other sites

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
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

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;

}
?>

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.