p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: passing a variable with a link


Message #1 by "svazi" <svaziphil@s...> on Wed, 25 Sep 2002 05:06:21
Hi all

Around two month ago I asked how to switch between functions by using a 
link and dan otrowski explained it to me. thanks again. I did not have 
time to continue the little project at that time (exams), but now, i 
continue and am stuck, after some days trying, with passing a variable to 
the function.

the code below shows a list of lecturers and a link "info" which should 
call a function that will display the lecturers details with an SQL 
statement. It calls the function but I can not pass the $user_id along to 
execute the SQL statment properly.

I tought something like "DisplayLecturerDetails&info=$user_id" is 
necessary but it does not work.


I think that somewhere in this part of the code is the problem:
---------------------------------------------------------

TD WIDTH=\"25%\" ALIGN=\"CENTER\">
		  <A HREF= "<? echo "$PHP_SELF?
action=DisplayLecturerDetails&info=$user_id ";?>" >info</A>  
		  <td width="108">Time table</td>
          <td width="78">Files</td>
        </tr>


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

The complete code below:



<?php

include "dbconnect_inc.php";
	?>

<html>
<head>
<title>sitmub</title>

</head>

<body bgcolor="#ffffff" topmargin="0" leftmargin="0">
<table border="0" width="777">
  <tr> 
    <td width="100%"> 
      <p>&nbsp;</p>
      <p> 
        <? 
	  
	  
	  /********** function to display list of lecturers *********/
	  function DisplayLecturers() {
	  global $PHP_SELF,$user_id;
	  
	  	 
	  $result = mysql_query ("select Name, FirstName, user_id
	   						 from users
							 where type 
= 'Teacher'
							 order by Name") 
		or die(mysql_error()); 
		
		
		while ($row = mysql_fetch_array($result))	
	   { ?>
      </p>
      <table width="770" border="0" cellspacing="1" cellpadding="1">
        <tr> 
          <td width="158"> 
            <? echo $row["Name"] ?>
          </td>
          <td width="355" align="left" valign="top"> <? echo $row
["FirstName"];?> 
          </td>
          <TD WIDTH=\"25%\" ALIGN=\"CENTER\">
		  <A HREF= "<? echo "$PHP_SELF?
action=DisplayLecturerDetails&info=$user_id ";?>" >info</A>  
		  <td width="108">Time table</td>
          <td width="78">Files</td>
        </tr>
      </table>
      <?
	  }//end while
	 }/********** end function displayLecuters*********************/
	 
	 
	 /************function to display details of lecturer *********/
	 function DisplayLecturerDetails() {
	 echo "this is details";
  
	 
	 $query = mysql_query ("select Name, FirstName, text, email, 
homepage, user_id
			 from users
			 where user_id = '$user_id'");
	      ?>
      <table width="770" border="0" cellspacing="1" cellpadding="3">
        <?
		
		while ($row = mysql_fetch_array($query)){
		?>
        <tr align="left" valign="top"> 
          <td width="171">Name</td>
          <td width="586"> <? echo $row["Name"] ;?> </td>
        </tr>
        <tr align="left" valign="top"> 
          <td width="171" height="2">First Name</td>
          <td width="586" height="2"> <? echo $row["FirstName"] ?> </td>
        </tr>
        <tr align="left" valign="top"> 
          <td width="171">About You:</td>
          <td width="586"> <? echo $row["text"] ?> </td>
        </tr>
        <tr align="left" valign="top"> 
          <td width="171">email:</td>
          <td width="586"> <a href="<?echo $row[email]?>"> 
            <? $row["email"] ?>
            </a> </td>
        </tr>
        <tr align="left" valign="top"> 
          <td width="171">Home Page:</td>
          <td width="586"> <a href="<? echo $row[homepage] ?>"><? echo 
$row["homepage"] ?></a> 
          </td>
        </tr>
        <tr align="left" valign="top"> 
          <td width="171">&nbsp;</td>
          <td width="586">&nbsp;</td>
        </tr>
      </table>
		
      <?
		}//end while
		
	}/***************** end displayLecturerDetails ****************/
	?>
      
	  
	  <? 
	  // *** Switching between the functions ***
	  
           if(isset($action) && $action != ''){
	  
		  switch($action) {
			  case "timetable":
				timetable();
			  break;
			  case "DisplayLecturerDetails":
				DisplayLecturerDetails();
			  break;
			  default: 
				displayLecturers();
			  break;

  	 }// end switch 
} else {
    
    echo "There was no action!<br>\n";   // so we know there wasn't an
                                         // action but you can go ahead
    DisplayLecturers();                  // with the script

}	    
	  ?>
      <!--*************  end php ************** -->
      <p>&nbsp;</p>
      </td>
  </tr>
</table>

</body>

</html>
Message #2 by "Nikolai Devereaux" <yomama@u...> on Tue, 24 Sep 2002 22:11:20 -0700
> I tought something like "DisplayLecturerDetails&info=$user_id" is
> necessary but it does not work.
>
> I think that somewhere in this part of the code is the problem:
  ...

> echo "$PHP_SELF?action=DisplayLecturerDetails&info=$user_id ";

should work just fine as long as $PHP_SELF and $user_id are, indeed, set.

Since you're importing the global variables user_id and PHP_SELF into function
scope, you should make sure you're not having problems with register_globals
(like 90% of the list has had, myself included).

I can't see anywhere in your code where $user_id is set, so I'm assuming it's
via a link or form.  Use $_GET['user_id'] or $_POST['user_id'] instead of
global $user_id;

nik

Message #3 by "svazi" <svaziphil@s...> on Thu, 26 Sep 2002 05:01:47
Hi nik

I think I have 2 mistakes. One I found yesterday. 


"I tought something like "DisplayLecturerDetails&info=$user_id" is 
necessary but it does not work."

The $user_id is not the correct variable... the correct one is $row
[user_id]. So one part is solved.

<A HREF= "<? echo "$PHP_SELF?action=DisplayLecturerDetails&info=$row
[user_id];"?>" >info</A>



But as you said the user_id has to be set.


If I click on the link Info and the function is called the user_id is not 
passed to the function DisplayLecturerDetails().

I test the user_id with
if(empty($userid)) error_message('Empty User ID!');
and it says empty user_id. Why?

the register globals setting is ok (ON).



Again the complete code below:



<?php

include "dbconnect_inc.php";
	?>

<html>
<head>
<title>sitmub</title>

</head>

<body bgcolor="#ffffff" topmargin="0" leftmargin="0">
<table border="0" width="777">
  <tr> 
    <td width="100%"> 
      <p>&nbsp;</p>
      <p> 
        <? 
	  
	  
	  /********** function to display list of lecturers *********/
	  function DisplayLecturers() {
	  global $PHP_SELF,$user_id;
	  
	  	 
	  $result = mysql_query ("select Name, FirstName, user_id
	   						 from users
							 where type 
= 'Teacher'
							 order by Name") 
		or die(mysql_error()); 
		
		
		while ($row = mysql_fetch_array($result))	
	   { ?>
      </p>
      <table width="770" border="0" cellspacing="1" cellpadding="1">
        <tr> 
          <td width="158"> 
            <? echo $row["Name"] ?>
          </td>
          <td width="355" align="left" valign="top"> <? echo $row 
["FirstName"];?> 
          </td>
          <TD WIDTH=\"25%\" ALIGN=\"CENTER\">
		  <A HREF= "<? echo "$PHP_SELF? 
action=DisplayLecturerDetails&info=$user_id ";?>" >info</A>  
		  <td width="108">Time table</td>
          <td width="78">Files</td>
        </tr>
      </table>
      <?
	  }//end while
	 }/********** end function displayLecuters*********************/
	 
	 
	 /************function to display details of lecturer *********/
	 function DisplayLecturerDetails() {
	   
	 
	 $query = mysql_query ("select Name, FirstName, text, email, 
homepage, user_id
			 from users
			 where user_id = '$user_id'");
	      ?>
      <table width="770" border="0" cellspacing="1" cellpadding="3">
        <?
		
		while ($row = mysql_fetch_array($query)){
		?>
        <tr align="left" valign="top"> 
          <td width="171">Name</td>
          <td width="586"> <? echo $row["Name"] ;?> </td>
        </tr>
        <tr align="left" valign="top"> 
          <td width="171" height="2">First Name</td>
          <td width="586" height="2"> <? echo $row["FirstName"] ?> </td>
        </tr>
        <tr align="left" valign="top"> 
          <td width="171">About You:</td>
          <td width="586"> <? echo $row["text"] ?> </td>
        </tr>
        <tr align="left" valign="top"> 
          <td width="171">email:</td>
          <td width="586"> <a href="<?echo $row[email]?>"> 
            <? $row["email"] ?>
            </a> </td>
        </tr>
        <tr align="left" valign="top"> 
          <td width="171">Home Page:</td>
          <td width="586"> <a href="<? echo $row[homepage] ?>"><? echo 
$row["homepage"] ?></a> 
          </td>
        </tr>
        <tr align="left" valign="top"> 
          <td width="171">&nbsp;</td>
          <td width="586">&nbsp;</td>
        </tr>
      </table>
		
      <?
		}//end while
		
	}/***************** end displayLecturerDetails ****************/
	?>
      
	  
	  <? 
	  // *** Switching between the functions ***
	  
           if(isset($action) && $action != ''){
	  
		  switch($action) {
			  case "timetable":
				timetable();
			  break;
			  case "DisplayLecturerDetails":
				DisplayLecturerDetails();
			  break;
			  default: 
				displayLecturers();
			  break;

  	 }// end switch 
} else {
    
    echo "There was no action!<br>\n";   // so we know there wasn't an
                                         // action but you can go ahead
    DisplayLecturers();                  // with the script

}	    
	  ?>
      <!--*************  end php ************** -->
      <p>&nbsp;</p>
      </td>
  </tr>
</table>

</body>

</html>








> 
> I tought something like "DisplayLecturerDetails&info=$user_id" is
> necessary but it does not work.
>
> I think that somewhere in this part of the code is the problem:
  ...

> echo "$PHP_SELF?action=DisplayLecturerDetails&info=$user_id ";

should work just fine as long as $PHP_SELF and $user_id are, indeed, set.

Since you're importing the global variables user_id and PHP_SELF into 
function
scope, you should make sure you're not having problems with 
register_globals
(like 90% of the list has had, myself included).

I can't see anywhere in your code where $user_id is set, so I'm assuming 
it's
via a link or form.  Use $_GET['user_id'] or $_POST['user_id'] instead of
global $user_id;

nik









Message #4 by "svazi" <svaziphil@s...> on Fri, 27 Sep 2002 03:56:42
ok, I finally got it to work


The problem was that I did not understand the whole concept of it. So I 
got all the variable names wrong.

The problem was in this line:

<A HREF= "<? echo "$PHP_SELF?
action=DisplayLecturerDetails&user_id=$user_id;"?>"


greetings, phil


For those that would like to know the whole working code below:


<?php
include "dbconnect_inc.php";
?>

<html>
<head>
<title>sitmub</title>

</head>

<body bgcolor="#ffffff" topmargin="0" leftmargin="0">
<table border="0" width="777">
  <tr> 
    <td width="100%"> 
      <p>
        <? 
	  	  
	  /********** function to display list of lecturers *********/
	  function DisplayLecturers() {
	 	 
	  global $PHP_SELF,$user_id, $sort_order;
	  
	  if(empty($sort_order)) {
      $sort_order = "Name";
   }
	 $result = mysql_query ("select Name, FirstName, user_id
	  						 from users
							 where type 
= 'Teacher'
							 order by 
$sort_order") 
		or die(mysql_error()); //$result is all the rows from the 
news table
	 ?>
	 
	 <table width="770" border="0">
        <tr>
          <td width="168" height="23"><a href="<?php echo "$PHP_SELF?
action=displayLecturers&sort_order=Name"; ?>">Name</a></td>
          <td width="341"><a href="<?php echo "$PHP_SELF?
action=displayLecturers&sort_order=FirstName"; ?>">First 
            Name</a></td>
          <td width="53">ID</td>
          <td width="104">&nbsp;</td>
          <td width="82">&nbsp;</td>
        </tr>
      </table>
	 
<?	 
		
		while ($row = mysql_fetch_array($result))	//as long 
as there are rows
	   { 
	     $Name = $row["Name"];
		 $FirstName = $row["FirstName"];
		 $user_id = $row["user_id"];?>
      </p>
      <table width="770" border="0" cellspacing="1" cellpadding="1">
        <tr> 
          <td width="172"> 
            <? echo $Name ?>
          </td>
          <td width="341" align="left" valign="top"> <? echo $FirstName;?
> 
          </td>
          <TD WIDTH=55 ALIGN=\"CENTER\"> <A HREF= "<? echo "$PHP_SELF?
action=DisplayLecturerDetails&user_id=$user_id;"?>" >info</A> 
            <? echo $user_id;?> 
          <td width="108">Time table</td>
          <td width="78">Files</td>
        </tr>
      </table>
      <?
	  }//end while
	 }/********** end function displayLecuters*********************/
	 
	 /************function to display details of lecturer *********/
	 function DisplayLecturerDetails() {
	 global $PHP_SELF,$user_id;
	  	 
	 $query = mysql_query ("select Name, FirstName, text, email, 
homepage, user_id
			 from users
			 where user_id = '$user_id'");
	      ?>
      <table width="770" border="0" cellspacing="1" cellpadding="3">
        <?
		
		while ($row = mysql_fetch_array($query)){
		
		 $Name = $row["Name"];
		 $FirstName = $row["FirstName"];
		 $user_id = $row["user_id"];
		 $text = $row["text"];
		 $email = $row["email"];
		 $homepage = $row["homepage"];
		
		 ?>
        <tr align="left" valign="top"> 
          <td width="171">Name</td>
          <td width="586"> <? echo $Name ;?> </td>
        </tr>
        <tr align="left" valign="top"> 
          <td width="171" height="2">First Name</td>
          <td width="586" height="2"> <? echo $FirstName ?> </td>
        </tr>
        <tr align="left" valign="top"> 
          <td width="171">About You:</td>
          <td width="586"> <? echo $text ?> </td>
        </tr>
        <tr align="left" valign="top"> 
          <td width="171">email:</td>
          <td width="586"> <a href="<?echo $email?>"> 
            <? $email ?>
            </a> </td>
        </tr>
        <tr align="left" valign="top"> 
          <td width="171">Home Page:</td>
          <td width="586"> <a href="<? echo $homepage ?>"><? echo 
$homepage ?></a> 
          </td>
        </tr>
        <tr align="left" valign="top"> 
          <td width="171">&nbsp;</td>
          <td width="586">&nbsp;</td>
        </tr>
      </table>
		
      <?
		}//end while
		
	}/***************** end displayLecturerDetails ****************/
	?>
     
	  <? 
	  // *** Switching between the functions ***
	  if(isset($action) && $action != ''){
	  
		  switch($action) {
			  case "timetable":
				timetable();
			  break;
			  case "DisplayLecturerDetails":
				DisplayLecturerDetails();
			  break;
			  default: 
				displayLecturers();
			  break;

  	 }// end switch 
} else {
    
    echo "There was no action!<br>\n";   // so we know there wasn't an
                                         // action but you can go ahead
    DisplayLecturers();                  // with the script

}	    
	  ?>
      <!--*************  end php ************** -->
      <p>&nbsp;</p>
      </td>
  </tr>
</table>

</body>

</html>









> Hi nik

> I think I have 2 mistakes. One I found yesterday. 

> 
"> I tought something like "DisplayLecturerDetails&info=$user_id" is 
n> ecessary but it does not work."

> The $user_id is not the correct variable... the correct one is $row
[> user_id]. So one part is solved.

> <A HREF= "<? echo "$PHP_SELF?action=DisplayLecturerDetails&info=$row
[> user_id];"?>" >info</A>

> 

> But as you said the user_id has to be set.

> 
I> f I click on the link Info and the function is called the user_id is 
not 
p> assed to the function DisplayLecturerDetails().

> I test the user_id with
i> f(empty($userid)) error_message('Empty User ID!');
a> nd it says empty user_id. Why?

> the register globals setting is ok (ON).

> 

Message #5 by "Nikolai Devereaux" <yomama@u...> on Mon, 30 Sep 2002 11:09:52 -0700
> I think I have 2 mistakes. One I found yesterday.
>
> "I tought something like "DisplayLecturerDetails&info=$user_id" is
> necessary but it does not work."
>
> The $user_id is not the correct variable... the correct one is $row
> [user_id]. So one part is solved.
>
> <A HREF= "<? echo "$PHP_SELF?action=DisplayLecturerDetails&info=$row
> [user_id];"?>" >info</A>
>
>
> But as you said the user_id has to be set.
>
>
> If I click on the link Info and the function is called the user_id is not
> passed to the function DisplayLecturerDetails().
>
> I test the user_id with
> if(empty($userid)) error_message('Empty User ID!');
> and it says empty user_id. Why?
>
> the register globals setting is ok (ON).


Okay, without looking through your complete code, I'd guess that it's because
userid is never set -- you're passing the variable as "info" on the GET query
string, not as "userid".

Try using
  if(empty($_GET['info']))

instead of
  if(empty($_GET['userid']))


take care,

nik


  Return to Index