Function error? Help to echo?

Discussion in 'PHP' started by Neo_The_One, Jan 31, 2007.

  1. #1
    Hi,

    i em trying to build in a comment system on my site on my tutorials. Here is the code for it..

    $category = $_GET['t'];
    $step = $_GET['s'];
    
    $spp = $sys->R('System.StepsPerPage');
    
    
    $get_cdata = $db->query("SELECT * FROM tutorials WHERE t_url = '".$category."'");
    if($db->numrows($get_cdata) != 0){
    
    	$mtutorial = $db->fetch();
    
    	$loadsteps = array();
    
    	if($spp == 1){
    		$loadsteps = array($step);
    	} else {
    		for($i=(($step-1)*$spp)+1;$i<=($step*$spp);$i++){
    			$loadsteps[] = $i;
    		}
    	}
    
    	$db->rs_create("SELECT * FROM steps WHERE s_for_tutorial = '".$mtutorial['t_id']."' AND s_number IN (".implode(',', $loadsteps).")");
    	$msteps = $db->rs_flush();
    
    } else {
    	$mtutorial = false;
    }
    
    
    
    function getComments(){
    
    
    	$commentquery = mysql_query("SELECT * FROM comments WHERE tutorialid='".$mtutorial['t_id']."' ORDER BY date") or die(mysql_error());
    
    
    	$commentNum = mysql_num_rows($commentquery);
    
    	echo "<h3 class=\"formtitle\">Current Comments</h3>\n";
    	echo $commentNum . " comments so far (<a href=\"#post\">post your own</a>)\n";
    
    	while($commentrow = mysql_fetch_row($commentquery)){
    
    	$commentbb = BBCode($commentrow[4]);
    
    		$commentDate = formatDate($commentrow[6]);
    
    		echo "
    		<p>$commentbb</p>
    		<p>Posted by ";
    		if($commentrow[3]){
    		echo "<a href=\"$commentrow[3]\">$commentrow[2]</a> ";
    		} else {
    		echo "$commentrow[2] ";
    		}
    		echo "on $commentDate | #$commentrow[0]</p>
    		";
    		
    	}
    	
    }
    PHP:

    As you se in the code this statement:
    $db->rs_create("SELECT * FROM steps WHERE s_for_tutorial = '".$mtutorial['t_id']."' AND s_number IN (".implode(',', $loadsteps).")");
        $msteps = $db->rs_flush();
    PHP:
    Its working etc and functional..
    Now im trying to add the same here on my function getComments
    function getComments(){
    
    
        $commentquery = mysql_query("SELECT * FROM comments WHERE tutorialid='".$mtutorial['t_id']."' ORDER BY date") or die(mysql_error());
    PHP:

    But its not working with this ".$mtutorial['t_id'].", i need to echo that t_id from my tutorials table but how do i do that??


    Please help me out, and please post here if you don't:confused: know what i mean, i can rewrite it and explain it hopefully!!
     
    Neo_The_One, Jan 31, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    The variable $mtutorial doesn't exist inside your function unless you globalize it, or pass it in the arguments.

    Try adding this line as first line inside your function.

    
    
    global $mtutorial;
    
    
    PHP:
     
    nico_swd, Jan 31, 2007 IP
  3. Neo_The_One

    Neo_The_One Active Member

    Messages:
    555
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #3
    great thank you it worked! ;=)

    I really apprisiate it.. ;)
     
    Neo_The_One, Jan 31, 2007 IP