Forum Moderators: coopster

Message Too Old, No Replies

Passing Variables into Functions

Why wont my script pass a variable into a function...

         

PSWorx

8:11 am on Nov 7, 2003 (gmt 0)

10+ Year Member



function prntpgnm($content,$name,$show,$id)
{
echo"var pagename: $pagename";
global$PHP_SELF;
$dbuser="#########";
$dbpass="#########";
$db="gdon_contents";
$link=mysql_connect("localhost",$dbuser,$dbpass);
mysql_select_db($db,$link);
$result=mysql_query("SELECT * FROM page$pagename");
while($field=mysql_fetch_array($result)){
echo"<div>\n<b id=\"loginhead2\">Paragraph ID:</b> $field[id]<br>\n";
echo"<b id=\"loginhead2\">Show:</b> ";
if($field[show]==1){echo"Yes";}else{echo"No";}
echo"<br>\n<b id=\"loginhead2\">Content:</b> $field[content]<br>\n";
echo"\n</div><hr align=\"center\" noshade size=\"1\" color=\"#000000\">";
}
}
$id=$pagename;
echo"<h2 id=\"loginhead\">Current Contents for: <span id=\"loginhead3\">";
include("scripts/title.php");
echo"</span></h2>\n";
echo prntpgnm($content,$name,$show,$id);
echo"var pagename: $pagename";

Paying detailed attention to the line "$result=mysql_query("SELECT * FROM page$pagename");" why is it that the valuye of the variable "$pagename" passed from another page "admin.php" isnt avaliable in the function its self but is outside of the function, would you advise compiling this functions contents in the function of the "$pagename" variable? or is there a simpler way of doing it...

All help will be appreciated.

coopster

8:43 am on Nov 7, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



PSWorx, your issue is with Variable Scope [us2.php.net]. The way you currently have the variable
$pagename
defined makes it local to the function itself, a brand new variable if you will. The existing variable you want is outside the function, a global, but still available to you within that function by accessing it through the $GLOBALS array. There are a number of ways to get at that variable without putting your function inside the admin.php script. Here is one option, but read the manual page link to learn more about Variable Scope in PHP (and also see the Array do's and don'ts [us3.php.net] section for more information about using quotes around a string literal array index such as superglobal arrays):

$result=mysql_query("SELECT * FROM page{$GLOBALS['pagename']}");

PSWorx

10:01 am on Nov 7, 2003 (gmt 0)

10+ Year Member



Thanks COOP but i sorted it out, one simple thing, i wasnt passing the variable thru in the functions argument like so:

prntpgnm($content,$name,$show,$id,$pagename)

Works fine now now time for the rest of the system, thanks for your advice tho

P.Davidson

PSWorx

2:36 am on Nov 9, 2003 (gmt 0)

10+ Year Member



Im trying to get my script to write the chosen page to edit as the selected option in the list, heres the code for the list:

function pageedit($result)
{
global$PHP_SELF;
echo"<div id=\"loginhead\">Please select a page to edit from the list:</div>\n";
echo"<form action=\"$PHP_SELF\" method=\"POST\">\n";
echo"<select name=\"pagename\">\n";
$dbuser="****";
$dbpass="****";
$db="gdon_contents";
$link=mysql_connect("localhost",$dbuser,$dbpass);
mysql_select_db($db,$link);
for($x=1;$x<=6;$x++){
$result=mysql_query("SELECT * FROM page$x WHERE id='1'");
while($field=mysql_fetch_array($result)){
echo"<option value=\"$x\"";
if(!$GLOBALS['pagename']=="$x")
{echo"SELECTED>$field[name]\n";
echo $GLOBALS['pagename'];}
else
{echo">$field[name]\n";
echo $GLOBALS['pagename'];}}}
echo"<input type=\"hidden\" name=\"editpages\" value=\"result\"><input type=\"submit\" value=\"Go!\" name=\"pagechoice\" id=\"button\">\n<br>\n</center>\n</form>\n<hr align=\"center\" noshade size=\"1\" color=\"#000000\">\n";
}

The best i can get it to do is to write selected to all other than that i cant seem to get it to work altho i know its something simple...

DONT BOTHER IVE SORTED IT! THANKS ANYWAY