Jump to content

Recommended Posts

Hi,

 

I'm new to PHP, so go easy on me.

 

I have file (menus.php) a multi-dimensional array where I'm storing menus/items.

 

$menu = array ( "main"  => array (	"Logging In/Out",
                                    "The Program Window",
                                    "My Settings"
                                     ),
                                      
               "basics" => array (		"Populating Records",
               							"Deleting Records",
               							"Saving Records"
								 ),

		   "sentinel" => array (	 "Item 1",
		   							 "Item 2",
		   							 "Item 3",
		   )

        	 );

 

I want to print the items in another different file (like say index.html), all the items in a sub-arays using a foreach, like:

 

foreach ($menu["main"] as $item) {

echo "<li>$item</li><br />";

}

 

But I'm not sure how to pass the values from the array to the html file. I guess actually it would be the html "getting" the values from the php file. I've looked for ways to do this, but most tutorials on this are high level.

 

I'm not looking for a best practice, I am really trying to learn PHP ground up.

 

Thanks everyone!

 

-Alec

 

Yes, I understand how to echo values from an array in general.

 

What I am wondering is how to echo values from an array, with a foreach function, except the array is in a different file.

 

Hopefully without using a POST?

 

-Alec

foreach (array_expression as $value)

    statement

foreach (array_expression as $key => $value)

    statement

 

 

and 2 examples:

 

<?php

$arr = array(1, 2, 3, 4);

foreach ($arr as &$value) {

    $value = $value * 2;

}

// $arr is now array(2, 4, 6, 8)

unset($value); // break the reference with the last element

?>

 

AND

 

<?php

/* foreach example 1: value only */

 

$a = array(1, 2, 3, 17);

 

foreach ($a as $v) {

    echo "Current value of \$a: $v.\n";

}

?>

 

OR

 

$a = array(1, 2, 3, 17);

 

$i = 0; /* for illustrative purposes only */

 

foreach ($a as $v) {

    echo "\$a[$i] => $v.\n";

    $i++;

}

 

 

good luck!

web, he's talking about how to use one file's variables in another file if I'm not mistaken.

 

use include, but you may want to consider making the array a constant if the values never (or rarely) change.

include("menus.php");

print_r($menu);

Hi there,

you need to generate your HTML code using PHP

 

for example, i will have a file called myHTML.php in the same folder of the menus.php which contains the following:

include('menus.php'); // This is one way that shows how to include the contents of one file into another using PHP

foreach ($menu["main"] as $item) {
echo "<li>$item</li><br />";
}

 

I hope that is clear enough to get you started

 

regards

web, he's talking about how to use one file's variables in another file if I'm not mistaken.

 

use include, but you may want to consider making the array a constant if the values never (or rarely) change.

include("menus.php");

print_r($menu);

 

If my array looks like this:

$menu = array ( "main"  => array (	"Logging In/Out",
                                    "The Program Window",
                                    "My Settings",
                                    "Viewing System Information",
								"Help Menu & Context Sensitive Help",
								"Data Records",
								"Data Record Tabs",
								"Tab Grids",
                                     ),
                                      
               "basics" => array (		"Populating Records",
               							"Deleting Records",
               							"Saving Records"
								 ),

		   "sentinel" => array (	 "Item 1",
		   							 "Item 2",
		   							 "Item 3",
		   )

        	 );

 

...how does it need to change to be a constant? And yes, the values should never change, so I suppose it's just safer to make it a constant so nothing happens accidentally later?

 

Thanks,

Alec

 

 

actually I kind of jumped the gun when I mentioned constants. Only scalar (like ints, floats, strings, etc.) can be made into costants.

 

There are some workarounds, like storing the serialized string of the array into a constant, but honestly, I wouldn't bother, as its not that big of a deal

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.