Re: [PHP] Re: how to insert html code with PHP

[email protected] (tamouse mailing lists)
Newsgroups php.general
Message-ID <CAHUC_t9x9k2BOi8DgX4P9kyeALiO93_ReKs7Dp9x0inF9OvukQ@mail.gmail.com>
On Thu, Apr 11, 2013 at 1:12 PM, Rafnews <[email protected]> wrote:
> On 11.04.2013 19:19, Jim Giner wrote:
>>
>> On 4/11/2013 10:48 AM, Rafnews wrote:
>>>
>>> On 11.04.2013 13:34, Rafnews wrote:
>>>>
>>>> Hi,
>>>>
>>>> I would like to insert a piece of HTML code inside several pages.
>>>> all pages are differently named.
>>>>
>>>> i need in each page to find a particular tag, let's say <div
>>>> id="#submenu">...</div> (so based on its ID and tagname) and inside it
>>>> to insert my PHP/HTML code.
>>>>
>>>> how can i do that ?
>>>>
>>>> thx.
>>>>
>>>> A.
>>>
>>>
>>> Ok i can write an include_once('myfile.php');
>>>
>>> however if i have an array in this file, other files where is the
>>> include_once(); do not recognize the array.
>>>
>>> here is an extract of this file to include (submenu.php):
>>> <?php
>>> $submenu = array();
>>> $submenu[] = array('id' => 1,'class'=>'menuitem1');
>>> $submenu[] = array('id' => 2,'class'=>'menuitem2');
>>>
>>> // function in the same file
>>> function DisplayMenu($title){
>>>   // here i do not have access to the previous array $submenu define
>>> outside the function
>>>   ...
>>> }
>>>
>>> as it is included the function DisplayMenu should also have access to
>>> $submenu array, no ?
>>>
>>>
>>>
>> basic php question.  Variables within any function are local to that
>> function (except for superglobals like $_POST, $_SESSION, etc.).  In order
>> to reference a var defined/used outside that function you must add a global
>> statement in the function:
>>
>> function xxxx()
>> {
>>    global $submenu;
>>
>> ...
>> ...
>> }
>>
> I solve my problem creating a class and now it works well.

Probably the best solution. As you asked the question here, it would
be nice to share your solution so others with a similar problem can
benefit.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.