You are here: Re: Problem getting included source code to work. « All PHP « IT news, forums, messages
Re: Problem getting included source code to work.

Posted by MS on 05/29/06 16:08

Your included file contains a function. Therefore to get the code within
the function to run you first need to include the file and then invoke the
function.
eg.

<?php
include ("functions.php"); // this loads all the functions within
functions.php ready for them to be called.
?>
<form>
Phone Type: <select name="phone_type_1">
<option>Select One</option>
<?php get_phonenames(); // Call the Function?>
</select>
</form>


--
----------------------------------------------------------------------------
http://www.myclubweb.co.uk - The Home of Club Websites
----------------------------------------------------------------------------


"kenoli" <kenoli@igc.org> wrote in message
news:1148875577.032742.32260@j33g2000cwa.googlegroups.com...
> I am having trouble getting included source code to work.
> The idea here is to create a pulldown list using an option field in a
> form where the option items are generated from data selectd from a look
> up table. A function selects the data and puts it into an array that
> is used to create the pulldown menu. When the code is placed directly
> into the php file it works fine, but when it is placed in the file
> using an include, the form displays, but the pull down menu is not
> created from the array.
>
> >From what I have read, included code should execute exactly as if it is
> in the script itself. This doesn't work in this case. Can anyone tell
> me what I am missing here? I am wondering if it may have something to
> do with scope issues, but I can't figure out what is going on.
>
> Here are the details. I have only included the relevant part of the
> form. It is part of a table that inputs other data as well.
>
> ================================================================
>
> At the beginning of the script, I have included a function from an
> included file. Like this:
>
> include ("functions.php"); //Include functions definition file.
>
> ================================================================
>
> This is the relevant function in the included file:
>
> function get_phonenames() {
>
> $query = "SELECT * FROM tlu_phone_type ORDER BY phone_type ASC";
> $result = @mysql_query ($query);
> $row = mysql_fetch_array;
>
> while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
> global $phonenames;
> $phonenames .= "<option>{$row['phone_type']}</option>\n";
> }
> return $phonenames;
> }
>
> ================================================================
>
> The following code correctly utilizes the function from the
> functions.php file and the array it returns to create the pulldown menu
> when I put it in the script itself, like this:
>
> get_phonenames();
>
> print <<<ADDFORM
> <form>
> Phone Type: <select name="phone_type_1"><option>Select
> One</option>$phonenames</select Phone 1: <input name="phone_1"
> type="text" size="12" maxlength="12" />
> </form>
> ADDFORM;
>
> (It correctly creates a drop down menu using data from the lookup table
> tlu_phone_type generated by the get_phone_types function and held in
> the array $phonenames.)
>
> ================================================================
>
> When I add the same code using an include like this:
>
> include "add_records.tpl"; //The content of add_record.tpl is exactly
> the same as the code above (see below).
>
> It prints the html correctly, but does does not create the options from
> the $phonenames array.
>
> Something is keeping the the php code from working in the form.
>
> I have tried invoking the function before the include with no sucess,
> rather than placing it in the included file. This doesn't work either.
>
> ================================================================
>
> Content of add_record.tpl:
>
> get_phonenames();
>
> print <<<ADDFORM
> <form>
> Phone Type: <select name="phone_type_1"><option>Select
> One</option>$phonenames</select Phone 1: <input name="phone_1"
> type="text" size="12" maxlength="12" />
> </form>
> ADDFORM;
>
> ================================================================
>
> I look forward to any help I can get understanding what is going on
> here.
>
> I suspect a scope issue here, but can't figure it out.
>
> Thanks,
>
> --Kenoli
>
> Kenoli Oleari
> Neighborhood Assemblies Network
> kenoli@igc.org
> 510-601-8217
> http://www.horizonsofchange.com
> http://www.sfnan.org
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация