benfolley Posted October 30, 2011 Share Posted October 30, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/250110-using-the-results-from-a-function-help-needed/ Share on other sites More sharing options...
ManiacDan Posted October 30, 2011 Share Posted October 30, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/250110-using-the-results-from-a-function-help-needed/#findComment-1283448 Share on other sites More sharing options...
benfolley Posted October 30, 2011 Author Share Posted October 30, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/250110-using-the-results-from-a-function-help-needed/#findComment-1283458 Share on other sites More sharing options...
benfolley Posted October 30, 2011 Author Share Posted October 30, 2011 Just FYI, this did work. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/250110-using-the-results-from-a-function-help-needed/#findComment-1283463 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.