problem with passing variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gecko
Forum Commoner
Posts: 34
Joined: Thu Oct 24, 2002 3:45 am

problem with passing variables

Post 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();
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Instead of just $action try $_GET['action'] and instead of $PHP_SELF, $_SERVER['PHP_SELF'].

Mac
gecko
Forum Commoner
Posts: 34
Joined: Thu Oct 24, 2002 3:45 am

problem passing variables

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
Last edited by volka on Thu Oct 24, 2002 4:19 am, edited 2 times in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
gecko
Forum Commoner
Posts: 34
Joined: Thu Oct 24, 2002 3:45 am

passing variables

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
gecko
Forum Commoner
Posts: 34
Joined: Thu Oct 24, 2002 3:45 am

passing variables

Post 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();
}
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
gecko
Forum Commoner
Posts: 34
Joined: Thu Oct 24, 2002 3:45 am

passing variables

Post 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
Winter
Forum Newbie
Posts: 15
Joined: Fri Feb 07, 2003 12:29 pm
Location: Texas
Contact:

Post 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: :?:
Post Reply