Page 1 of 1

problem with passing variables

Posted: Thu Oct 24, 2002 3:45 am
by gecko
i have a problem passing variables within my script.

i am using a function (see below) which contains the following:

-------------------------------------------------------------------------------
"<TD WIDTH=\"25%\" ALIGN=\"CENTER\">
<A HREF=\"javascript:open_window('$PHP_SELF?action=view_record&userid=$userid');\">View Record</A></TD>\n";

--------------------------------------------------------------------------------

further in the script i am using the following:

---------------------------------------------------------------------------------
switch($action) {
case "view_record":
view_record();
break;
default:
list_records();
break;
}
----------------------------------------------------------------------------------

The problem is that i am getting the following message in my browserwindow:

Notice: Undefined variable: action in c:\apache\htdocs\phptest\tmp22lg84h9fb.php on line 228

this is i assume because i am working with php4.2 with register globals off.
and the url with $PHP_SELF is no longer accepted for keeping variables.

in the function i have set GLOBAL $PHP_SELF. I am aware that this no longer works, but despite a lot of time spent on reading php.net online documentation and various forums i can not get it to work. can someone please help? Below is the complete function.

thanks

hansie




THIS IS THE ENTIRE FUNCTION

function list_records() {
global $default_dbname, $user_tablename;
global $default_sort_order, $default_order_by, $records_per_page;
global $sort_order, $order_by, $cur_page;
global $PHP_SELF;

$link_id = db_connect($default_dbname);
if(!$link_id) error_message(sql_error());

$query = "SELECT count(*) FROM $user_tablename";

$result = mysql_query($query);
if(!$result) error_message(sql_error());

$query_data = mysql_fetch_row($result);
$total_num_user = $query_data[0];
if(!$total_num_user) error_message('No User Found!');
$page_num = $cur_page + 1;

$total_num_page = $last_page_num
= ceil($total_num_user/$records_per_page);

html_header();

echo "<CENTER><H3>$total_num_user users found. Displaying the page
$page_num out of $last_page_num.</H3></CENTER>\n";

if(empty($order_by)) {
$order_by_str = "ORDER BY $default_order_by";
$order_by = $default_order_by;
}
else $order_by_str = "ORDER BY $order_by";
if(empty($sort_order)) {
$sort_order_str = $org_sort_order = $default_sort_order;
$sort_order = 'DESC';
}
else {
$sort_order_str = $org_sort_order = $sort_order;
if($sort_order == 'DESC') $sort_order = 'ASC';
else $sort_order = 'DESC';
}

if(empty($cur_page)) {
$cur_page = 0;
}
$limit_str = "LIMIT ". $cur_page * $records_per_page .
", $records_per_page";
$query = "SELECT usernumber, userid, username FROM $user_tablename $order_by_str $sort_order_str $limit_str";

$result = mysql_query($query);
if(!$result) error_message(sql_error());
?>

<DIV ALIGN="CENTER">
<TABLE BORDER="1" WIDTH="90%" CELLPADDING="2">
<TR>
<TH WIDTH="25%" NOWRAP>
<A HREF="<?php echo "$PHP_SELF?action=list_records&sort_order=$sort_order&order_by=usernumber"; ?>">
User Number
</A>
</TH>
<TH WIDTH="25%" NOWRAP>
<A HREF="<?php echo "$PHP_SELF?action=list_records&sort_order=$sort_order&order_by=userid"; ?>">
User ID
</A>
</TH>
<TH WIDTH="25%" NOWRAP>
<A HREF="<?php echo "$PHP_SELF?action=list_records&sort_order=$sort_order&order_by=username"; ?>">
User Name
</A>
</TH>
<TH WIDTH="25%" NOWRAP>Action</TH>
</TR>
<?php

while($query_data = mysql_fetch_array($result)) {
$usernumber = $query_data["usernumber"];
$userid = $query_data["userid"];
$username = $query_data["username"];
echo "<TR>\n";
echo "<TD WIDTH=\"25%\" ALIGN=\"CENTER\">$usernumber</TD>\n";
echo "<TD WIDTH=\"25%\" ALIGN=\"CENTER\">$userid</TD>\n";
echo "<TD WIDTH=\"25%\" ALIGN=\"CENTER\">$username</TD>\n";
echo "<TD WIDTH=\"25%\" ALIGN=\"CENTER\">
<A HREF=\"javascript:open_window('$PHP_SELF?action=view_record&userid=$userid');\">View Record</A></TD>\n";
echo "</TR>\n";
}
?>
</TABLE>
</DIV>
<?php
echo "<BR>\n";
echo "<STRONG><CENTER>";
if($page_num > 1) {
$prev_page = $cur_page - 1;

echo "<A HREF=\"$PHP_SELF?action=list_records&sort_order=$org_sort_order&order_by=$order_by&cur_page=0\">[Top]</A>";

echo "<A HREF=\"$PHP_SELF?action=list_records&sort_order=$org_sort_order&order_by=$order_by&cur_page=$prev_page\">[Prev]</A> ";
}
if($page_num < $total_num_page) {
$next_page = $cur_page + 1;
$last_page = $total_num_page - 1;

echo "<A HREF=\"$PHP_SELF?action=list_records&sort_order=$org_sort_order&order_by=$order_by&cur_page=$next_page\">[Next]</A> ";

echo "<A HREF=\"$PHP_SELF?action=list_records&sort_order=$org_sort_order&order_by=$order_by&cur_page=$last_page\">[Bottom]</A>";
}

echo "</STRONG></CENTER>";
html_footer();

Posted: Thu Oct 24, 2002 3:47 am
by twigletmac
Instead of just $action try $_GET['action'] and instead of $PHP_SELF, $_SERVER['PHP_SELF'].

Mac

problem passing variables

Posted: Thu Oct 24, 2002 4:09 am
by gecko
i have tried the suggestion using $_GET['action'], and $_SERVER['PHP_SELF']. I have tried it before after having read the documentation on php.net, and i have tried it again after mac had mentioned it (just to be safe) i get the following error message:

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in c:\apache\htdocs\phptest\tmp2w3wa4hahk.php on line 60

line 60 is this:

<A HREF="<?php echo "$_SERVER['PHP_SELF']?action=list_records&sort_order=$sort_order&order_by=usernumber"; ?>">

i have by the way deleted the GLOBAL $PHP_SELF. when trying out the superglobals.

any idea what is going wrong?

thanks
hansie

Posted: Thu Oct 24, 2002 4:12 am
by volka
try

Code: Select all

&lt;A HREF="&lt;?php echo $_SERVER&#1111;'PHP_SELF'];?&gt;?action=list_records&amp;sort_order=$sort_order&amp;order_by=usernumber"&gt;
(without amp;)
and read http://www.php.net/manual/en/language.t ... ng.parsing to learn more about string parsing ;)

edit: uh I missed the second var.
Better try twigletmac's

Posted: Thu Oct 24, 2002 4:14 am
by twigletmac
Change that line to this:

Code: Select all

&lt;A HREF="&lt;?php echo $_SERVER&#1111;'PHP_SELF']."?action=list_records&amp;sort_order=$sort_order&amp;order_by=usernumber"; ?&gt;"&gt;
or this

Code: Select all

&lt;A HREF="&lt;?php echo "{$_SERVER&#1111;'PHP_SELF']}?action=list_records&amp;sort_order=$sort_order&amp;order_by=usernumber"; ?&gt;"&gt;
Where you have quoted array elements within a double quoted string you need to either concenate the variable (like the first example) or surround it in curly brackets (like the second).

Mac

Posted: Thu Oct 24, 2002 4:31 am
by twigletmac
The other thing you can do, since you're going to be using $_SERVER['PHP_SELF'] throughout the function is just put:

Code: Select all

$PHP_SELF = $_SERVER&#1111;'PHP_SELF'];
after your global statements and then you don't have to change every instance of it in the function.

Mac

passing variables

Posted: Thu Oct 24, 2002 4:39 am
by gecko
thanks, i have made the changes. i still get the following message:


Notice: Undefined index: action in c:\apache\htdocs\phptest\tmp43ya94hc2e.php on line 227

does this seem wrong to you?

$action = $_GET['action'];
switch($action) {
case "view_record":
view_record();
break;
default:
list_records();
break;
}

you would say that by using the $_SERVER['PHP_SELF'] in combination with the $_GET['action'] things should work.

if you would have any more ideas, i would be happy to hear them.

thanks

hansie

Posted: Thu Oct 24, 2002 4:44 am
by twigletmac
Try something like this:

Code: Select all

if (!empty($_GET&#1111;'action'])) {
	switch ($_GET&#1111;'action']) {
		case 'view_record': 
			view_record(); 
			break; 
		default: 
			list_records(); 
			break; 
	}
} else {
	list_records();
}
Mac

passing variables

Posted: Thu Oct 24, 2002 9:16 am
by gecko
if you are still there, thanks mac. i am almost there thanks to your suggestion. anyway one issue left.

in the top of one of my functions (function in full below) i have the following on lines 8 and line 9:

$cur_page = $_GET['cur_page'];
$sort_order = $_GET['sort_order'];

this is because the values for cur_page and sort_order are passed in a url,
and i have got my register_globals off.

functionality wise this works fine, it does everything i want to do, but it first gives me the following error message:

Notice: Undefined index: cur_page in c:\apache\htdocs\phptest\tmpnkqh4howr.php on line 8

Notice: Undefined index: sort_order in c:\apache\htdocs\phptest\tmpnkqh4howr.php on line 9

below the error messages it shows me everything i want to see, working just fine.

any ideas why this might be.

thanks hansie

here follows the complete function:

function list_records() {
global $default_dbname, $user_tablename;
global $default_sort_order, $default_order_by, $records_per_page;
global $sort_order, $order_by;
$cur_page = $_GET['cur_page'];
$sort_order = $_GET['sort_order'];
$PHP_SELF = $_SERVER['PHP_SELF'];

$link_id = db_connect($default_dbname);
if(!$link_id) error_message(sql_error());

$query = "SELECT count(*) FROM $user_tablename";

$result = mysql_query($query);
if(!$result) error_message(sql_error());

$query_data = mysql_fetch_row($result);
$total_num_user = $query_data[0];
if(!$total_num_user) error_message('No User Found!');
$page_num = $cur_page + 1;

$total_num_page = $last_page_num
= ceil($total_num_user/$records_per_page);

html_header();

echo "<CENTER><H3>$total_num_user users found. Displaying the page
$page_num out of $last_page_num.</H3></CENTER>\n";

if(empty($order_by)) {
$order_by_str = "ORDER BY $default_order_by";
$order_by = $default_order_by;
}
else $order_by_str = "ORDER BY $order_by";
if(empty($sort_order)) {
$sort_order_str = $org_sort_order = $default_sort_order;
$sort_order = 'DESC';
}
else {
$sort_order_str = $org_sort_order = $sort_order;
if($sort_order == 'DESC') $sort_order = 'ASC';
else $sort_order = 'DESC';
}

if(empty($cur_page)) {
$cur_page = 0;
}
$limit_str = "LIMIT ". $cur_page * $records_per_page .
", $records_per_page";
$query = "SELECT usernumber, userid, username FROM $user_tablename $order_by_str $sort_order_str $limit_str";

$result = mysql_query($query);
if(!$result) error_message(sql_error());
?>

<DIV ALIGN="CENTER">
<TABLE BORDER="1" WIDTH="90%" CELLPADDING="2">
<TR>
<TH WIDTH="25%" NOWRAP>
<A HREF="<?php echo "$PHP_SELF?action=list_records&sort_order=$sort_order&order_by=usernumber"; ?>">
User Number
</A>
</TH>
<TH WIDTH="25%" NOWRAP>
<A HREF="<?php echo "$PHP_SELF?action=list_records&sort_order=$sort_order&order_by=userid"; ?>">
User ID
</A>
</TH>
<TH WIDTH="25%" NOWRAP>
<A HREF="<?php echo "$PHP_SELF?action=list_records&sort_order=$sort_order&order_by=username"; ?>">
User Name
</A>
</TH>
<TH WIDTH="25%" NOWRAP>Action</TH>
</TR>
<?php

while($query_data = mysql_fetch_array($result)) {
$usernumber = $query_data["usernumber"];
$userid = $query_data["userid"];
$username = $query_data["username"];
echo "<TR>\n";
echo "<TD WIDTH=\"25%\" ALIGN=\"CENTER\">$usernumber</TD>\n";
echo "<TD WIDTH=\"25%\" ALIGN=\"CENTER\">$userid</TD>\n";
echo "<TD WIDTH=\"25%\" ALIGN=\"CENTER\">$username</TD>\n";
echo "<TD WIDTH=\"25%\" ALIGN=\"CENTER\">
<A HREF=\"javascript:open_window('$PHP_SELF?action=view_record&userid=$userid');\">View Record</A></TD>\n";
echo "</TR>\n";
}
?>
</TABLE>
</DIV>
<?php
echo "<BR>\n";
echo "<STRONG><CENTER>";
if($page_num > 1) {
$prev_page = $cur_page - 1;

echo "<A HREF=\"$PHP_SELF?action=list_records&sort_order=$org_sort_order&order_by=$order_by&cur_page=0\">[Top]</A>";

echo "<A HREF=\"$PHP_SELF?action=list_records&sort_order=$org_sort_order&order_by=$order_by&cur_page=$prev_page\">[Prev]</A> ";
}
if($page_num < $total_num_page) {
$next_page = $cur_page + 1;
$last_page = $total_num_page - 1;

echo "<A HREF=\"$PHP_SELF?action=list_records&sort_order=$org_sort_order&order_by=$order_by&cur_page=$next_page\">[Next]</A> ";

echo "<A HREF=\"$PHP_SELF?action=list_records&sort_order=$org_sort_order&order_by=$order_by&cur_page=$last_page\">[Bottom]</A>";
}

echo "</STRONG></CENTER>";
html_footer();
}

Posted: Thu Oct 24, 2002 9:32 am
by twigletmac
It will report this warning if $_GET['cur_page'] or $_GET['sort_order'] are not set - you can just set them to empty strings if they aren't:

Code: Select all

$cur_page = (isset($_GET&#1111;'cur_page'])) ? $_GET&#1111;'cur_page'] : '';
$sort_order= (isset($_GET&#1111;'sort_order'])) ? $_GET&#1111;'sort_order'] : '';
But since you deal with the prospect of $cur_page and $sort_order being empty (or unset) later on you could just rearrange your code a bit so you have something like this for $cur_page:

Code: Select all

$cur_page = (!empty($_GET&#1111;'cur_page'])) ? $_GET&#1111;'cur_page'] : 0;
instead of:

Code: Select all

if(empty($cur_page)) { 
$cur_page = 0; 
}
For $sort_order you can have something like:

Code: Select all

if(empty($_GET&#1111;'sort_order'])) { 
    $sort_order_str = $org_sort_order = $default_sort_order; 
    $sort_order = 'DESC'; 
} else {
    $sort_order_str = $org_sort_order = $sort_order;
    $sort_order = ($sort_order == 'DESC') ? 'ASC' : 'DESC';
}
instead of

Code: Select all

if(empty($sort_order)) { 
$sort_order_str = $org_sort_order = $default_sort_order; 
$sort_order = 'DESC'; 
} 
else { 
$sort_order_str = $org_sort_order = $sort_order; 
if($sort_order == 'DESC') $sort_order = 'ASC'; 
else $sort_order = 'DESC'; 
}
Mac

passing variables

Posted: Thu Oct 24, 2002 9:40 am
by gecko
Mac, this has really helped me a lot. everything is working, but moreover,
i have learned a lot from this. thank you for your valuable time. hope i will be able to help somone out some other time.

hans

Posted: Thu Feb 13, 2003 2:52 pm
by Winter
Hello,

Your discussions were very helpful.. but.. why I got a blank page without any error message.. totally blank while I tried to execute the codes that you posted here?


Winter :oops: :?: