Jump to content

Using the results from a function... HELP NEEDED!


benfolley

Recommended Posts

Hi there.

 

I have a PHP Function like so:

 

 <?php function JomSocial_field($fieldcode) {
$database=& JFactory::getDBO();
  $user =& JFactory::getUser();
  $userId = $user->get( 'id' );
  $sql = "SELECT (id) FROM #__community_fields WHERE fieldcode= '$fieldcode'";
  $database->setQuery( $sql );
  $fieldID = $database->loadResult();
  $sql = "SELECT (value) FROM #__community_fields_values WHERE field_id= {$fieldID} && user_id= {$userId}";
  $database->setQuery( $sql );
  echo $database->loadResult();
}

?>

 

I can then insert the results into form inputs like so: 

<input type="hidden" id="enrolment_year" name="enrolment_year" value="<?php JomSocial_field("enrolment_year"); ?>" />

 

However, I now want to use the result in a variable like so

 

 <?php $filename1 = "folder/".print(JomSocial_field("enrolment_year"))."/file.zip";

 

but it doesn't work...it just actually prints the result as text on the page. The echo command doesn't work either.  If it were an item that has been posted, I could get it using the $_POST["enrolment_year"], but the trouble is, I want to get the value on first page load, not after a post.

 

Can anyone help me please?

That function doesn't return any data.  If you'd like to use the return value of a function...return something.

 

Also, stop printing things if you want to put them in a variable.  You've taken a function that prints information and attempted to print it again when you're trying to assign it.

Hi there

 

Thanks for pointing this out!!!  You mean a like...

 

 

<?php function JomSocial_field($fieldcode) {

$database=& JFactory::getDBO();

  $user =& JFactory::getUser();

  $userId = $user->get( 'id' );

  $sql = "SELECT (id) FROM #__community_fields WHERE fieldcode= '$fieldcode'";

  $database->setQuery( $sql );

  $fieldID = $database->loadResult();

  $sql = "SELECT (value) FROM #__community_fields_values WHERE field_id= {$fieldID} && user_id= {$userId}";

  $database->setQuery( $sql );

  return $database->loadResult();

}

 

?>

 

Setting a variable from this like: 

 

$enrolment_year = JomSocial_field("enrolment_year");

 

And then using it like

 

 

 

<?php $filename1 = "folder/"$enrolment_year."/file.zip";

 

 

?

 

Thanks for any further guidance you can give

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.