Page 1 of 1

passing selected id from one function to another

Posted: Wed Mar 10, 2004 6:40 pm
by Rmias
Hi I have the following two functions. the first function displays the topics in the forum. what I would like to do is if the user select one topic and click on it, it will pass the selected topic id to the other function. Here are the codes. what I am getting is blank page in the 2nd function.

Code: Select all

<?php
function viewForum()  {
	if(!($_REQUEST['action']=="viewForum"))return;
           
           $db_connect = mysql_connect("localhost") or die ("Unable to connect to the LocalHost");
           $db_select  = mysql_select_db("coursework",$db_connect) or die ("Unable to connect to the database");
           $query = "SELECT id, title,date_format(date_created, '%b %e %Y at %r') as date,created_by 
 		     FROM topic 
 		     ORDER BY date_created DESC";	     
 	   $result = mysql_query($query, $db_connect) or die (mysql_error(). '<br> SQL: '.$sql);
 	   
 	   if (!(mysql_num_rows($result))) {
 	   	echo "<b>No topics found in the forum </b><br>";
 	   }
 	   else {
 	   	echo "<table border=1>";
 	   	echo "<tr>";
 	   	echo "<th>Posted Topics</th>";
 	   	echo "<th>Number of Posts</th>";
 	   	echo "</tr>";
 	   		while ($posts = mysql_fetch_array($result))
			{
			   $id           = $posts['id'];
			   $title        = trim($posts['title']);
			   $date_created = $posts['date'];
			   $created_by   = trim($posts['created_by']);
			   
			   $num_replies = "SELECT count(post_id) from post WHERE id = $id";
			   $result_num_replies = mysql_query($num_replies, $db_connect) or die (mysql_error(). '<br> SQL: '.$sql);
			   $total_num_replies  = mysql_result($result_num_replies,0,'count(post_id)');
			            
			   echo "<tr>";
			   ?>
			   <td><A href="<?=$_SERVER['PHP_SELF']?>?action=displayTopic=<?=$id?>"><b><? echo "$title";?></b></a></br>
			   <? echo "Thread posted on <b>$date_created</b> by <i>$created_by</i>";?>
			   <td align=center><? echo "$total_num_replies"; ?></td>
			   </tr>
			   <?
			 }
		echo "</table>";
		}		
	?>
	<p>
	<A href="<?=$_SERVER['PHP_SELF']?>?action=Cancel">Go Back</A>&nbsp;&nbsp;&nbsp;
	<A href="<?=$_SERVER['PHP_SELF']?>?action=submitTopic">Create New Topic</A><br>
	</p>
	<?

}
?>
and the code for the second function is as follows (i removed all the code as it is more than 100 lines just used the first line for debugging

Code: Select all

<?php
function displayTopic()  {
if(!($_REQUEST['action']=="displayTopic"))return;
$id = $_REQUEST[displayTopic];
//$id = $_GET['displayTopic'];

echo "Topic id selected is $id";

}

?>
Please help

Posted: Wed Mar 10, 2004 7:03 pm
by scorphus
Try change from this:

Code: Select all

<A href="<?=$_SERVER['PHP_SELF']?>?action=displayTopic=<?=$id?>"><b><? echo "$title";?></b></a>
to this:

Code: Select all

<a href="<?=$_SERVER['PHP_SELF']?>?action=displayTopic&displayTopic=<?=$id?>"><b><? echo "$title";?></b></a>

Posted: Thu Mar 11, 2004 2:15 am
by Rmias
Hey scorphus Thanks again and again it finally works i was struggling with this problem for more than 8 hours

I really appreaciate your help just a quick question what is the function of & in displayTopic&displaytopic = .........

Posted: Thu Mar 11, 2004 9:02 am
by scorphus
I'm happy to help ;)

Everything after the '?' in a URL is a query string like this:

Code: Select all

http://www.php.net/source.php?url=/index.php
Here url eill be converted to a variable filed ($_GET['url']) when the script starts.
With the '&' symbol you can separate the variables to be passed by the query string, like this:

Code: Select all

http://www.tutorials.net/showtut.php?section=photoshop&tutName=tut01&page=2
When the showtut.php script starts the $_GET array will be like this:

Code: Select all

Array
(
    &#1111;section] => photoshop
    &#1111;tutName] => tut01
    &#1111;page] => 2
)
You can find further info on this link: HTTP GET variables: $_GET

Scorphus.

Posted: Sat Mar 13, 2004 1:39 pm
by Rmias
hey thank you,

you guys are great, and because of you out there i am learing and practising a lot.