Using PHP to include part of a file, not the whole file?

  • Thread starter Thread starter Zelus et Radix
  • Start date Start date
Z

Zelus et Radix

Guest
I understand how to use PHP include to include the whole content of a given file into a php file, but now I'd like to know if PHP can be used to include only part of that file, I mean, an specific HTML element/block of text among several different.

For example, I have four menus in four languages. I want to have all those menus in a single file then "mark" each of them as an element/block then refer to only one of those from the respective page, not the others. I want to use PHP for doing that because currently I'm learning it.
How can this be done? Thanks in advance.
 
Can't you just use one menu and change the language based on the location that the user request came for?

any ways what you asked can be done with some planning. for example:
1, your menu would be a PHP file with a class, a constructor, a member variable, and 5 functions each function is a menu.

2. when you want to display the menu you pass a parmeter to the menu class, based on that parameter it returns the correct menu:

<?php
Class MenuClass
{
var $m_currentLanguage;
function MenuClass(languageType)
{
$this->m_currentLanguage = languageType;
}
function GetMenuType()
{
if($m_currentLanguage == "english")
{
$this->englishMenu();
}
else if($m_currentLanguage == "spanish")
{
$this->spanishMenu();
}

else if($m_currentLanguage == "french")
{
$this->frenchMenu();
}
else if($m_currentLanguage == "german")
{
$this->gerManMenu();
}
else
{
echo "menu not found";
}
}
function englishMenu()
{
menuString = "<div>menu button 1</div><div> menu button 2</div><div style=\"font-weight:bold\">menu button 3</div>";
//etc.....
return menuString;
}
function spanishMenu()
{
//spanish menu stuff
return menuString;
}
function frenchMenu()
{
// french menu stuff goes here
return menuString;
}
function germanMenu()
{
// german menu stuff goes here
return menuString;
}
}
?>


now when you are ready to use the menu, just call the php class:
<?php
$myMenu = new MenuClass("english");
echo $myMenu->PrintMenu();
?>


hope that helps
 
you could make a class/object with php that when you call the constructor you pass parameters that signal which type of menu is to be used.
 
Back
Top