Jump to content

Recommended Posts

i have this php function

 

function edit($id)
{

	require_once('DbConnector.php');
	$loginConnector = new DbConnector();
	$getnote = $loginConnector->query("Select * FROM Notepad WHERE Id ='$id'");
	while ($this->notedata = $loginConnector->fetchArray($getnote))
		{	
		$text = $this->notedata['text'];
		$id = $this->notedata['Id'];
		echo $text;
		}
}

 

now it gets the information from my database. But instead of echoing out the information i want to return the values of $text to be used else where within the iniation script.

 

This is how i call upon the function

 

$edit = new note();
$id = $_GET['id'];
$edit->edit($id);

 

 

Link to comment
https://forums.phpfreaks.com/topic/124483-solved-php-functions/
Share on other sites

function edit($id)
{
     require_once('DbConnector.php');
     $loginConnector = new DbConnector();
     $getnote = $loginConnector->query("Select * FROM Notepad WHERE Id ='$id'");
     while ($this->notedata = $loginConnector->fetchArray($getnote))
     {	
  $text = $this->notedata['text'];
  $id = $this->notedata['Id'];
  echo $text;
      }
      
      return $text;
}

 

$edit = new note();
$id = $_GET['id'];
$edit_text = $edit->edit($id);

 

Of course you may want to do some checking before you return $text, i.e., to see if it's a value.

Link to comment
https://forums.phpfreaks.com/topic/124483-solved-php-functions/#findComment-642832
Share on other sites

Well because you're echoing out the $text variable you can't just return the value at the end. You could try saving the outputs into one string, or an array, and then return that?

 

To return a value simply add "return $varname;" to the end, or wherever in the function. The function will stop when it reaches return...

 

Then you'd use something like:

 

$text = edit($id);

 

Adam

Link to comment
https://forums.phpfreaks.com/topic/124483-solved-php-functions/#findComment-642839
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.